C + + bit operations
--binary calculation (by operation Priority)
Foreword: We all know that all the operations of a computer are actually performed in binary, so the speed of bit operations in binary conditions is actually very fast, and the time complexity of the program can be reduced by using proper bit arithmetic in programming.
~ Take the counter: ~x, said the X of you to reverse, that is, ~1=0,~1=0.
>> shift Right: x>>p, which means to move X to the right P-bit, which can be understood as the number x right-hand side of the binary. Example: (2) 10>>1= (1) 2>> 1 = = "(11) 2= (1); () 10>>2= (1111) 2>>2 = =" (2) 3 = (10)
<< left: x<<p, which means to shift x to the left P-bit, which can be understood as the number of binary x on the right side of P 0. Example: (1) 10<<1= (1) 2<<1 = = "(Ten) 2= (2), (3) 10<<3= (one) 2<<3 =" (11000) 2 = (24) 10
& Bitwise AND: X&p, which means that x and p are performed and operated in binary, and the law is: 1&1=1, 1&0=0, each of the 0&0=0,x and each of the P is computed according to this rule.
Example: 2&3 ==> 10, which results in 2
&
10
^ Bitwise XOR: X^p, which indicates that X and P perform an XOR operation in binary, the law is: each of the 1^1=0,1^0=1,0^0=0,x and p each is computed according to this rule.
Example: 2&4 = = "010, which results in 6
^
110
| bitwise OR: X|P, which means that X and p are carried out or operated in binary, the law is: each of the 1|1=1,1|0=1,0|0=0,x and P is calculated as the result of this rule.
Example: 2|4== "010, which results in 6
|
110
C + + bit operations (binary)