Bit masking: set, clear, toggle and test bits
Bit masking uses a pattern of bits, the mask, with 1s in the positions you care about, and combines it with a value using OR, AND or XOR to change or read only those bits: x | mask sets them, x & ~mask clears them, x ^ mask toggles them and x & mask tests them. Enter a value and a bit range below to see each one.
Making the mask
For a single bit n, the mask is 1 << n: a 1 shifted left n places, so bit 3 is
00001000. For a field of k bits starting at bit lo, make k ones with
(1 << k) - 1 and shift them into place:
((1 << k) - 1) << lo, so bits 4 to 7 give 11110000.
Bits are numbered from 0 at the right, the least significant end.
The four operations, and why they work
- Set,
x | mask: OR with 1 always gives 1, and OR with 0 leaves a bit alone, so only the masked bits change, to 1. - Clear,
x & ~mask:~maskhas 0s in the masked positions; AND with 0 gives 0 and AND with 1 keeps a bit. - Toggle,
x ^ mask: XOR with 1 flips a bit, and XOR with 0 keeps it. - Test,
(x & mask) != 0: AND keeps only the masked bits, so the result is non-zero exactly when one of them is 1. To read a field as a number, shift it down and mask it:(x >> lo) & ((1 << k) - 1).
Each of these is one logic gate per bit, working on every column at once. A CPU's AND, OR and XOR instructions are exactly that: try them in the bitwise calculator, or build the gates themselves in the lab, starting from the AND, OR and XOR truth tables.
Worked example
Take x = 10100110 (166) and the low four bits, mask 00001111.
Setting them gives 10101111, clearing them gives 10100000,
toggling them gives 10101001, the test is true (bits 1 and 2 are 1), and extracting
them gives 0110, which is 6. The calculator above opens on this example.
Where bit masks are used
Anywhere several yes-or-no flags or small numbers share one word: option flags passed to a function, file
permissions (in Unix, 0o755 packs read, write and execute bits for owner, group and
others), status and control registers in microcontrollers, where each bit switches one feature, and packed
formats such as a 16-bit color that holds red, green and blue fields. The same operators exist in C, C++, Java,
JavaScript and Python; in JavaScript they work on 32-bit integers.
Frequently asked
What is bit masking?
Bit masking is using a binary pattern, the mask, to select particular bits of a value and then set, clear, toggle or test only those bits with OR, AND or XOR, leaving the other bits unchanged.
How do you set a bit?
OR the value with a mask that has a 1 at that bit: x | (1 << n). For example 10100110 | 00000001 = 10100111.
How do you clear a bit?
AND the value with the inverted mask: x & ~(1 << n). The inverted mask has a 0 only at bit n, so that bit becomes 0 and the rest stay as they were.
How do you check if a bit is set?
AND the value with the mask and compare with zero: (x & (1 << n)) != 0 is true when bit n is 1. Equivalently, (x >> n) & 1 is the bit itself, 0 or 1.
How do you toggle a bit?
XOR the value with the mask: x ^ (1 << n) flips bit n and leaves the others alone. Doing it twice gives the original value back.
Learn the logic behind the operators in boolean algebra, or see them inside an ALU.
Related tools: bitwise calculator (AND, OR, XOR, shifts and rotates), binary converter and number base converter. For the order of bytes in memory, see big-endian vs little-endian.
Open the lab →