logo

Programming Languages - Bit Manipulation

Last Updated: 2021-12-05

AND(&)

| AND | 0   | 1   |
| --- | --- | --- |
| 0   | 0   | 0   |
| 1   | 0   | 1   |

OR(|)

| OR  | 0   | 1   |
| --- | --- | --- |
| 0   | 0   | 0   |
| 1   | 0   | 1   |

XOR(^)

| XOR | 0   | 1   |
| --- | --- | --- |
| 0   | 0   | 0   |
| 1   | 0   | 1   |

NOT(!)

| NOT |     |
| --- | --- |
| 0   | 1   |
| 1   | 0   |

Basic Operations

Bitwise And: &

int bitmask = 0x000F;
int val = 0x2222;

System.out.println(val & bitmask);
// 2

Bitwise exclusive OR: ^

Bitwise inclusive OR: |

Unary bitwise complement: ~

System.out.println(~256);
// -257

Signed left shift: <<

Signed right shift: >>

Unsigned right shift: >>>