DeveloperEducationUtility

Bitwise Playground

Type two numbers in decimal, hex, or binary and see every bitwise operation laid out as a bit grid. Click any bit on A or B to toggle it and watch AND, OR, XOR, NOT and the three shift variants update instantly.

Width

Adec 180hex 0xB4
Bdec 106hex 0x6A
Shift amount n2

Results

A AND B

A & B

dec 32hex 0x20

A OR B

A | B

dec 254hex 0xFE

A XOR B

A ^ B

dec 222hex 0xDE

NOT A

~A

dec 75hex 0x4B

A NAND B

~(A & B)

dec 223hex 0xDF

A << 2

A << n

dec 208hex 0xD0

A >>> 2

A >>> n (logical)

dec 45hex 0x2D

A >> 2

A >> n (arith)

dec 237hex 0xED

The bitwise operators

OperatorRuleUse case
A & B1 if both bits are 1Mask bits off, check flags
A | B1 if either bit is 1Combine flags, set bits
A ^ B1 if bits differFlip bits, swap without temp
~AFlip every bitNegate within a width
A << nShift left, fill 0 on the rightMultiply by 2n
A >>> nLogical right shift, fill 0 on leftUnsigned divide by 2n
A >> nArithmetic right shift, preserves sign bitSigned divide by 2n

Bit tricks worth knowing

  • Check bit k: (x >> k) & 1
  • Set bit k: x | (1 << k)
  • Clear bit k: x & ~(1 << k)
  • Toggle bit k: x ^ (1 << k)
  • Lowest set bit: x & −x — isolates the rightmost 1.
  • Clear lowest set bit: x & (x − 1) — counts bits one at a time.
  • XOR swap: a ^= b; b ^= a; a ^= b; swaps two integers without a temporary.
  • Power of two? x !== 0 && (x & (x − 1)) === 0.

Logical vs arithmetic right shift

>>> always fills the vacated high bits with 0 — useful when you treat the number as unsigned. >> copies the original sign bit into the high bits, so negative numbers stay negative when divided by a power of two. Toggle a high bit on a wide width and slide n to see the difference between the two right-shift outputs.

Width matters

The width selector decides how many low bits are kept. At 8-bit, ~0 equals 255; at 16-bit it's 65,535; at 32-bit it's 4,294,967,295. The same conceptual bit pattern produces very different decimal numbers depending on how wide a register you imagine it living in — a common source of confusion in C/Go and in network protocol code.