|
| 1 | +# About Bit Manipulation |
| 2 | + |
| 3 | +Java has operators for manipulating the bits of an [integral type][integral-type] (a `byte`, `short`, `int`, `long` or `char`). |
| 4 | + |
| 5 | +## Shift operators |
| 6 | + |
| 7 | +Use `<<` to shift bits to the left and `>>` to shift to the right. |
| 8 | + |
| 9 | +```java |
| 10 | +// Shift two places to the left |
| 11 | +0b0000_1011 << 2; |
| 12 | +// # => 0b0010_1100 |
| 13 | + |
| 14 | +// Shift two places to the right |
| 15 | +0b0000_1011 >> 2; |
| 16 | +// # => 0b0000_0010 |
| 17 | +``` |
| 18 | + |
| 19 | +`>>` is also called a [signed shift or arithmetic shift][arithmetic-shift] operator because the bit it is inserts is same as its sign bit. |
| 20 | +This is the left most bit and is 0 if the value is positive or 1 when negative. |
| 21 | +Shifting to the left with `<<` always inserts 0s on the right hand side. |
| 22 | + |
| 23 | +```java |
| 24 | +// Shift 2 places to the right preserves the sign |
| 25 | +// The binary value is two's complement of 0b0000_0110 (or 0x00000026), so the binary representation will be |
| 26 | +// 1000_0000_0000_0000_0000_0000_0010_0110 |
| 27 | +int value = -0x7FFFFFDA; |
| 28 | + |
| 29 | +// Shift two places to the right, preserving the sign bit |
| 30 | +value >> 2; |
| 31 | +// # => 1110_0000_0000_0000_0000_0000_0000_1001 |
| 32 | +``` |
| 33 | + |
| 34 | +Use `>>>` instead when 0s are to be inserted when shifting to the right. |
| 35 | +Inserting 0s when shifting is also known as a [logical shift][logical-shift]. |
| 36 | + |
| 37 | +```java |
| 38 | +// Shift two places to the right, inserting 0s on the left |
| 39 | +value >>> 2; |
| 40 | +// # => 0010_0000_0000_0000_0000_0000_0000_1001 |
| 41 | +``` |
| 42 | + |
| 43 | +## Bitwise Operations |
| 44 | + |
| 45 | +### Bitwise AND |
| 46 | + |
| 47 | +The bitwise AND (`&`) operator takes two values and performs an AND on each bit. |
| 48 | +It compares each bit from the first value with the bit in the same position from the second value. |
| 49 | +If they are both 1, then the result's bit is 1. |
| 50 | +Otherwise, the result's bit is 0. |
| 51 | + |
| 52 | +```java |
| 53 | +0b0110_0101 & 0b0011_1100; |
| 54 | +// # => 0b0010_0100 |
| 55 | +``` |
| 56 | + |
| 57 | +### Bitwise OR |
| 58 | + |
| 59 | +The bitwise OR (`|`) operator takes two values and performs an OR on each bit. |
| 60 | +It compares each bit from the first value with the bit in thes same position from the second value. |
| 61 | +If either bit is 1, the result's bit is 1. |
| 62 | +Otherwise, it is 0. |
| 63 | + |
| 64 | +```java |
| 65 | +0b0110_0101 | 0b0011_1100; |
| 66 | +// # => 0b0111_1101 |
| 67 | +``` |
| 68 | + |
| 69 | +### Bitwise XOR |
| 70 | + |
| 71 | +The bitwise XOR operator (`^`) performs a bitwise XOR on two values. |
| 72 | +Like the bitwise AND and bitwise OR operators, it compares each bit from the first value against the bit in the same position from the second value. |
| 73 | +If only one of them is 1, the resulting bit is 1. |
| 74 | +Otherwise, it is 0. |
| 75 | + |
| 76 | +```java |
| 77 | +0b0110_0101 ^ 0b0011_1100; |
| 78 | +// # => 0b0101_1001 |
| 79 | +``` |
| 80 | + |
| 81 | +### Bitwise NOT(`~`) |
| 82 | + |
| 83 | +Lastly, the bitwise NOT operator (`~`) flips each bit. |
| 84 | +Unlike the earlier operators, this is a unary operator, acting only on one value. |
| 85 | + |
| 86 | +```java |
| 87 | +~0b0110_0101; |
| 88 | +// # => 0b1001_1010 |
| 89 | +``` |
| 90 | + |
| 91 | +[integral-type]: https://docs.oracle.com/javase/specs/jls/se21/html/jls-4.html#jls-4.2 |
| 92 | +[arithmetic-shift]: https://en.wikipedia.org/wiki/Arithmetic_shift |
| 93 | +[logical-shift]: https://en.wikipedia.org/wiki/Logical_shift |
0 commit comments