C ++ bitwise AND (&), or (|), exclusive OR (^) and the like operators

It had previously been for these operators disagree, but recently discovered mastered these operators will actually improve the efficiency of problem-solving, code logic will be more clearly

 

a% = b is equivalent to a = a% b modulo and assignment. 

a | = b is equivalent to a = a | b and bitwise or assignment. 

a & = b is equivalent to a = a & b and bitwise assignment. 

a ^ = b is equivalent to a = a ^ b bitwise XOR and assignment. 

a! = b logic determination, a does not equal b, when ab true when unequal. 

&& logic, are true is true. 

|| logical OR, are both false is false, otherwise true. 

! a logical not, a false result is true, otherwise reversed. 

| Bitwise OR 

^ bitwise exclusive OR 

& Bitwise AND 

~ bitwise

  

 

Bitwise AND operator (&)

Two data participate in operations, according to the binary bits "and" operation.

0&0=0;  
0&1=0;    
1&0=0;     
1&1=1;   

0000 0011 & 0000 0101 = 0000 0001;

"The operation" special purpose:

(1) cleared. If you want to clear a unit, even if all its binary bit is 0, as long as a phase value you are zero, the result is zero.

(2) takes a specified bit number

Method: find a number, the corresponding bit of X to be fetched, the corresponding bit number is 1, the remaining bits are zero, and this number X is "the operation" can be specified bit of X.

Example: Let X = 10101110,

          X out of the lower 4 bits, with X & 0000 1111 = 0000 1110 can be obtained;

     X may also be used to take the 2,4,6 position.

 

Bitwise OR operator (|)

Two objects participate in the operation, according to the binary bits "or" operation.

0|0=0;   
0|1=1;   
1|0=1;    
1|1=1;

0000 0011 | 0000 0101 = 0000 0111 ;

  

Method: find a number, corresponding to the X set to 1, the corresponding bit number is 1, the remaining bits are zero. This number allows to X or X 1 in certain positions.

Example: 4 The low position of X = 10100000 1, with X | 0000 1111 = 1010 1111 can be obtained.

 

Exclusive-OR operator (^)

Two data participate in operations, according to a binary bit "exclusive or" operation.

That is: the two objects participate in operations, if the two bits corresponding to "exclusive" (different values), the result is 1 bit, and 0 otherwise.

0^0=0;   
0^1=1;   
1^0=1;   
1^1=0;

  

"XOR operation" special effect:

(1) a specific number to find a bit flip, X corresponds to flip you, the number of the corresponding bit is 1, the remaining bits are zero, this number corresponding to the X-bit to the exclusive OR.

  Example: X = 10101110, the lower 4 bits of the X-inverted by X ^ 0000 1111 = 1010 0001 can be obtained. 

(2) with or different from 0, to retain the original value, X ^ 0000 0000 = 1010 1110.

Negation operator (~)

A data participate in operations, according to the binary bits "inverted" operation.

Namely: a bitwise binary number, i.e. 0 becomes 1, 1 0 variable.

~1=0;  
~0=1;

  

So that a minimum number of zero bits, may be expressed as: a & ~ 1.

1111111111111110 ~ 1 value, then "AND" operation, some of the least significant bit 0.

Since "-" operator precedence than arithmetic operators, relational operators, logical operators, and other operators are high.

 

Left shift operator (<<)

All bits of each of the left operand a number of bits (binary digits discarded left, the right complement 0). Therefore, the following formula is equivalent to two

a = a << 2;
a = a * 2; 

  

Right shift operator (>>)

All bits of each of a number of a number of bits to the right, the left complement positive numbers 0, 1 negative fill the left and right discarded.

Operand every right shift, equivalent to the number divided by two.

 

>> operator to specify all the bits to the right of expression1 expression2 digits. expression1 sign bit is used to fill the vacated bits to the right of left. Right bit shifted out are discarded.

For example, the following code is evaluated, the value of temp -4:

   14 (ie, binary 11110010)

     Right two equal 4 (ie, binary 11111100).

     var temp = -14 >> 2

 

Unsigned right shift operator (>>>)

>>> operator the right to respective bits specified in expression2 expression1 bits. After the left to the right vacated bit positions are filled with zeroes. Right out bits are discarded.

For example: var temp = -14 >>> 2

     The variable temp is 14 (i.e., binary 1,111,111,111,111,111 1,111,111,111,110,010),

     Equal to 1,073,741,820 (ie binary 0,011,111,111,111,111 1,111,111,111,111,100) to the right after two

Data of different lengths bitwise operation

If two different sizes of data for bit operation, the system will press both the right alignment, and the bitwise operation.

In "and" operation example as follows: We know that in C language type is four bytes long, int type 2 bytes, if a long int-type data with a data type alignment "and" operation, right after the left insufficient bit according to the following three cases make up,

If the integer data is positive, the left complement 16 0

If the integer data is negative, fill the left 16 1

If the unsigned integer data is left also make 16 0


Such as: long a = 123; int b = 1; calculating a & b.

Such as: long a = 123; int b = -1; calculating a & b.

Such as: long a = 123; unsigned int b = 1; calculating a & b.

 

Reference: https: //blog.csdn.net/tkp2014/article/details/41172103

Guess you like

Origin www.cnblogs.com/jaszzz/p/12635375.html