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.

Computer ScienceBinary Math

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.

OperationExpressionTypical purpose
Test/keepx & maskKeep selected 1-bits
Setx | maskForce selected bits to 1
Togglex ^ maskFlip selected bits
Clearx & ~maskForce 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

ConceptFormula or rule
ANDx & mask
ORx | mask
XORx ^ mask

Worked example

Example: 42 AND 15 keeps only the low four bits.
Example 2: 13 AND 10: 1101₂ & 1010₂ = 1000₂ = 8.
Example 3: 13 OR 10: 1101₂ | 1010₂ = 1111₂ = 15.
Example 4: 13 XOR 10: 1101₂ ^ 1010₂ = 0111₂ = 7.
Example 5: Clearing mask 0100₂ from 1101₂ gives 1001₂ = 9; the selected 4's-place bit is forced to zero.

Common mistakes

⚠️
Using the wrong input format

Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.

⚠️
Ignoring assumptions

Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.

⚠️
Confusing OR with XOR

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.

⚠️
Ignoring integer width

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

Can I use decimals?
Most numerical calculators allow decimals, but modular arithmetic and coding-theory tools usually require integers or binary strings.
Is this for homework checking?
Yes. The page is designed to show both the answer and the reasoning pattern.
Why does the result sometimes say approximate?
Some probability, floating-point, and numerical methods naturally produce approximations.
How do I test whether a particular bit is set?
AND the value with a mask containing 1 only at that position. A nonzero result means that bit was set.
Why does XOR toggle bits?
For a mask bit of 1, XOR maps 0 to 1 and 1 to 0. For a mask bit of 0, the original value bit is unchanged.
What mask sets bit k?
Using zero-based indexing, a one-bit mask is 1 shifted left k positions, written conceptually as 1 << k.
Why can JavaScript bitwise results become negative?
JavaScript converts operands to signed 32-bit integers for bitwise operations. If bit 31 is set, the signed decimal interpretation is negative even though the same bits can be viewed as unsigned.

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.

Bloom Filter False Positive Calculator →Conditional Entropy Calculator →Cross Entropy Calculator →Math Formula Explorer →