Hex Bit Mask Calculator
Build a hex bitmask covering a specific bit range — pick the start and end bit, and get the mask you'd AND or OR against a value.

Built by
Swapnil SanghviFull-Stack Web Developer & WordPress Developer
Swapnil Sanghvi is a full-stack web and WordPress developer, UI designer, and full-time freelancer who builds and maintains Hex Calculator.
Further reading: Bitwise operation — Wikipedia
How to Build a Bit-Range Mask
Pick the lowest and highest bit index you want included — every bit in that range (inclusive on both ends) is set to 1 in the result, and every bit outside the range stays 0. This is exactly the mask you'd use to isolate, set, or clear that specific field.
Bit Mask Example, Step by Step
Bits 4-7 mask (8-bit) = 0xF0
mask covering bits 4 through 7 = 0xF0
bit index: 7 6 5 4 3 2 1 0 mask bit: 1 1 1 1 0 0 0 0 = 1111 0000 = 0xF0
| Step | Description | Result |
|---|---|---|
| Set bits 4, 5, 6, and 7 to 1 | the requested range, inclusive | 11110000 |
| Leave bits 0-3 at 0 | outside the requested range | unchanged |
| Convert to hex | 11110000 | 0xF0 |
0xF0 selects exactly the high nibble — this is the same mask you'd AND with a byte to isolate its top 4 bits.
Where Hex Bit Masks Actually Come Up
Isolating a Multi-Bit Field in a Packed Register
Many hardware registers pack several fields into one byte or word — building the exact mask for a field's bit range, then ANDing it with the register, extracts just that field.
(register & fieldMask) >> fieldStart
Clearing a Field Before Writing a New Value
Combining a mask's inverse with AND clears out an old field's bits so a new value can be ORed in cleanly, without leftover bits from the previous value corrupting the result.
register = (register & ~fieldMask) | newValue
Documenting a Register Layout
Working out the exact hex mask for each named field in a hardware datasheet's register diagram, to use consistently throughout driver or firmware code.
STATUS_ERROR_MASK covers bits 8-11
Why Use This Calculator Instead of Doing It by Hand
- Handles the bit-range math for you, avoiding off-by-one mistakes at the range edges
- Supports 8/16/32/64-bit widths
- Runs entirely in your browser — nothing you type gets sent anywhere
- Works for single-bit masks and full-width masks alike, not just typical nibble/byte ranges
Limitations
- Only builds a single contiguous bit range — a mask covering several separate, non-adjacent bit ranges needs combining multiple masks with OR yourself.
- Bit positions must be within the selected bit width (0 to width-1).
Frequently Asked Questions
What is a bitmask?
A value with specific bits set to 1 and everything else 0, used to select, clear, or test those particular bits in another value via AND, OR, or XOR.
How do I build a mask for bits 4 through 7?
Enter 4 as the from-bit and 7 as the to-bit — this calculator sets exactly those 4 bits and leaves everything else 0, giving 0xF0 at 8-bit width.
Are the from and to bits inclusive?
Yes — both the from-bit and to-bit are included in the mask, so a range from 0 to 3 sets 4 bits total (bits 0, 1, 2, and 3).
What do I do with the mask once I have it?
AND it with a value to extract just that bit range, OR it in to set that whole range to 1s, or AND with its inverse (NOT) to clear that range — see the Hex AND/OR/NOT Calculators for the next step.