All data is stored in binary form in the computer. Bit operations are actually operations on the binary data in memory, so the data processing speed is very fast.
Bit operation basics
There are 6 basic bit operators: And
, Or
, XOR
, Complement
, Left Shift
, and Right Shift
. Their operation rules are as follows:
Symbol | Description | Rule |
---|---|---|
& | And | The result is 1 when both bits are 1 |
| | Or | The result is 0 when both bits are 0 |
^ | Xor | 0 if both bits are the same, 1 if they are different |
~ | Complement | 0 becomes 1, 1 becomes 0 |
<< | Left shift | Each binary is shifted to the left by several bits, the high bits are discarded, and the low bits are padded with 0. |
>> | Right shift | Each binary bit is shifted to the right by several bits. Unsigned numbers, high-order bits are complemented with 0, and signed numbers. Each compiler handles the method differently. Some complement bits (arithmetic right shift), and some complement 0 (logical right shift). |
Note:
- Of these 6 operators, only
~
complement is a monocular operator, and the other 5 are binocular operators. - Bit operations can only be used for integer data. Bit operations on float and double types will be reported by the compiler as errors.
- The bit operator has a lower precedence, so try to use parentheses to ensure the order of operations, otherwise you may get some inexplicable results. For example, to get
2^i + 1
numbers like 1, 3, 5, and 9. It is wrong to writeint a = 1 << i + 1
; Should be written asint a = (1 << i) + 1
;
And (&)
1 | System.out.println(5 & 3); |
5 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0101
3 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0011
———————————————————–
1 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0001
Or (|)
1 | System.out.println(5 | 3); |
5 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0101
3 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0011
———————————————————–
7 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0111
Xor(^)
1 | System.out.println(5 ^ 3); |
5 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0101
3 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0011
———————————————————–
6 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0110
Complement (~)
1 | System.out.println(~5); |
5 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0101
———————————————————–
-6 convert to binary: 1111 1111 1111 1111 1111 1111 1111 1010
Left shift (<<)
1 | System.out.println(5<<2); |
5 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0101
———————————————————–
20 convert to binary: 0000 0000 0000 0000 0000 0000 0001 0100
Right shift (>>)
1 | System.out.println(5>>2); |
5 convert to binary: 0000 0000 0000 0000 0000 0000 0000 0101
———————————————————–
1 convert to binary: 00
00 0000 0000 0000 0000 0000 0000 0001
Common bit manipulation tips
Judging parity
Just need to check the least significant bit is 0 or 1, 0 is even and 1 is odd. So we can use if((a & 1) == 0)
instead of if(a% 2 == 0)
to determine whether a
is even. The following program will output all even numbers between 0 and 100:
1 | for (int i = 0; i < 100; i ++) { |
Exchange two numbers
1 | int a = 1, b = 2; |
- The first step
a^ = b
isa = (a^b)
; - The second step
b^ = a
isb = b^(a^b)
. Since the OR operation satisfies the commutative law,b^(a^b)
=b^b^a
. Since the result of OR of a number and itself is 0 and the OR of any number and 0 is unchanged,b
is assigned the value ofa
- The third step
a^ = b
isa = a^b
. Since the previous two steps show thata = (a^b)
andb = a
,a = a^b
isa = (a^b)^a
. Soa
is assigned the value ofb
.
Changing the sign
Changing the sign means that positive numbers become negative numbers, and negative numbers become positive numbers. For -11 and 11, you can use the following transformation method to change -11 to 11
1 | / 11 |
So we only need to complement the number and add 1.
1 | int a = -15, b = 15; |
Bit manipulation tips
1 | // 1. Get int max |