Bitwise calculator

AND, OR, XOR, NOT and shifts, shown in binary, decimal and hex.

a
b
a
12

0x0000000C · 2 bits set

b
10

0x0000000A · 2 bits set

Operations

OperationDecimalHexBinary
AND (a & b)
Bit set only where both are set — masking
80x0000000800000000 00000000 00000000 00001000
OR (a | b)
Bit set where either is set — combining flags
140x0000000E00000000 00000000 00000000 00001110
XOR (a ^ b)
Bit set where exactly one is set — toggling
60x0000000600000000 00000000 00000000 00000110
NOT a (~a)
Every bit inverted — note the sign
-130xFFFFFFF311111111 11111111 11111111 11110011
AND NOT (a & ~b)
Clear the bits of b from a
40x0000000400000000 00000000 00000000 00000100
Left shift (a << 2)
Multiply by 2ⁿ
480x0000003000000000 00000000 00000000 00110000
Sig2ed right shift (a >> n)
Divide by 2ⁿ, keeping the sign
30x0000000300000000 00000000 00000000 00000011
U2signed right shift (a >>> n)
Fills with zeros from the left
30x0000000300000000 00000000 00000000 00000011

How it works

JavaScript's bitwise operators convert their operands to 32-bit signed integers, act on the bits, and convert back. That conversion is why ~5 is −6 rather than a large positive number, and why values above 2³¹ behave unexpectedly.

Two's complement

Negative numbers are represented by inverting all bits and adding one. The top bit is the sign, so 0xFFFFFFFF is −1 rather than 4,294,967,295. The unsigned right shift >>> is the one operator that treats the value as unsigned, which is the standard way to coerce a number to a uint32.

Bit flags

Packing booleans into one integer is still the standard way to represent permission sets and feature flags:

set: flags |= FLAG clear: flags &= ~FLAG toggle: flags ^= FLAG test: (flags & FLAG) !== 0

Shifts as arithmetic

x << n multiplies by 2ⁿ and x >> n divides by it, rounding toward negative infinity. Compilers do this transformation themselves, so writing shifts for speed in high-level code buys nothing and costs readability.

XOR tricks

XOR is its own inverse: a ^ b ^ b === a. That underpins one-time-pad encryption, simple checksums, and the classic “find the non-duplicated element” puzzle — XOR every value in a list and the pairs cancel out.