Free Bitwise Tool

Bit Mask Calculator

Apply binary masks to integers with AND, OR, XOR and NOT. Set, clear, toggle or test individual bits, extract bit fields, and inspect results in binary, decimal and hexadecimal. Choose an 8-, 16-, 32- or 64-bit word size for exact fixed-width calculations.

Bit Mask Calculator

Runs in your browser. No form submission or page refresh is required.

Examples: 255, 0xFF, 0b11111111. Underscores and spaces are allowed.

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.

A:   11001010 (202)
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.

A:   10000000
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.

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.

Frequently Asked Questions

1. What is a bit mask used for?

A bit mask selects, tests, sets, clears or toggles specific bits in a number. Common applications include feature flags, permissions, device registers, and packed binary fields.

2. How do I use AND to extract bits?

Put 1s in the mask positions you want to keep and 0s in the positions you want to clear. AND returns only the bits set in both the input and mask.

3. What is the difference between OR and XOR?

OR sets an output bit when either input bit is 1. XOR sets an output bit when the input bits differ. OR is useful for setting flags; XOR is useful for toggling flags.

4. How are bit positions counted?

Positions start at zero from the rightmost, least significant bit. In an 8-bit value, positions run from 0 to 7.

5. Can I enter hexadecimal or binary numbers?

Yes. In auto-detect mode, use a prefix such as 0xFF for hexadecimal or 0b11111111 for binary. You can also choose an explicit input format.

6. Does the calculator support 64-bit masks?

Yes. It uses BigInt for exact integer bit operations and limits the result to the selected word width.

Related Binary Calculators

Explore more free binary and number-system tools on Binarios.org.