Bitwise Calculator | AND, OR, XOR
Perform bitwise AND, OR, XOR, NOT, left shift, and right shift on 8/16/32-bit integers. Interactive bit toggle panel, colour-coded bit grid, multi-base output, and common bit manipulation patterns.
Operation
Bit Width
Input Mode
Click any bit to toggle:
Click any bit to toggle:
Quick Examples
Common Bit Manipulation Patterns
n & 1Check odd: result is 1 if n is odd, 0 if evenn & (n-1)Clear lowest set bit, also tests if n is a power of 2 (result = 0)n | (1 << k)Set bit k (make it 1)n & ~(1 << k)Clear bit k (make it 0)n ^ (1 << k)Toggle bit k (flip it)(n >> k) & 1Read bit k (returns 0 or 1)n & 0xFFExtract lowest byte (mask)n << 1Multiply by 2 (faster than × for powers of 2)n >> 1Divide by 2 (unsigned, floor)n ^ nAlways 0, used in XOR swap trickPress Enter to calculate · Esc to reset · Inputs auto-saved to browser
What Is the Bitwise Calculator | AND, OR, XOR?
This calculator performs all six bitwise operations: AND, OR, XOR, NOT, left shift, and right shift, on 8, 16, or 32-bit integers. It goes far beyond showing a decimal result: you get a colour-coded bit grid where every individual bit is visible, plus multi-base output in decimal, binary, hex, and octal.
- ›Interactive bit panels, click any bit in the A or B panel to toggle it on/off. The input field updates instantly, so you can build values bit-by-bit without knowing the decimal equivalent.
- ›8 / 16 / 32-bit widths, results are masked to the chosen width so you see exactly what a real 8-bit register or 32-bit integer would hold.
- ›Dec / Bin / Hex input modes, enter values in any base. Switching modes automatically converts existing values so you never have to calculate the conversion manually.
- ›Shift amount slider + field, drag the slider or type to set the shift count. A live label shows the equivalent multiply/divide factor.
- ›Popcount and bit statistics, the result panel shows the number of set bits (1s), clear bits (0s), MSB, and LSB at a glance.
- ›Common patterns reference, a collapsible panel lists the most-used bit manipulation idioms with concise explanations.
Formula
Per-bit rules (applied to every bit position independently)
AND (both 1 → 1)
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
OR (either 1 → 1)
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
XOR (different → 1)
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
NOT (flip every bit)
~0 = 1 ~1 = 0
~00001100₂ = 11110011₂ (8-bit)
Shifts
a < < n = a × 2ⁿ (left shift)
a > > n = a ÷ 2ⁿ (right shift, unsigned)
All Six Operations at a Glance
| Operation | Symbol | Rule | Example | Result |
|---|---|---|---|---|
| AND | & | Both bits 1 → 1 | 1100 & 1010 | 1000 |
| OR | | | Either bit 1 → 1 | 1100 | 1010 | 1110 |
| XOR | ^ | Bits differ → 1 | 1100 ^ 1010 | 0110 |
| NOT | ~ | Flip every bit | ~00001100 | 11110011 |
| LSHIFT | << | Shift bits left (× 2 each step) | 0011 << 2 | 1100 |
| RSHIFT | >> | Shift bits right (÷ 2 each step) | 1100 >> 2 | 0011 |
Single-bit Truth Table
| A | B | A AND B | A OR B | A XOR B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Each row applies independently to every bit position. An 8-bit AND runs the table simultaneously for bits 7 down to 0.
How to Use
- 1Select an operation: Click AND, OR, XOR, NOT, << Shift, or >> Shift from the operation tabs. The input area adapts, B field is hidden for NOT; a shift slider appears for shift operations.
- 2Choose bit width and input mode: Select 8-bit, 16-bit, or 32-bit. Choose Dec for standard integers, Bin for binary strings, or Hex for hex values. Switching modes converts existing values automatically.
- 3Enter values: Type in the input fields, or click individual bits in the interactive bit panels below each field to toggle bits directly. A live hint line shows the value in all other bases.
- 4Use a quick example: Click any preset (e.g. "Extract low byte" or "Check odd/even") to instantly load a real-world example, great for learning a new technique.
- 5Press Enter or click Calculate: The colour-coded bit grid appears showing A, the operator, B, and the result row. Bit 0 (LSB) is on the right; bit n−1 (MSB) is on the left.
- 6Read all output formats: The results table shows signed decimal, unsigned decimal, binary, hexadecimal, and octal simultaneously. Click any Copy button to copy that format to the clipboard.
Example Calculation
Example 1: Bit masking, extract the low byte of 305
A = 305 (decimal) = 0x131 = 100110001₂ (needs 16-bit)
B = 255 (decimal) = 0x0FF = 011111111₂ (the mask)
A: 0000 0001 0011 0001
B: 0000 0000 1111 1111
─────────────────────
A & B: 0000 0000 0011 0001 = 49 (0x31)
Why this works
The mask 0xFF (binary 11111111) has 1s only in the 8 lowest bit positions. AND-ing any value with it preserves only those 8 bits and forces all higher bits to 0, extracting exactly one byte. This pattern is used in every language runtime, network stack, and image processing library on the planet.
Example 2: XOR toggle, flip bit 3 of 0b10100 (20)
A = 20 = 00010100₂
B = 8 = 00001000₂ (bit 3 mask: 1 < < 3)
A: 0001 0100 = 20
B: 0000 1000 = 8
─────────────
A ^ B: 0001 1100 = 28
Example 3: Left shift, multiply 3 by 4 using bit shifts
A = 3 = 00000011₂
Left shift by 2 (= × 2² = × 4)
00000011 < < 2
= 00001100 (bits moved left, vacated positions filled with 0)
= 12 ✓ (3 × 4 = 12)
Understanding Bitwise | AND, OR, XOR
Why Bitwise Operations Are Everywhere in Software
Every processor in existence, from a microcontroller in a toothbrush to a 64-core server CPU, executes bitwise operations as its most primitive instructions. They operate in a single clock cycle, require no division or multiplication hardware, and work directly on the binary representation of data. Understanding them unlocks a huge class of fast, elegant algorithms that would otherwise require loops or conditionals.
Modern software uses bitwise operations constantly, often hidden behind abstractions:
- ›Network programming, IP address masking, subnet calculations, port flags
- ›Graphics, extracting R, G, B, A channels from a packed 32-bit pixel value
- ›Cryptography, stream ciphers, hash functions, AES S-box operations
- ›Compression, Huffman encoding, LZ77 back-references, bit packing
- ›Embedded systems, GPIO register control, SPI/I²C protocol framing
- ›Databases, bitmap indexes for fast multi-column filtering
- ›Game development, entity component system flags, tile map attributes
AND, Masking and Extracting Bits
AND is the most-used bitwise operation in practical programming. Its defining characteristic is that any bit AND 0 = 0, so you can use a mask value to selectively zero out bits you don't care about, leaving only the bits that are 1 in both A and the mask.
- ›
n & 1, check if n is odd. Returns 1 if the LSB is set (odd), 0 otherwise. - ›
n & 0xFF, extract the lowest byte of any integer. - ›
n & (n-1), clear the lowest set bit. Result is 0 if and only if n is a power of 2. - ›
ip & subnet_mask, compute the network address of an IP in subnetting.
OR, Setting Bits and Combining Flags
OR is used to set specific bits to 1 without affecting others. Any bit OR 1 = 1; any bit OR 0 = unchanged. This makes it perfect for combining feature flags.
- ›
flags | READ_FLAG, grant read permission without changing other permission bits. - ›
pixel | 0xFF000000, force the alpha channel of a 32-bit ARGB pixel to fully opaque. - ›
a | b, merge two bit sets (union in set-theoretic terms).
XOR, Toggling and Detecting Differences
XOR is the "parity" operator, it produces 1 when its inputs differ and 0 when they match. This makes it uniquely useful for:
- ›Toggling specific bits:
n ^ (1 < < k)flips bit k. - ›Swapping two variables without a temporary:
a ^= b; b ^= a; a ^= b; - ›Finding the one unique element in an array of pairs, XOR all values and pairs cancel out.
- ›Simple data scrambling and one-time pad encryption (XOR with a key).
- ›Parity checking in data transmission, XOR all bytes; the result is 0 if transmission was clean.
NOT, Inverting Bits and Creating Masks
NOT flips every single bit. Combined with AND it becomes a "clear bit" operation: to clear bit k, AND with ~(1 < < k). The NOT of the mask has 0 only at bit position k, which ANDs that bit to zero while leaving all others unchanged.
Note: the result of NOT depends on the bit width. NOT 0x0F in 8-bit gives 0xF0, but in 32-bit gives 0xFFFFFFF0. This calculator masks the result to your chosen width so you always see the correct representation.
Bit Shifts, The Fastest Multiply and Divide
Left shifting by n positions multiplies by 2ⁿ. Right shifting divides by 2ⁿ (truncating, not rounding). Compilers routinely replace multiplications and divisions by powers of 2 with shifts because a shift executes in a single clock cycle while multiply takes several.
| Expression | Equivalent to | Example (A = 12) |
|---|---|---|
| A << 1 | A × 2 | 12 << 1 = 24 |
| A << 2 | A × 4 | 12 << 2 = 48 |
| A << 3 | A × 8 | 12 << 3 = 96 |
| A >> 1 | A ÷ 2 | 12 >> 1 = 6 |
| A >> 2 | A ÷ 4 | 12 >> 2 = 3 |
| A >> 3 | A ÷ 8 | 12 >> 3 = 1 (truncated) |
Arithmetic vs logical right shift
A logical right shift (≫) always fills vacated bits with 0 regardless of the sign. An arithmetic right shift copies the sign bit (MSB) into vacated positions, preserving the sign of negative two's complement numbers. This calculator uses logical (unsigned) right shift. In C/C++, the behaviour of >> on signed integers is implementation-defined, use (unsigned)n > > k for a guaranteed logical shift.
Frequently Asked Questions
What is a bitwise operation?
A bitwise operation works on the binary (bit-level) representation of an integer, one bit at a time.
- ›AND, OR, XOR: apply a Boolean logic rule to each pair of bits at the same position independently
- ›NOT: flip every single bit, 0 becomes 1, 1 becomes 0
- ›Left/right shifts: move all bits together by n positions, filling vacated positions with 0
The result of each bit position is completely independent of all other positions. An 8-bit AND runs the same rule simultaneously on all 8 pairs of bits.
What is bitwise AND used for in programming?
AND is the primary tool for bit masking, reading or clearing specific bits while leaving others unchanged.
- ›
n & 1, check odd (result 1) or even (result 0) - ›
n & 0xFF, extract the lowest 8 bits (one byte) - ›
n & (n-1), clear the lowest set bit; result is 0 iff n is a power of 2 - ›
flags & ~READ_FLAG, clear a specific flag bit - ›
ip & subnet_mask, compute the network address (e.g. 192.168.1.45 & 255.255.255.0 = 192.168.1.0)
What does XOR do and why is it special?
XOR stands out because it is its own inverse: applying XOR twice returns the original value.
Key properties:
- ›n ^ n = 0 (anything XOR itself = 0)
- ›n ^ 0 = n (anything XOR 0 = unchanged)
- ›(a ^ b) ^ b = a (self-inverse / reversible)
Practical uses:
- ›Toggle a specific bit: n ^ (1 << k)
- ›Swap two variables: a ^= b; b ^= a; a ^= b (no temp variable)
- ›Find the unpaired element in an array where everything else appears twice
- ›Parity checks in data transmission
- ›One-time-pad encryption: encrypt by XOR with key, decrypt by XOR again
What is bit masking and how do I use it?
A bitmask is a value where specific bits are 1, used to selectively manipulate individual bits:
- ›Read bit k:
(n > > k) & 1, returns 0 or 1 - ›Set bit k to 1:
n | (1 < < k) - ›Clear bit k to 0:
n & ~(1 < < k) - ›Toggle bit k:
n ^ (1 < < k)
Real-world examples:
- ›Linux file permissions (rwxr-xr-x) stored in a single 9-bit integer
- ›GPIO register bits in embedded systems (each bit controls one physical pin)
- ›IP addresses and subnet masks, masking gives the network address
- ›CSS border styles and HTML character attributes packed as flags
What is the difference between left shift and right shift?
Left shift (<<):
- ›Moves all bits left by n positions
- ›Vacated right positions are filled with 0
- ›Equivalent to multiplying by 2ⁿ (as long as bits don't overflow)
- ›Example: 3 << 2 = 12 (= 3 × 4)
Right shift (>>):
- ›Moves all bits right by n positions
- ›Logical right shift (this calculator): fills left with 0, correct for unsigned values
- ›Arithmetic right shift: fills left with sign bit, preserves sign of negative numbers
- ›Equivalent to dividing by 2ⁿ (floor/truncation, not rounding)
- ›Example: 12 >> 2 = 3 (= 12 ÷ 4)
What happens with NOT in different bit widths?
NOT flips every bit, the result depends on which width you are working in:
- ›~12 in 8-bit: 11110011₂ = 243 unsigned / −13 signed
- ›~12 in 16-bit: 1111111111110011₂ = 65523 unsigned / −13 signed
- ›~12 in 32-bit: 4294967283 unsigned / −13 signed
The signed decimal (−13) is the same in all widths because two's complement negative numbers are width-invariant by design. The unsigned representation grows with each additional width. This calculator automatically clips the result to the selected width.
Can I use this calculator for subnet masks and IP addresses?
Yes, an IPv4 address and subnet mask are each 32-bit integers, and the network address is their bitwise AND:
- ›Set bit width to 32-bit, operation to AND
- ›Enter the IP address in decimal (e.g. 192.168.1.45 = 3232235821)
- ›Enter the subnet mask in decimal (e.g. /24 = 255.255.255.0 = 4294967040)
- ›Result = network address (e.g. 192.168.1.0 = 3232235776)
For the full subnetting breakdown with host ranges, broadcast address, and CIDR notation, use the IP Subnet Calculator.
Does the calculator save my inputs?
Yes, all inputs are automatically persisted to your browser's localStorage:
- ›Values A and B (in whatever input mode you were using)
- ›Selected operation, input mode, and bit width
- ›Shift amount for shift operations
- ›All data stays local, nothing is sent to any server
Click Reset All to clear all fields and delete the saved data.