x | y | x & y |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
x | y | x | y |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
x | y | x ^ y |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Disclaimer: We are taking byte
(8 bits) as our datatype to explain the
concepts instead of the usual integer.
1 << 3 = 8
00000001 << 3 = 00001000
Two types:
a. Signed Right Shift (>>):
64 >> 2 = 16
001000000 >> 2 = 00010000
-64 >> 2 = -16
111000000 >> 2 = 11110000
b. Unsigned Right Shift (>>>):
64 >>> 2 = 16
001000000 >>> 2 = 00010000
-64 >>> 2 = 56
111000000 >>> 2 = 00111000
byte mask = 1 << 3;
Explanation,
00000001 << 3 = 00001000
byte mask = (1 << 3) - 1;
Explanation,
00000001 << 3 = 00001000
00001000 - 00000001 = 00000111
byte mask = 0x55;
byte mask = 0xaa;
byte mask = 1 << 3;
byte num = num & ~mask;
Explanation,
00000001 << 3 = 00001000
~(00001000) = 11110111