What bitwise operations do
A bitwise operation treats a number as a row of bits and works on each position independently. They are the fastest operations a CPU has and show up everywhere: permission flags, network masks, hash functions, graphics, compression and protocol parsing.
| Operation | Symbol (C, JS, Go, Python) | Rule per bit | Typical use |
|---|---|---|---|
| AND | & | 1 only if both bits are 1 | Mask out bits, test a flag: x & FLAG |
| OR | | | 1 if either bit is 1 | Set a flag: x | FLAG |
| XOR | ^ | 1 if the bits differ | Toggle a flag, swap, checksums |
| NOT | ~ | Flip every bit | Build an inverse mask: x & ~FLAG clears a flag |
| Shift left | << | Move bits left, fill with 0 | Multiply by 2ⁿ, build a mask: 1 << n |
| Shift right | >> | Move bits right | Divide by 2ⁿ, extract a field |
| Rotate | rotl / rotr | Shift, bits that fall off re-enter at the other end | Hashes and ciphers |
Arithmetic versus logical right shift
Shifting right has two flavours. A logical shift fills the new top bits with zeros. An arithmetic shift copies the sign bit, so a negative two's-complement number stays negative. In C and Go, >> on a signed type is arithmetic and on an unsigned type is logical. JavaScript has both: >> is arithmetic and >>> is logical. Set the width to 8 and try -8 >> 1 here to see the difference.
Why the width matters
In a real register, bits that shift past the top are lost and NOT flips exactly as many bits as the register holds. ~0 is 255 in 8 bits, 65535 in 16 bits and 4294967295 in 32. This calculator always works in the width you pick, so results match what your code will see in a uint8_t, uint32 and so on.
Common idioms
Questions
What is the difference between logical and bitwise operators?
Logical operators (&&, ||, !) treat whole values as true or false and give a single true/false result. Bitwise operators (&, |, ^, ~) work on each bit of the operands and give a number. 6 && 3 is true, but 6 & 3 is 2.
Why does NOT give a negative number in my code?
If the variable is signed, the top bit is the sign bit. ~5 in 32-bit two's complement is 0xFFFFFFFA, which a signed int displays as −6. Store the result in an unsigned type, or mask it (~5 & 0xFF = 250), to see the raw bit pattern.
What happens to bits shifted past the end?
In a fixed-width register they are discarded. Shifting left can therefore lose the top bits (overflow); shifting right loses the bottom bits (that is why x >> 1 rounds down). Rotates differ: the bits re-enter at the other end.
Does XOR of a number with itself always give zero?
Yes. Every bit is compared with an identical bit, and XOR of equal bits is 0. That is why x ^ x = 0 and why XOR can be used to find the one unpaired element in a list where everything else appears twice.
Can I use this for 64-bit values?
Yes. The tool uses arbitrary-precision integers and masks to the width you choose, so 64-bit operations are exact, unlike JavaScript's native operators which stop at 32 bits.