Bitwise operation 1). definition.
Refers to each of the 1 binary data bits to participate in the operation.
The premise of the bitwise operation is that the number must be 1 binary.
Attention:
a). Binary data that participates in bit operations must be in complement form.
b). The result of the bitwise operation is also the complement form of the binary.
2). Bitwise AND: &
Two binary numbers that participate in bitwise AND. If all are 1 then the result is 1 for 1 bits and 0 Then the result is 0.
3 & 2;
1th step: Get the two-digit twos complement form first.
3 Complement: 00000000 00000000 00000000 00000011
2 Complement: 00000000 00000000 00000000 00000010
-------------------------------------------------------
00000000 00000000 00000000) 00000010 2
-3 & 4;
-3 of the original code: 10000000 00000000 00000000 00000011
-3 anti-code: 11111111 11111111 11111111 11111100
-3 Complement: 11111111 11111111 11111111 11111101
4 Complement: 00000000 00000000 00000000 00000100
------------------------------------------------------
00000000 00000000 00000000 00000100
-3 &-4;
-4 of the original code: 10000000 00000000 00000000 00000100
-4 anti-code: 11111111 11111111 11111111 11111011
-3 Complement: 11111111 11111111 11111111 11111101
-4 Complement: 11111111 11111111 11111111 11111100
-----------------------------------------------------
11111111 11111111 11111111 11111100
The result is a complement: now revert to the inverse code
Reverse code: 11111111 11111111 11111111 11111011
Original code: 10000000 00000000 00000000 00000100
Attention:
The result of any digit bitwise AND 1 is: is the lowest bit of this number.
Ten & 1
xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxx0
00000000 00000000 00000000 00000001
------------------------------------
00000000 00000000 00000000 00000000
The lowest bit of even number must be 0. The minimum number of odd numbers must be 1.
So, if you want to judge whether this number is odd or even, just use this number to bitwise AND 1.
If the result is 1 then it is odd if the result is 0 then the result is an even number.
int num = 10;
if (num & 1) = = 0)
{
Even.
}
Else
{
Odd.
}
3). Bitwise OR. |
Binary data that participates in bitwise OR, as long as 1 bits is 1 then the result is 1 only if two bits are 0.
3 | 2;
3 Complement: 00000000 00000000 00000000 00000011
2 Complement: 00000000 00000000 00000000 00000010
------------------------------------------------------
00000000 00000000 00000000 00000011
-3 | 4
-3 Complement: 11111111 11111111 11111111 11111101
4 Complement: 00000000 00000000 00000000 00000100
------------------------------------------------------
11111111 11111111 11111111 11111101
The result is a 1 negative number.
Reverse code: 11111111 11111111 11111111 11111100
Original code: 10000000 00000000 00000000 00000011
-3 | -4
-3 Complement: 11111111 11111111 11111111 11111101
-4 Complement: 11111111 11111111 11111111 11111100
-------------------------------------------------------
11111111 11111111 11111111 11111101
4). Bitwise COUNTER: ~
The monocular operator. Take each bit of this binary number back.
to;
3 Complement: 00000000 00000000 00000000 00000011
11111111 11111111 11111111 11111100 complement
11111111 11111111 11111111 11111011 Anti-code
10000000 00000000 00000000 00000100 Original code
5). Bitwise XOR: ^
Bits that participate in bitwise XOR or binary data are 0 different if the same is 1.
3 ^ 2;
00000000 00000000 00000000 00000011 3 complement
00000000 00000000 00000000 00000010 2 complement
--------------------------------------
00000000 00000000 00000000) 00000001 1
The value of the exchange of two variables can be used in an XOR operation.
int a = 3;
int b = 2;
A = a ^ b; A = 1
b = a ^ b; b = 3
A = a ^ b; b = 2
1 ^ 2
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000010
-------------------------------------------------------
00000000 00000000 00000000) 00000011 3
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000011
------------------------------------------------------
00000000 00000000 00000000) 00000010 2
6). Bitwise left SHIFT operation. <<
Binary data that participates in bitwise left-shift operations. Moves the specified number of digits to the left. Low enough to fill 0 high overflow is discarded.
3 << 2;
00000000 00000000 00000000 00000011
000000 00000000 00000000 0000001100
12
Attention:
a). The left shift operation has the potential to change its positive and negative nature.
b). Shift the 1 number to the left N-bit, which is equivalent to multiplying the number by the N-squared of 2.
3 << 2 3 times 2 of 2.
<< 3; 16 * 8 128
5 * =????
5 << 4
7). Bitwise RIGHT SHIFT. >>
Participates in bitwise right-shift binary data. Moves the specified number of digits to the right. Low overflow drop, high fill sign bit.
3 >> 2;
00000000 00000000 00000000 00000011
0000000000 00000000 00000000 000000
>> 2;
00000000 00000000 00000000 00010000
00000000 00000000 00000000 000100
-16 >> 3
10000000 00000000 00000000 00010000
11111111 11111111 11111111 11101111
11111111 11111111 11111111 11110000
11111111111 11111111 11111111 11110
11111111111 11111111 11111111 11101 Anti-code
10000000000 00000000 00000000 00010
Attention.
a). The right-shift operation does not change the positive and negative nature.
b). 1 digits The bitwise right shifts n bits. Equals this number divided by 2 of the n-th square.
>> 2 equals 100/4
C-Language bitwise arithmetic