XOR

Reference: http://blog.csdn.net/u012050154/article/details/51957530

Q: Given M positive integers, of which M-1 appears an even number of times and 1 appears an odd number of times, find the number.

     When I was asked this question, I said, store the M numbers in the hash table, and then you can find the number that appears an odd number of times. However, this is not a good solution, and the interviewer prompts for XOR. (The following is knowledge taken from the Internet)

     1. Bitwise AND (&) [0 is 0]

Operation rules: 0&0=0; 0&1=0; 1&0=0; 1&1=1;

use:

(1) Clear. If you want to clear a cell, just AND a value where each bit is 0.

(2) Take out the specified bit of a number. The value of the last specified bit is 1, and the remaining bits are 0.

Example: Set x=11101100, take the lower four bits of x. Let x&00001111=00001100

     2. Bitwise OR (|) [If there is 1, then 1]

Operation rules: 0|0=0; 0|1=1; 1|0=1; 1|1=1;

use:

(1) is often used to 1 for some positions . Or the value where the last specified bit is 1 and the remaining bits are 0.

Example: Set x=11101100, and set the lower four bits of x to 1. Let x|00001111=11101111

     3. XOR (^) [Same 0 X 1]

Operation rules: 0^0=0; 0^1=1; 1^0=1; 1^1=0;

use:

(1) Flip a specific bit, XOR the value of the last bit to be flipped is 1, and the remaining bits are 0.

Example: Set x=11101100, flip the lower four bits of x. Let x^00001111=11100011

(2) XOR with 0, keep the original value.

(3) Based on the XOR operation, without referring to the new variable, the values ​​of the two variables are exchanged

a = a ^ b ; b = a ^ b ; a = a ^ b

So, the solution to this programming problem is:

public class FindOdd {
    public static void main(String[] args) { int[]a={1,1,4,4,4,5,5}; for(int i=1;i<a.length;i++){ a[0]=a[0]^a[i]; } System.out.println(a[0]); } }

 Q2: Find the two numbers that appear an odd number of times in a string of numbers.

Ideas:

All the numbers in this string of numbers are XORed, and the result must be the XORed value of the two numbers that appear an odd number of times, and it is not 0, which is recorded as resExclusiveOR;

Find the first bit of resExclusiveOR that is 1, record it as indexOf1, divide the numbers whose indexOf1 bit is 1 in the array into a group of num1, and the numbers whose indexOf1 bit is 0 are grouped into a group of num2;

XOR the numbers in num1 and num2 respectively, and the result is the two numbers you are looking for. (with code, from the Internet)

public class FindOdd {
    static void findOnce(int[]data,int length){ int num1=0; int num2=0; if(length<2) return; int resExclusiveOR = data[0]; for(int i=1;i<length;i++){ resExclusiveOR^=data[i]; } int indexOf1 = findFirstBitIs1(resExclusiveOR); for(int i=0;i<length;i++){ if(isBit1(data[i],indexOf1)){ num1=num1^data[i]; }else{ num2=num2^data[i]; } } System.out.println(num1+" "+num2); } static int findFirstBitIs1(int r){ int indexBit=0; while((r&1)==0&&(indexBit<32)){ r=r>>1; indexBit++; } return indexBit; } static boolean isBit1(int num,int index){ if(((num>>index)&1)==1) return true; else return false; } public static void main(String[] args) { int[]arr={1,4,4,1,6,6,6,7,8,8,8,7}; findOnce(arr,12); } }

 17.11.9

(1) Calculate the number of 1s in the binary representation of a positive integer

method 1:

public static void main(String[] args) { int n=7; int count=0; while(n>0) { if((n&1)==1) { count++; } n=n>>1; } System.out.println(count); }

But there is a problem with this solution:

(Replenish:

Right shift: 00001010>>2=00000010; 10001010>>3=11110001

左移:00001010<<2=00101000;10001010<<3=01010000)

Method 2:

 private static int countBit(int num){ int count = 0; for(; num > 0; count++) { num &= (num - 1); } return count; }

refer to:

http://www.cnblogs.com/hapjin/p/5839797.html

http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html

(2) Determine whether the i-th bit of a number is 0 or 1

private static boolean getBit(int num,int b) { return ((num & (1 << b)) != 0); }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325376843&siteId=291194637
XOR