How the bitwise operators work
AND produces 1 only when both aligned bits are 1. OR produces 1 when either bit is 1. XOR produces 1 when the bits differ. NOT reverses every bit, so its result depends on the selected word width.
| A | B | AND | OR | XOR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Why word width and signed interpretation matter
A fixed-width register has only a set number of positions. This calculator discards overflow beyond that width. The same pattern can be read as unsigned or as signed two’s complement: for example, 11111111 is 255 unsigned but −1 signed in eight bits.
Shifts and rotations
A left shift inserts zeros at the right. A logical right shift inserts zeros at the left, while an arithmetic right shift repeats the sign bit. Rotation moves discarded bits around to the opposite end instead of losing them. A left shift is equivalent to multiplication by a power of two only when no significant bit overflows.
Practical bitwise use cases
Checking permissions and feature flags
Suppose READ = 4, WRITE = 2 and EXECUTE = 1. A permission value of 6 is binary 110. The expression 6 AND 4 returns 4, so READ is enabled; 6 AND 1 returns 0, so EXECUTE is not. OR can enable a flag and XOR can toggle it.
Extracting a colour channel
The packed colour 0xAC3C53 contains red, green and blue bytes. Shifting right by 16 and applying AND 0xFF extracts red as 0xAC, or 172 decimal. Similar masks decode packed dates, device registers and file headers.
Finding an IPv4 network address
A router combines an address with its subnet mask using AND. The last octet of 192.168.1.77 is 01001101 and the last octet of a /26 mask is 11000000. AND gives 01000000, or 64, making the network address 192.168.1.64.
Worked examples
| Expression | 8-bit result | What it demonstrates |
|---|---|---|
0xAC AND 0x3C | 0x2C | Keeps only bits present in both masks |
NOT 0xAC | 0x53 | Flips all eight bits |
0xAC logical-right 2 | 0x2B | Inserts two zero bits |
0xAC arithmetic-right 2 | 0xEB | Extends the leading sign bit |
rotate-left 0xAC by 1 | 0x59 | Returns the discarded high bit at the right |