Bitwise Operations
Bitwise operations work on each bit independently. AND, OR, XOR compare corresponding bits; NOT inverts all. Shifts move bits — left multiplies by 2ⁿ, right divides.
Why This Mathematical Concept Matters
Why: Bitwise ops map to single CPU instructions. IP subnet masks use AND; XOR is reversible for encryption.
How: Each operation applies to corresponding bit positions. Set: value | (1<<n). Clear: value & ~(1<<n). Toggle: value ^ (1<<n).
- ●IP subnet masks use AND: 192.168.1.5 & 255.255.255.0 = 192.168.1.0.
- ●XOR is reversible: (data ^ key) ^ key = data — used in stream ciphers.
- ●NAND and NOR are universal gates — any boolean function from them alone.
Bitwise Calculator — All Logic Operations
AND, OR, XOR, NOT, NAND, NOR, XNOR, and bit shifts. Binary, decimal, hex input.
🔢 Sample Examples — Click to Load
Input Format
Operation
Operands
⚠️For educational and informational purposes only. Verify with a qualified professional.
🧮 Fascinating Math Facts
IP subnet masks use AND to identify network vs host portions.
— RFC 791
XOR is reversible: (data ^ key) ^ key = data — used in encryption.
— Cryptography
📋 Key Takeaways
- • AND (&) — Returns 1 only when both bits are 1. Use for masking and bit extraction.
- • OR (|) — Returns 1 if either bit is 1. Use for combining flags and setting bits.
- • XOR (^) — Returns 1 when bits differ. Use for toggling and parity checks.
- • NOT (~) — Inverts all bits. Use for complement and bit flipping.
- • NAND — NOT of AND. Universal gate; can build any logic circuit.
- • NOR — NOT of OR. Universal gate; used in memory cells.
- • XNOR — NOT of XOR. Returns 1 when bits are equal; equality check.
- • Left shift (<<) multiplies by 2^n; right shift (>>) divides by 2^n.
💡 Did You Know?
📖 How Bitwise Operations Work
Bitwise operations work on each bit independently. AND, OR, and XOR compare corresponding bits of two numbers; NOT inverts all bits of a single number. Shifts move bits left or right — left shift fills with zeros; logical right shift fills with zeros; arithmetic right shift preserves the sign bit for signed integers.
Bit-by-Bit Processing
Each operation applies to corresponding bit positions. Example: 1010 & 1100 compares bit 0 with bit 0, bit 1 with bit 1, etc. Result: 1000 (only position 3 has both 1s).
🎯 Expert Tips
Set Bit
value |= (1 << n)
Clear Bit
value &= ~(1 << n)
Toggle Bit
value ^= (1 << n)
Check Bit
(value & (1 << n)) !== 0
⚖️ Comparison Table — All Operations
| A | B | AND | OR | XOR | NAND | NOR | XNOR |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
❓ Frequently Asked Questions
Why are bitwise ops faster than arithmetic?
They work at the hardware level in a single cycle. x << 1 is often faster than x * 2. No carry propagation for AND/OR/XOR.
Logical vs arithmetic right shift?
Logical (>>>) fills with 0s. Arithmetic (>>) preserves the sign bit for signed integers — negative numbers stay negative.
What is a bitmask?
A pattern of bits to extract or manipulate specific bits. Example: (1 << 4) - 1 gives 0b1111 for lower 4 bits. Use AND to apply.
When to use bitwise operations?
Flags, networking (subnet masks), graphics (color packing), crypto (XOR cipher), embedded systems, performance-critical code.
What is NAND and why is it universal?
NAND = NOT(AND). You can build NOT, AND, OR, XOR from NAND alone. Same for NOR. Used in flash memory and logic design.
How does XNOR differ from XOR?
XNOR returns 1 when bits are equal (0,0 or 1,1). XOR returns 1 when bits differ. XNOR is useful for equality checks.
Can I use bitwise ops on floating-point?
In JavaScript, bitwise ops convert to 32-bit signed integers first. For floats, use typed arrays (Float32Array) and view as Int32.
What is the difference between & and &&?
& is bitwise AND (operates on each bit). && is logical AND (short-circuit, returns boolean). Use & for bit manipulation, && for conditions.
How do shifts handle overflow?
Left shift can overflow; JavaScript uses 32-bit for bitwise, so bits beyond 32 are lost. Right shift by ≥32 gives 0 or -1 (arithmetic).
📊 Infographic Stats
📚 Official Sources
⚠️ Disclaimer: Results assume fixed bit-width. JavaScript uses 32-bit signed integers for bitwise ops internally. For production use, verify behavior in your target language and platform. Not all operations may be available in every programming language.