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.
Did our AI summary help? Let us know.
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.
Ready to run the numbers?
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).
Run the calculator when you are ready.
Bitwise Calculator โ All Logic Operations
AND, OR, XOR, NOT, NAND, NOR, XNOR, and bit shifts. Binary, decimal, hex input.
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.
Related Calculators
Bit Shift Calculator
Perform left and right bit shifts on binary, decimal, or hexadecimal values. Includes educational content about how bit shifting works in computing.
MathematicsBinary Operations Calculator
Perform binary arithmetic and logical operations with step-by-step explanations. Supports addition, subtraction, multiplication, division, and bitwise...
MathematicsAND Calculator
Calculate logical AND operations between values in binary, decimal, or hexadecimal format.
MathematicsNOR Calculator
Calculate logical NOR operations between values in binary, decimal, or hexadecimal format.
MathematicsOR Calculator
Calculate logical OR operations between values in binary, decimal, or hexadecimal format.
MathematicsTwo's Complement Calculator
Calculate and visualize two's complement representation for binary, decimal, and hexadecimal numbers.
Mathematics