Binary symbols explained

A: binary operator

&: And: 1 are to be true

例:A&B    A=12    B=3

A&B=1100&0011=0000

|: Or: There is a 1 will be true

Example: A | B A = 12 B = 3

A|B=1100|0011=1111

^: XOR: a is 0 to 1 the other two are not the same is true ---

Example: A ^ B A = 12 B = 3

A^B=1100^0011=1111

~: 0 will invert into 1, 1 is converted to the 0

Examples: ~ A A = 12

~A=0011

II: Application binary operator

1)n&(n-1)

(1) may be converted to a binary n know there are several requirements 1 --- a binary number represents the number 1

while(n>0){
    count++;
    n=n&(n-1);
}

(2) determining whether a number is a power of 2

IF (n-> 0 && ((& n-(N- . 1 )) == 0 )) 
COUT << " The number is a power of 2 " << endl;

(2) calculated N! Prime factors of the number 2

Easily draw quality factor number 2 N = [N / 2] + [ N / 4] + [N / 8] + ...!
Here is a simple example of what procedure is derived: N = 10101 (in binary representation)
now we track the most significant bit is 1, without considering other bits assumed to be 0,
then the
[N / 2] 01000
[N /. 4] 00100
[N /. 8] 00010
[N /. 8] 00001
all add up to 01111 = 10000 - 1
! and thereby push the other bits can be obtained: (10101) number of prime factors of 2 10000--1 + 00100--1 + 00001--1 = 10101--3 (binary representation of the number 1)

Push and general N! 2, the number of prime factors is N (N indicates the number of binary 1)

 

2)n&(-n)

In the tree to find the array lowbit t appear in the form factor of 2 ^ k number n is the number used to obtain the rightmost 1 can be known in which there are several factors 2

10: 0000 1010

-10: 1111 0110

10 & (- 10) = 0010 2 is a factor of 10 so as to have a 2, in the form of 2 ^ 2 ^ k 1

8 & (- 8) = [1000] = 8, 8 so as factor 2 has 3, in the form of 2 ^ 2 ^ 3 k

 

Guess you like

Origin www.cnblogs.com/Aiahtwo/p/11407440.html