Bitwise Calculator
Compute AND, OR, XOR, NOT, left shift, right shift — results in decimal, binary, and hex
Bitwise Calculator
Input format hint
Result (signed decimal)
Result (unsigned 32-bit)
Result (hexadecimal)
Result (32-bit binary, 8-bit groups)
Common examples
Bitwise truth table (per bit) ▸
| A | B | A AND B | A OR B | A XOR B | NOT A |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 |
Bitwise operations are fundamental to systems programming, cryptography, graphics, and performance-critical code — yet they’re often confusing without a visual tool showing what happens at the bit level. This calculator computes bitwise AND, OR, XOR, NOT, left shift, and right shift, displaying results in decimal, binary (32-bit), and hex simultaneously.
The Six Bitwise Operations
All bitwise operations work on the individual bits of their operands, treating each bit independently (except shifts).
AND (&)
A bit is 1 only if both corresponding bits are 1. Used to extract or mask specific bits.
0011 1100 (60)
& 0000 1101 (13)
-----------
0000 1100 (12)
Use case: flags & MASK — check if specific flags are set.
OR (|)
A bit is 1 if either bit is 1. Used to set specific bits.
0011 1100 (60)
| 0000 1101 (13)
-----------
0011 1101 (61)
Use case: flags | NEW_FLAG — turn on a permission bit.
XOR (^)
A bit is 1 if the bits are different. Used to toggle bits or detect differences.
0011 1100 (60)
^ 0000 1101 (13)
-----------
0011 0001 (49)
Use cases:
- Toggle bits:
value ^ MASKflips exactly the bits in MASK - Swap two variables without temp:
a ^= b; b ^= a; a ^= b - Simple hash mixing: XOR is reversible and has no bias
NOT (~)
Inverts every bit. In 32-bit signed integers, ~n = -(n+1) due to two’s complement.
~ 0000 0000 0000 0000 0000 0000 0011 1100 (60)
= 1111 1111 1111 1111 1111 1111 1100 0011 (-61 signed, 4294967235 unsigned)
Use case: ~MASK — create the complement of a bitmask.
Left Shift (<<)
Shifts all bits left by N positions, filling with zeros on the right. Equivalent to multiplying by 2ᴺ.
60 << 2 = 240
0011 1100 → 1111 0000
Use case: 1 << n — create a mask with only bit n set. Fast power-of-2 multiplication.
Right Shift (>>)
Shifts all bits right by N positions. For signed integers in JS, fills left with the sign bit (arithmetic shift). Equivalent to integer-dividing by 2ᴺ.
60 >> 2 = 15
0011 1100 → 0000 1111
Use case: Extract the high byte: (value >> 8) & 0xFF.
Common Patterns
Extract a Byte from a Multi-Byte Value
const value = 0x12345678;
const byte0 = value & 0xFF; // 0x78 = 120 (low byte)
const byte1 = (value >> 8) & 0xFF; // 0x56 = 86
const byte2 = (value >> 16) & 0xFF; // 0x34 = 52
const byte3 = (value >> 24) & 0xFF; // 0x12 = 18 (high byte)
Test if a Number Is Even or Odd
// Faster than modulo in tight loops
const isOdd = n & 1; // true if odd
Round Down to Nearest Power of 2 Boundary
const aligned = value & ~(alignment - 1);
// e.g., align to 16-byte boundary: value & ~15 = value & 0xFFFFFFF0
Swap Two Values Without a Temp Variable
a ^= b;
b ^= a;
a ^= b;
// a and b are now swapped
Bitmask for Feature Flags
const FEATURE_DARK_MODE = 1 << 0; // 1
const FEATURE_ANALYTICS = 1 << 1; // 2
const FEATURE_BETA = 1 << 2; // 4
let flags = FEATURE_DARK_MODE | FEATURE_BETA; // 5
// Check: is dark mode on?
if (flags & FEATURE_DARK_MODE) { ... }
// Toggle analytics:
flags ^= FEATURE_ANALYTICS;
// Turn off beta:
flags &= ~FEATURE_BETA;
Two’s Complement and the NOT Gotcha
JavaScript’s bitwise operators work on 32-bit signed integers. The NOT operator follows two’s complement arithmetic: ~n = -(n+1).
This has a practical use: ~arr.indexOf(item) !== 0 is equivalent to arr.indexOf(item) !== -1 because ~(-1) = 0 (falsy) and ~(anything_else) ≠ 0 (truthy). Though modern code prefers .includes().
The unsigned right shift >>> (not in this calculator’s primary options but visible in binary output as 32-bit) treats the value as unsigned 32-bit, preventing sign-extension issues.
Practical Use Cases by Domain
| Domain | Operation | Example |
|---|---|---|
| Networking | AND to apply subnet mask | IP & subnet_mask |
| Graphics | Blend channel with mask | (color >> 16) & 0xFF |
| Cryptography | Mix entropy | hash ^= hash >> 13 |
| Embedded | Read hardware register flag | status & FLAG_BIT |
| Compression | Pack bits | (a & 0xF) |
| Hashing | FNV mix step | hash ^= byte; hash *= 16777619 |
Privacy
All calculations run entirely in your browser. No data is sent to any server.