What Is a Bit Mask?
A bit mask is an integer whose binary digits determine which bits participate in an operation. A mask containing a 1 selects that bit; a mask containing a 0 can preserve, clear, or ignore that bit, depending on the operation being performed.
Bit masks are widely used for configuration flags, permissions, hardware registers, communication protocols, packed values, and low-level programming. Instead of storing each yes-or-no setting separately, a program can store several flags in one integer.
Bitwise AND: Select or Test Bits
AND produces 1 only when both corresponding input bits are 1.
It is useful for keeping selected bits and clearing all others.
For example, the mask 00001111 keeps the low four bits.
Mask: 00001111 (15)
A AND Mask: 00001010 (10)
Bitwise OR: Set Bits
OR produces 1 if either input bit is 1. OR with a mask sets all positions containing a 1 in the mask while leaving the other positions unchanged.
Mask: 00000001
A OR Mask: 10000001
Bitwise XOR: Toggle Bits
XOR produces 1 when the input bits differ. XOR with a mask toggles the selected bits: a selected 0 becomes 1, and a selected 1 becomes 0. Applying the same XOR mask twice restores the original value.
Bitwise NOT: Invert Bits
NOT flips every bit within the chosen word size. Width matters:
in 8 bits, NOT of 00001111 is
11110000. The calculator therefore masks the result
to the selected 8-, 16-, 32- or 64-bit width.
Set, Clear, Toggle and Test a Single Bit
Bit positions are numbered from zero at the least significant bit on the right. In an 8-bit value, the rightmost bit is position 0 and the leftmost bit is position 7.
- Set: use OR with a mask containing one 1 at the desired position.
- Clear: use AND with the width-limited inverse of that mask.
- Toggle: use XOR with the one-bit mask.
- Test: use AND with the one-bit mask; a nonzero result means the bit is set.
Extract a Bit Field
A bit field is a group of adjacent bits inside a larger value. To extract it, create a mask with the desired number of consecutive ones, shift the mask to the field's starting position, AND it with the input, and shift the selected bits down to position zero.
For example, extracting four bits starting at position zero from
11001010 returns 1010, which is decimal 10.
The calculator reports the extracted value as an integer and displays
its binary, hexadecimal and octal forms.
Bit Mask Examples
| Operation | Inputs | Result | Purpose |
|---|---|---|---|
| AND | 11001010 & 00001111 |
00001010 |
Keep the low nibble |
| OR | 10000000 | 00000001 |
10000001 |
Set a flag |
| XOR | 1010 ^ 0011 |
1001 |
Toggle selected bits |
| NOT (8-bit) | ~00001111 |
11110000 |
Invert all eight bits |
| Set bit 0 | 10000000 |
10000001 |
Turn on the lowest bit |
Why Does Bit Width Matter?
A fixed-width mask defines exactly how many bits are kept. An 8-bit word contains 256 possible bit patterns, from 0 to 255 when interpreted as unsigned. A 16-bit word contains 65,536 patterns. Wider widths allow larger values and make NOT operations behave differently because more bits are inverted.
This calculator uses JavaScript BigInt to avoid ordinary JavaScript number precision problems when handling 64-bit integers. Operations are constrained to the selected width, and both unsigned and two's-complement signed interpretations are displayed.