Bitwise Operations Calculator
Perform bitwise AND, OR, XOR, NOT, left shift, and right shift operations on integers.
See results in binary, decimal, hexadecimal, and octal with step-by-step breakdown.
What Are Bitwise Operations? Bitwise operations work directly on the binary (bit-level) representation of integers. Each bit is processed independently — there is no “carry” between bits. They are extremely fast in hardware — often a single CPU clock cycle. Bitwise operations are fundamental in embedded systems, networking, cryptography, and low-level programming.
The Six Core Bitwise Operations AND (&): result bit is 1 only if BOTH input bits are 1. Used for masking: value & mask. OR (|): result bit is 1 if EITHER input bit is 1. Used for setting flags: value | flag. XOR (^): result bit is 1 if exactly ONE input bit is 1. Used for toggling: value ^ toggle. NOT (~): flips all bits. In two’s complement: ~n = −n − 1. Left shift («): shifts bits left by n positions, filling with zeros. Equivalent to multiplying by 2ⁿ. Right shift (»): shifts bits right. Arithmetic right shift preserves sign (fills with sign bit). Logical fills with 0.
Truth Table A | B | A&B | A|B | A^B 0 | 0 | 0 | 0 | 0 0 | 1 | 0 | 1 | 1 1 | 0 | 0 | 1 | 1 1 | 1 | 1 | 1 | 0
Common Bitwise Tricks Check if n is even: (n & 1) == 0 Check if power of 2: n > 0 && (n & (n-1)) == 0 Clear the lowest set bit: n & (n-1) Get the lowest set bit: n & (-n) Swap two variables: a ^= b; b ^= a; a ^= b; (no temp variable) Set bit k: n | (1 « k) Clear bit k: n & ~(1 « k) Toggle bit k: n ^ (1 « k) Test bit k: (n » k) & 1
Representation Systems Binary (base 2): 0 and 1 only. Prefix 0b. Example: 0b1010 = 10. Octal (base 8): digits 0–7. Prefix 0o or 0. Example: 0o12 = 10. Decimal (base 10): everyday numbers. Hexadecimal (base 16): 0–9 and A–F. Prefix 0x. Example: 0xFF = 255. Hex is popular in programming because one hex digit = exactly 4 bits (one nibble).
Two’s Complement Representation Most modern CPUs use two’s complement for signed integers. Positive numbers: standard binary. Negative numbers: flip all bits of the positive representation, then add 1. Range for N bits: −2^(N−1) to 2^(N−1) − 1. For 8-bit: −128 to 127. For 32-bit: −2,147,483,648 to 2,147,483,647. NOT flips all bits: ~5 (binary 00000101) = 11111010 = −6 in two’s complement.
Applications Networking: IP subnetting uses AND with subnet masks (192.168.1.100 & 255.255.255.0). Graphics: alpha blending, color channel manipulation. Cryptography: AES, SHA, and most ciphers rely heavily on XOR. Compression: Huffman coding, bit packing. Flags: Unix file permissions (rwxrwxrwx), CPU status registers.