Bitwise Operators in Embedded and Digital Systems
Bitwise operators manipulate individual bits inside an integer. They are fundamental in embedded firmware, device drivers, register configuration, communication protocols, file formats, graphics, compression, and cryptographic primitives. Unlike arithmetic operators that treat a number mainly as a quantity, bitwise operators treat the same stored value as a set of binary flags or fields. That viewpoint is essential when a microcontroller register contains one bit for enable, another bit for interrupt status, and several bits for a mode selection.
AND keeps a bit set only if it is set in both inputs. OR sets a bit if it is set in either input. XOR sets a bit if the inputs differ. NOT flips every bit, which is why fixed word width matters. A NOT operation on an 8-bit value is different from NOT on a 32-bit value because the number of bits being inverted is different. This calculator applies a selected word-width mask so results match the kind of constrained integer used in hardware.
Shifts move bits left or right. A left shift by one usually multiplies an unsigned integer by two if no bit is shifted out of the selected width. An unsigned right shift by one divides by two and discards the least significant bit. Shifts are used for field extraction, scaling powers of two, packing bytes, CRCs, and efficient low-level algorithms. They are also a common source of bugs when signedness, overflow, or language-specific rules are misunderstood.
Manual Calculation Steps
Take A = 170 and B = 85 in an 8-bit word. A is 10101010 and B is 01010101. A AND B is 00000000 because no bit position has a 1 in both inputs. A OR B is 11111111 because every position has a 1 in one of the inputs. A XOR B is also 11111111 because every bit differs. NOT A is 01010101 when limited to 8 bits. If A is shifted left by one, the raw pattern becomes 101010100, but the 8-bit mask keeps 01010100.
Masks and Registers
A mask is a value designed to select or modify specific bits. To test whether bit 3 is set, AND the value with 1 shifted left by 3. To set a bit, OR with the mask. To toggle a bit, XOR with the mask. To clear a bit, AND with the inverse of the mask. These patterns are used constantly in peripheral drivers. For example, enabling a UART transmitter might mean setting one bit in a control register without disturbing other fields in that same register.
Multi-bit fields use masks plus shifts. A register might store a three-bit prescaler in bits 4 through 6. To read it, shift the register right by 4 and mask with binary 111. To write it, clear the target field with an inverted mask, shift the new value into position, then OR it into the register. This style prevents unrelated control bits from changing accidentally.
Engineering Cautions
Word width, signedness, and language rules matter. JavaScript bitwise operators operate on 32-bit integers. C and C++ have additional rules for integer promotion and undefined or implementation-defined behavior in some shift cases. Hardware registers may be 8, 16, 32, or 64 bits wide, and some bits may be reserved or write-one-to clear. A correct bit expression on paper must still follow the target platform's rules.
Bitwise operations are powerful because they expose exactly what happens at the binary level. The safest workflow is to write masks clearly, document field positions, use named constants, and test edge cases at the selected word width.
Endianness is related but separate. Bitwise operators work on the numeric value after bytes have already been interpreted by the program. Network packets, binary files, and memory-mapped peripherals may store bytes in a particular order, but AND, OR, XOR, and shifts operate on the value in the CPU register. When debugging a protocol, first decode the byte order correctly, then apply masks and shifts to the resulting integer fields.
Bit numbering conventions also vary. Some data sheets call the least significant bit bit 0, while diagrams may draw the most significant bit on the left. Communication standards sometimes transmit least significant bit first even when documentation lists fields from most significant to least significant. Clear diagrams and tests with known values help prevent off-by-one bit mistakes.
When reviewing firmware, include at least one test vector where only a single bit is set and another where every bit is set. Those extremes quickly reveal masks that are shifted by one position or operations that forgot to constrain the result back to the target word size.