BitwiseCalc

Bitwise Operations: Complete Reference

Every truth table, every bit hack, every shift pattern — one page. For embedded devs, systems programmers, and anyone who works close to the metal.

Truth Tables — Core Operations

AND (&)

ABA&B
111
100
010
000

1 only if both are 1
Use: bit masking / flag check

OR (|)

ABA|B
111
101
011
000

1 if either is 1
Use: set bits / combine flags

XOR (^)

ABA^B
110
101
011
000

1 only if bits differ
Use: toggle / swap / parity

NOT (~)

A~A
10
01

Flips every bit
Use: bitwise complement

NAND

AB~(A&B)
110
101
011
001

Universal gate in hardware
Use: CPU logic circuits

NOR

AB~(|)
110
100
010
001

Also a universal gate
Use: CPU logic circuits

XNOR

AB~(^)
111
100
010
001

1 only if bits are equal
Use: equality check

Bit Shift — Visual Guide (8-bit example: 0b00101101 = 45)

Original
00101101
= 45
45 << 1
01011010
= 90 (×2)
45 << 2
10110100
= 180 (×4)
45 >> 1
00010110
= 22 (÷2, trunc)
45 >> 2
00001011
= 11 (÷4, trunc)

Arithmetic right shift (>>) preserves sign bit (MSB). Logical right shift (>>>) always fills with 0.
Left shift (<<) always fills with 0 regardless of signedness.

Operator Precedence (C/Java/JS — highest to lowest)

PrecedenceOperatorDescriptionAssociativity
1~Bitwise NOTRight-to-left
2<< >> >>>Bit shiftsLeft-to-right
3&Bitwise ANDLeft-to-right
4^Bitwise XORLeft-to-right
5|Bitwise ORLeft-to-right

Common pitfall: x & 1 == 0 parses as x & (1 == 0), not (x & 1) == 0. Always parenthesize.

20 Essential Bit Manipulation Tricks

Set nth bitx | (1 << n)Set bit n to 1
Clear nth bitx & ~(1 << n)Set bit n to 0
Toggle nth bitx ^ (1 << n)Flip bit n
Check nth bit(x >> n) & 1Returns 0 or 1
Is power of 2?x && !(x & (x-1))x>0 and only 1 bit set
Multiply by 2ⁿx << nFast x·2ⁿ
Divide by 2ⁿx >> nFast floor(x/2ⁿ)
Modulo 2ⁿx & ((1<<n) - 1)x % 2ⁿ (non-negative)
Is even?(x & 1) == 0LSB is 0
Is odd?x & 1LSB is 1
Swap without tempa^=b; b^=a; a^=bXOR swap (integers only)
Clear lowest set bitx & (x - 1)Brian Kernighan
Isolate lowest set bitx & -xTwo's complement trick
Count set bits__builtin_popcount(x)POPCNT instruction
Set all bits to 1~0All-ones mask
Extract lowest bytex & 0xFFLow 8 bits
Merge bits from a,b(a & mask) | (b & ~mask)mask=1→from a
Sign extend (byte→int)(x ^ 0x80) - 0x808 to 32-bit sign ext
Next power of 21 << (32 - __builtin_clz(x-1))Round up to next 2ⁿ
Upper/lowercase flipc ^ 32'A'↔'a' due to ASCII offset

Signed Integer Ranges

TypeBitsMinMaxUnsigned Max
int8_t8-128127255 (uint8_t)
int16_t16-32,76832,76765,535 (uint16_t)
int32_t32-2,147,483,6482,147,483,6474,294,967,295
int64_t64−9.22×10¹⁸9.22×10¹⁸1.84×10¹⁹

Try it live — Bitwise Calculator

AND, OR, XOR, NOT, shift — enter two numbers and see the result in binary, hex, and decimal instantly.

Open Bitwise Calculator →

Operators follow C17/C++20 standards. Ranges follow ISO C <stdint.h>. Last updated August 2026.