Computing
Key Aspect 1: They are binary operators, both of which are integers, and the result is also an integer.
2 "<" Left shift: Fill 0 on the right blank bit. The left bit will be squeezed out from the header, and its value is equal to 2.
3 ">" right shift: The right bit is squeezed out. For the vacant positions removed from the left, if it is a positive number, the vacant space is supplemented with 0. If it is a negative number, it may be supplemented with 0 or 1, depending on the computer system used.
4 ">>>" operator, the right bit is squeezed out, and 0 is added for the left removed space.
 
Application of bitwise operators (source operand s mask)
(1) bitwise AND --&
1. Reset the special location (0 for a specific position in the mask, 1 for other bits, s = s & mask)
2. Locate a specified position in a specified number (1 in mask, 0 for other bits, s = s & mask)
(2) bitwise OR -- |
It is often used to set the source operand to some positions of 1, and the other BITs remain unchanged. (In mask, the specific position is 1, and the other bit is 0 s = s | mask)
(3) bitwise OR -- ^
1. Reverse the value of the specific location (1 in a specific position in mask and 0 s = s ^ mask in other bits)
2. If the third variable is not introduced, the values of the two variables are exchanged (set a = a1, B = b1)
Status after target operation
A = a1 ^ b1 a = a ^ B a = a1 ^ b1, B = b1
B = a1 ^ b1 ^ b1 B = a ^ B a = a1 ^ b1, B = a1
A = b1 ^ a1 ^ a1 a = a ^ B a = b1, B = a1
 
Binary complement calculation formula:
-X = ~ X + 1 = ~ X-1)
~ X =-X-1
-(~ X) = x + 1
~ (-X) = X-1
X + y = x -~ Y-1 = (x | y) + (x & y)
X-y = x ++ ~ Y + 1 = (x | ~ Y )-(~ X & y)
X ^ y = (x | y)-(x & y)
X | y = (x &~ Y) + y
X & y = (~ X | y )-~ X
X = y :~ (X-y | y-x)
X! = Y: x-y | y-x
X <y: (x-y) ^ (x ^ y) & (x-y) ^ x ))
X <= y: (x | ~ Y) & (x ^ y) | ~ (Y-x ))
X <y :(~ X & y) | ((~ X | y) & (x-y) // comparison of unsigned x and y
X <= y :(~ X | y) & (x ^ y) | ~ (Y-x) // unsigned x, y comparison
 
Application Example
(1) judge whether int variable a is an odd or even number.
A & 1 = 0 even
A & 1 = 1 odd
(2) Take the k-bit (k =, 2…) of int-type variable ...... Sizeof (int), that is, a> k & 1
(3) Clear the k bit of int variable a by 0, that is, a = &~ (1 <k)
(4) Place the k position of int type variable a 1, that is, a = a | (1 <k)
(5) int type variable loops are shifted k times left, that is, a = a <k | a> 16-k (set sizeof (int) = 16)
(6) int type variable a shifts k times to the right, that is, a = a> k | a <16-k (set sizeof (int) = 16)
(7) Average integer
If two integers x and y are used to calculate the average value (x + y)/2, the overflow will occur, because x + y may be greater than INT_MAX, however, we know that their average values will certainly not overflow. We use the following algorithm:
Int average (int x, int y) // returns the average value of X and Y.
{
Return (x & y) + (x ^ y)> 1 );
}
(8) judge whether an integer is the power of 2. For a number x> = 0, determine whether it is the power of 2.
Boolean power2 (int x)
{
Return (x & (x-1) = 0) & (x! = 0 );
}
(9) two integers are not exchanged using temp.
Void swap (int x, int y)
{
X ^ = y;
Y ^ = x;
X ^ = y;
}
(10) Calculate the absolute value
Int abs (int x)
{
Int y;
Y = x> 31;
Return (x ^ y)-y; // or: (x + y) ^ y
}
(11) modulo operation into bitwise operation (without Overflow)
A % (2 ^ n) is equivalent to a & (2 ^ n-1)
(12) multiplication is converted into bitwise operations (without Overflow)
A * (2 ^ n) is equivalent to a <n
(13) Division operations are converted into bitwise operations (without Overflow)
A/(2 ^ n) is equivalent to a> n
For example: 12/8 = 12> 3
(14) a % 2 is equivalent to a & 1
(15) if (x = a) x = B;
Else x =;
It is equivalent to x = a ^ B ^ x;
(16) The opposite number of x is expressed (~ X + 1)
# Include <stdio. h>
// Set the y-th of x to 1.
# Define setbit (x, y) (x) | = (1 <(Y-1 ))
// Obtain the y-th value of x.
# Define BitGet (Number, pos) (Number) >>( pos-1) & 1)
// Print the value of x
# Define print (x) printf ("% d \ n", x)
// Round the integer (4 bytes) to the right to move k digits
# Define Rot (a, k) (a) <(k) | (a)> (32-k ))
// Determine whether a is a power of 2
# Define POW2 (a) & (A-1) = 0) & (! = 0 ))
# Define OPPX (x )(~ (X) + 1)
// Returns the average values of X and Y.
Int average (int x, int y)
{
Return (x & y) + (x ^ y)> 1 );
}
// Determine whether a is a power of 2
Bool power2 (int x)
{
Return (x & (x-1) = 0) & (x! = 0 );
}
// Swap x with y
Void swap (int & x, int & y)
{
X ^ = y;
Y ^ = x;
X ^ = y;
}
Int main ()
{
Int a = 0x000D;
Print ();
Int B = BitGet (a, 2 );
Print (B );
Setbit (a, 2 );
Print ();
Print (BitGet (a, 2 ));
Int c = Rot (a, 33 );
Print (c );
Print (BitGet (c, 5 ));
Printf ("8 + 5 = % d \ n", average (8,692 ));
Int I;
For (I = 0; I <1000; I ++)
{
If (POW2 (I) // call power2 (I)
{
Printf ("%-5d", I );
}
}
Printf ("\ n ");
Int x = 10, y = 90;
Swap (x, y );
Print (x );
Print (y );
Print (OPPX (-705 ));
Return 0;
}