Bitwise Calculator
AND · OR · XOR · NOT · Left Shift · Right Shift · Decimal, Binary & Hex input · Visual bit diagrams
Input Format
Bit Width
Operation
Quick Examples
Visual Bit Diagram
Step-by-Step
Enter a value above to compute bitwise results
Enter two integers to see all bitwise operations at once. Negative numbers are shown in 32-bit two's complement.
| Operation | Expression | Decimal | Binary | Hex |
|---|
Enter values A and B to see all operations
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
| Operation | Symbol | Description | Example (4-bit) |
|---|---|---|---|
| AND | & | Both bits must be 1 | 1100 & 1010 = 1000 |
| OR | | | At least one bit is 1 | 1100 | 1010 = 1110 |
| XOR | ^ | Bits must be different | 1100 ^ 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.
| A | B | AND | OR | XOR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Real-World Applications
- Permission flags: Unix file permissions encode read (4), write (2), and execute (1) as bits. A permission value of 6 =
110in 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 with0xF0and 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 & 1returns 1 if n is odd, 0 if even. Much faster than the modulo operator. - Multiply / divide by power of 2:
n << kmultiplies by 2k;n >> kdivides 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 position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| 60 (A) | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
| 13 (B) | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |
| Result | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
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 position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| 5 (A) | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
| 3 (B) | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| Result | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
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:
| Step | Binary | Decimal |
|---|---|---|
| Start (4) | 0000 0100 | 4 |
| After 1 shift | 0000 1000 | 8 |
| After 2 shifts | 0001 0000 | 16 |
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?
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?
|) 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?
~) 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?
<<) 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?
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?
What is the difference between logical and bitwise 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.
Related Calculators
Binary / Decimal / Hex Converter
Convert between number bases with step-by-step working
Scientific Calculator
Full scientific functions with memory and history
Boolean Algebra Simplifier
Simplify boolean expressions and truth tables
Arithmetic Calculator
Add, subtract, multiply, divide with full step-by-step