Skip to content
OneKitly

Shifting Left by Two Is Multiplying by Four

Published 11/30/2026 · 3 min read · Developer tools

Daniel Okonkwo

Daniel Okonkwo — Front-end developer and tech writer at OneKitly

Web performance · File formats

Checked against 2 sources

View profile →
In short

Twelve in binary is 1100 and ten is 1010. AND keeps the bits both numbers have: 1000, which is 8. OR keeps the bits either has: 1110, which is 14. XOR keeps the bits exactly one has: 0110, which is 6. Shifting moves the whole pattern: 1100 shifted two places left becomes 110000, which is 48, and shifted two right becomes 11, which is 3. Every shift left is a multiplication by two, every shift right an integer division — the same reason moving a decimal digit multiplies by ten.

Advertisement

12 << 2 is 48 and 12 >> 2 is 3. The operators look cryptic and are ordinary arithmetic in base two — until a sign bit or a word width gets involved.

XOR is its own undo

Apply XOR with the same value twice and you are back where you started, because a bit flipped twice is unflipped. That single property carries an astonishing amount of software: toggling a flag, swapping two variables without a third, the parity bit that catches a single-bit error, and the one-time pad, which is unbreakable precisely because the key is as long as the message and used once. It is also why XOR is the wrong tool for hiding data — reusing a key on two messages leaks their difference immediately.

Word width is where the surprises live

Shifting left eventually pushes bits off the end, and what happens then depends on the type: an 8-bit value shifted far enough becomes zero, a signed value can become negative, and in some languages the shift count itself wraps around so that shifting by 32 does nothing at all. Shifting right divides, but for negative numbers there are two answers — one that preserves the sign and one that does not — and different languages choose differently. The calculator asks for a width for exactly this reason: without one, the question is incomplete.

Bits
12 and 10 in binary
OperationBitsValue
12 AND 1010008
12 OR 10111014
12 XOR 1001106
12 << 211000048
Bitwise CalculatorAND, OR, XOR, NOT and shifts in binary, decimal or hex, at 8, 16, 32 or 64 bits, with the bit-by-bit diagram and the result in all three bases. Computed with BigInt, so the 64-bit answers are right where JavaScript's own 32-bit operators would silently truncate.Try the tool →
Advertisement

Frequently asked questions

Are bitwise tricks still faster than arithmetic?
Rarely, on a modern compiler. Writing x << 3 instead of x * 8 saves nothing, because the compiler already emits the shift, and writing x & 7 instead of x % 8 is the same story. Where the operators still earn their place is where the meaning is genuinely about bits — flags packed into an integer, colour channels, network protocol headers, hardware registers — and there the readable choice is the bitwise one, not the arithmetic one.
Why is AND used to extract a field?
Because ANDing with a pattern of ones keeps exactly those positions and zeroes the rest — a mask. Reading the middle four bits of a byte is an AND with 00111100 followed by a shift right of two, which is how nearly every packed format is decoded. The reverse operation writes a field: clear the positions with an AND against the inverted mask, then set them with an OR. Two operators, in that order, cover most of what protocol code does.

Articles you may find interesting

All guides →

Related tools

Sources

Spotted a mistake in this article?