Bitmask Calculator
Use the free Bitmask Calculator to generate an exact binary mask from a selected bit width, starting bit position, and number of enabled bits. Build masks for programming, flags, registers, packed data, networking, and low-level binary operations, with instant binary, hexadecimal, and decimal results.
What Is a Bitmask?
A bitmask is a binary pattern used to select, test, enable, disable, isolate, or modify particular bit positions inside another binary value. Each position in the mask contains either 0 or 1.
The meaning of those mask bits depends on the operation being performed. With AND, a 1 can preserve a selected bit. With OR, a 1 can turn a selected bit on. With XOR, a 1 can toggle a selected bit.
How This Bitmask Calculator Works
For example, select 8 bits, 16 bits, 32 bits, or another width up to 128.
Bit numbering begins at 0 on the right. Therefore bit 0 is the least-significant bit.
The calculator enables that many positions beginning at the chosen start bit.
Selected positions become 1 while every other position remains 0.
BinaryCon also displays the equivalent hexadecimal and decimal value.
Worked Example: 8-Bit Mask Starting at Bit 2
Suppose the mask uses 8 bits, starts at bit 2, and contains three consecutive 1 bits.
Start Bit = 2
Number of 1 Bits = 3 Selected positions: bit 2
bit 3
bit 4 Final mask: 00011100
The rightmost position is bit 0, so the three selected positions appear in the middle of the eight-bit pattern.
Bit Position Numbering
Bit positions are normally counted from right to left beginning with zero.
| Position | Power of 2 | Decimal Value |
|---|---|---|
| Bit 0 | 2⁰ | 1 |
| Bit 1 | 2¹ | 2 |
| Bit 2 | 2² | 4 |
| Bit 3 | 2³ | 8 |
| Bit 4 | 2⁴ | 16 |
| Bit 5 | 2⁵ | 32 |
| Bit 6 | 2⁶ | 64 |
| Bit 7 | 2⁷ | 128 |
Common Bitmask Examples
| Purpose | 8-Bit Mask | Hex |
|---|---|---|
| Lowest bit | 00000001 |
0x01 |
| Lowest 4 bits | 00001111 |
0x0F |
| Highest 4 bits | 11110000 |
0xF0 |
| Middle 3 bits, start 2 | 00011100 |
0x1C |
| All 8 bits | 11111111 |
0xFF |
Using a Bitmask with Binary AND
Bitwise AND can isolate selected bits. Positions containing 1 in the mask are allowed to remain, while positions containing 0 are cleared.
Only the lower four bits remain in the result.
Using a Bitmask with Binary OR
OR is commonly used when selected bits need to be turned on without clearing existing 1 bits.
Using a Bitmask with XOR
XOR can toggle selected positions. Mask bits containing 1 flip the corresponding value bit, while zeros leave it unchanged.
Why Hexadecimal Is Common for Bitmasks
Hexadecimal is convenient because each hex digit represents exactly four binary bits. This makes long masks significantly easier to read.
For this reason, programming documentation frequently presents masks in hex
notation such as 0xFF, 0x0F, or 0xF000.
8-Bit, 16-Bit, 32-Bit and 64-Bit Masks
8-bit
Useful for bytes, small registers, flags, channels, and compact control values.
16-bit
Common in embedded systems, networking fields, hardware registers, and packed values.
32-bit
Widely used for integer flags, IP addresses, color values, permissions, and bit fields.
64-bit
Useful when applications require larger packed fields or more independent Boolean flags.
Bitmask Formula
A contiguous mask containing n ones beginning at bit position s can be expressed mathematically as:
The first part, 2ⁿ - 1, creates the required number of low-order
ones. Multiplying by 2ˢ moves that group to the selected starting position.
What Is a Field Mask?
A field mask identifies a group of adjacent bits inside a larger value. Software and hardware formats frequently pack several independent values into different sections of one integer.
After applying AND, a right shift can move the extracted field toward bit 0 for easier interpretation.
Bitmask vs Binary Number
Technically, a bitmask is simply a binary number. What makes it a mask is how that number is used.
Binary number
Usually represents an ordinary numerical value.
Bitmask
Uses individual bit positions as selectors, flags, permissions, or field markers.
Where Are Bitmasks Used?
Programming Flags
Store many Boolean settings inside a single integer and test them independently.
Permissions
Represent read, write, execute, feature, or access states using individual bits.
Embedded Systems
Read or modify selected fields inside hardware registers.
Networking
Subnet masks, protocol headers, flags, and packed address fields rely heavily on masks.
Graphics
Packed colors and pixel formats use masks to isolate red, green, blue, or alpha fields.
File Formats
Binary structures often place status flags and small numeric fields within specific bit ranges.
Common Bitmask Mistakes
Most programming conventions begin with bit 0 at the rightmost position.
Higher bit numbers appear toward the left; bit 0 remains on the far right.
The start bit plus the number of selected bits cannot exceed the total width.
The mask defines which positions should be affected. The actual data is a separate value.
AND isolates or clears, OR sets, XOR toggles, and NOT inverts. The correct operation depends on what you want to do with the mask.
Related BinaryCon Tools
Bitmask Calculator FAQs
What is a bitmask?
Which position is bit 0?
What is the mask for the lowest 4 bits of an 8-bit value?
00001111, which is hexadecimal 0x0F
and decimal 15.
What is the mask for the highest 4 bits of an 8-bit value?
11110000, hexadecimal 0xF0, or decimal 240.
What mask selects bits 2 through 4?
00011100.
Why do programmers write masks in hexadecimal?
How do I use a mask with AND?
How do I use a mask with OR?
How do I toggle bits with a mask?
What does NOT do to a bitmask?
What is the formula for a contiguous bitmask?
(2ⁿ - 1) × 2ˢ.
What happens if the mask exceeds the bit width?
Can I create a 32-bit or 64-bit mask?
Can this calculator generate masks larger than 64 bits?
What is a field mask?
Is a bitmask just a binary number?
Generate Exact Bitmasks Instantly
Build binary masks for fields, flags, permissions, registers, programming, networking, and packed data with exact bit positions plus binary, hexadecimal, and decimal output.