Bit Mask Calculator
Apply AND, OR, XOR, set, clear, and toggle bit-mask operations. This educational calculator shows the formula, result, and step-by-step interpretation.
Calculator
What this calculator teaches
Bit masks are used to store flags, permissions, packed data, and low-level state.
Use the result as a learning aid. For classwork, still show the formula and intermediate reasoning so the final answer is not just a black-box number.
Reading Bit Masks as Position Selectors
A bit mask is a binary pattern used to inspect or change selected bit positions in an integer. A 1 in the mask marks a position affected by the operation; a 0 usually leaves that position unchanged or excludes it. AND keeps a bit only when both value and mask contain 1. OR sets every masked bit to 1. XOR toggles every masked bit. Clearing uses value AND NOT mask.
For example, decimal 13 is binary 1101 and decimal 10 is 1010. Their AND result is 1000₂ = 8 because only the highest shown bit is 1 in both values. Their OR result is 1111₂ = 15, while XOR gives 0111₂ = 7. These operations are widely used for flags, permissions, packed fields, hardware registers, and low-level protocols.
| Operation | Expression | Typical purpose |
|---|---|---|
| Test/keep | x & mask | Keep selected 1-bits |
| Set | x | mask | Force selected bits to 1 |
| Toggle | x ^ mask | Flip selected bits |
| Clear | x & ~mask | Force selected bits to 0 |
JavaScript bitwise operators work on signed 32-bit integer representations. That implementation detail matters for large values and for how the highest bit affects the displayed signed decimal result.
To build a mask from bit positions, remember that zero-based bit k has decimal value 2k. Bit 0 is 1, bit 1 is 2, bit 2 is 4, bit 3 is 8, and so on. Combining positions uses OR: a mask for bits 0 and 3 is 0001₂ | 1000₂ = 1001₂ = 9.
Testing a flag commonly uses (value & mask) !== 0. Clearing a flag uses AND with the complemented mask, while toggling with XOR is reversible. These patterns let one integer store many independent yes/no settings compactly, but the chosen bit numbering and integer width should be documented so different systems interpret the mask consistently.
Formula & Symbols
| Concept | Formula or rule |
|---|---|
| AND | x & mask |
| OR | x | mask |
| XOR | x ^ mask |
Worked example
Common mistakes
Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.
Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.
OR makes a selected bit 1 even when it was already 1. XOR flips it, so applying the same XOR mask twice restores the original value.
Bitwise NOT flips every bit in the implementation's fixed-width representation. Think in the intended width rather than treating ~mask as an infinite positive binary number.
FAQ
Related calculators
These links will work after the calculators are registered in the final Math layout update.
Formula Explorer connections
Interpretation: This relationship quantifies information, representation, storage, error, search or computational performance. Assumption: Use the exact encoding, data distribution, machine representation and algorithm assumptions. Real systems also include implementation and hardware overhead.