Bitwise Calculator

AND · OR · XOR · NOT · Left Shift · Right Shift · Decimal, Binary & Hex input · Visual bit diagrams

Input Format

Bit Width

Operation

Quick Examples

Enter a value above to compute bitwise results

What is a Bitwise Operation?

Bitwise operations work directly on the binary representations of integers at the bit level. Rather than treating a number as a single value, the CPU processes each bit independently using simple logic gates. The six fundamental bitwise operations are AND, OR, XOR, NOT, left shift, and right shift. They are available in virtually every programming language and form the backbone of low-level systems programming, cryptography, graphics, and network protocols.

Every integer is stored in memory as a sequence of bits (binary digits: 0 or 1). An 8-bit integer can hold 256 distinct values (0–255), a 16-bit integer holds 65,536 values, and a 32-bit integer holds over 4 billion values. Bitwise operations let you read, set, clear, or toggle individual bits within those values with a single CPU instruction — far faster than arithmetic alternatives.

Bitwise Operations Reference Table

OperationSymbolDescriptionExample (4-bit)
AND&Both bits must be 11100 & 1010 = 1000
OR|At least one bit is 11100 | 1010 = 1110
XOR^Bits must be different1100 ^ 1010 = 0110
NOT~Invert all bits~1100 = 0011
Left Shift<<Shift bits left (×2 per position)0011 << 1 = 0110
Right Shift>>Shift bits right (÷2 per position)1100 >> 1 = 0110

Truth Table

For each pair of single bits, the truth table below shows every possible result. XOR (exclusive OR) is the key differentiator from plain OR — it produces 0 when both bits are 1.

ABANDORXOR
00000
01011
10011
11110

Real-World Applications

  • Permission flags: Unix file permissions encode read (4), write (2), and execute (1) as bits. A permission value of 6 = 110 in binary means read + write but no execute. chmod 755 = owner 7 (rwx), group 5 (r-x), others 5 (r-x).
  • Masking bits: To extract the lower nibble of a byte, AND with 0x0F: 0xAB & 0x0F = 0x0B. To extract the upper nibble, AND with 0xF0 and shift right by 4.
  • Toggle a bit: XOR the value with a mask that has a 1 in the target position: x ^ (1 << n) flips bit n without affecting other bits.
  • Check if odd: n & 1 returns 1 if n is odd, 0 if even. Much faster than the modulo operator.
  • Multiply / divide by power of 2: n << k multiplies by 2k; n >> k divides by 2k (integer division). Compilers use this optimization automatically.
  • Color channel extraction: A 32-bit RGBA color packs four 8-bit channels. Red = (color >> 16) & 0xFF, Green = (color >> 8) & 0xFF, Blue = color & 0xFF.
  • Cryptography: XOR is the foundation of stream ciphers and one-time pads. Because XOR is its own inverse, encrypting and decrypting use the same operation: (A XOR key) XOR key = A.

Worked Examples

Example 1: 60 AND 13

In binary (8-bit): 0011 1100 AND 0000 1101

Bit position76543210
60 (A)00111100
13 (B)00001101
Result00001100

Result: 0000 1100 = 12 (decimal) = 0x0C (hex). Only the bits where both A and B had a 1 survived.

Example 2: 5 OR 3

In binary (8-bit): 0000 0101 OR 0000 0011

Bit position76543210
5 (A)00000101
3 (B)00000011
Result00000111

Result: 0000 0111 = 7 (decimal) = 0x07 (hex). Any bit that was 1 in either A or B appears in the result.

Example 3: 4 Left Shift 2

4 << 2: take binary 4 (0000 0100) and shift every bit two positions to the left, filling with zeros on the right:

StepBinaryDecimal
Start (4)0000 01004
After 1 shift0000 10008
After 2 shifts0001 000016

Result: 16. Each left shift doubles the value, so 2 shifts = ×4. This is equivalent to 4 × 2² = 16.

Frequently Asked Questions

What is a bitwise AND operation?
A bitwise AND compares each corresponding bit of two numbers. The result bit is 1 only when both input bits are 1; otherwise it is 0. For example, 12 AND 10 in binary is 1100 & 1010 = 1000 (decimal 8). AND is commonly used for masking — isolating specific bits within a value.
What is the difference between OR and XOR?
OR (|) produces a 1 if at least one of the two bits is 1. XOR (^) produces a 1 only when the two bits are different. So 1 XOR 1 = 0, whereas 1 OR 1 = 1. XOR is often used to toggle bits or detect differences between values, and is the foundation of many encryption algorithms.
What does the NOT operator do?
The NOT operator (~) inverts every bit — each 0 becomes 1 and each 1 becomes 0. In JavaScript and most languages, NOT operates on 32-bit signed integers using two's complement, so ~5 = -6 (formula: ~n = -(n+1)). This calculator displays the unsigned 32-bit result alongside the signed value so you can see the raw bit pattern clearly.
How does left shift multiply by 2?
Left shift (<<) moves all bits one position to the left, inserting a 0 on the right. Because binary is positional (each position is worth double the previous), moving every bit left by one doubles the value. Shifting by k positions multiplies by 2k. For example, 3 << 2 = 12 (3 × 4). This is faster than multiplication in low-level code and is a common compiler optimization.
What is a bitmask?
A bitmask is a value used with AND, OR, or XOR to selectively read, set, or clear specific bits. To extract the lower nibble of a byte, AND with 0x0F (0000 1111). To set bit 3, OR with 0x08 (0000 1000). To toggle bit 3, XOR with 0x08. Bitmasks are fundamental in flags, file permissions, hardware registers, network protocol headers, and graphics color manipulation.
How are bitwise operations used in programming?
Bitwise operations appear across many areas of programming. Unix file permissions encode read/write/execute as bit flags. Network protocols pack multiple fields into single integers. Graphics code extracts RGB channels with bit masks. Embedded systems control hardware registers bit-by-bit. Cryptography algorithms (AES, SHA) rely on XOR. Hashing algorithms use shifts to mix bits. Compilers automatically replace simple multiplications and divisions with faster shifts. Understanding bitwise ops is essential for competitive programming, systems programming, and performance-critical code.
What is the difference between logical and bitwise operators?
Logical operators (&&, ||, !) work on boolean values and return a boolean. They also short-circuit: in A && B, B is not evaluated if A is false. Bitwise operators (&, |, ~, ^) work on the individual bits of integers and always evaluate both operands. Confusing them is a common bug — for example, 5 & 3 = 1 (bitwise AND), while 5 && 3 = true (logical AND). In JavaScript, using | instead of || in conditions can cause very subtle errors.