Bitwise operations

Bitwise AND / OR / XOR / NOT / shifts with bit visualisation. Plus IEEE 754 — float32 broken into sign, exponent and mantissa.

01AND / OR / XOR02IEEE 75403About04Related
01

Bitwise operations

enter A and B, then pick an operator below
A · decimal
B · decimal
Operation
Result
100x0A
Bit breakdown
A0010101042
B0000111115
AND0000101010
02

IEEE 754 — float32

Floating-point number
Sign
0
positive
Exponent
10000000
128 −127 = 1
Mantissa
10010001111010111000011
1.4781507
HEX0x4048F5C3
BIN (32 bits)0 10000000 10010001111010111000011
03

About bitwise operations

AND, OR, XOR

Basics
0b0011 AND 0b0101 = 0b0001
Bitwise operations apply to each bit pair independently. AND — both must be 1. OR — at least one is 1. XOR — bits differ. Used for masking, setting/clearing flags, encryption (XOR is a basic building block), checksums.

Shifts <<, >>

Multiply / divide
5 << 2 = 20 · 20 >> 2 = 5
Shift left by n is equivalent to multiplying by 2^n, right — to integer division by 2^n. Faster than mul/div on a CPU. In JS the >>> operator is an unsigned right shift (important for 32-bit signed operations).

IEEE 754 · float32

3 parts
1 bit sign · 8 bits exponent · 23 bits mantissa
Standard representation of real numbers: 32 bits = sign + biased exponent + mantissa. 0.1 in binary is a repeating fraction (like 1/3 in decimal), which is why 0.1 + 0.2 ≠ 0.3. For money, use decimal / integer cents.

Bit flags

Practice
READ=1 WRITE=2 EXEC=4 · RWX = 7
Classic technique: each bit stores one boolean flag. Set: v |= FLAG. Clear: v &= ~FLAG. Check: v & FLAG. Toggle: v ^= FLAG. That's how Unix file permissions (chmod) work, along with UI settings, feature flags, and countless APIs.
«» added to favorites