Bit Calculator: AND, OR, XOR, Shifts & Bit Set/Clear
Calculate bitwise AND, OR, XOR, NOT, and bit shifts for 8, 16, 32, and 64-bit numbers.
How it works
Bitwise operations manipulate numbers at the binary level. They are essential for low-level programming, embedded systems, and masking.
Essential Bit Calculation Techniques
In standard binary, bits are numbered from right to left, starting at 0 (the Least Significant Bit, or LSB). Bit 0 has a value of 2⁰, Bit 1 is 2¹, and so on.
- Set a bit: Use OR (
val | (1 << bit)) - Clear a bit: Use AND with NOT (
val & ~(1 << bit)) - Toggle a bit: Use XOR (
val ^ (1 << bit)) - Test a bit: Use AND (
val & (1 << bit))
Two's Complement
Negative numbers are typically represented using two's complement. To find the negative of a number, invert all bits (NOT) and add 1. Our bit calculation logic automatically handles signed formats according to the chosen data size (8-bit, 16-bit, etc).
Related reading: Decoding peripheral register bitmasks or status flags in disassembled binaries? See our technical guide on Reverse Engineering Firmware with Ghidra and AI in Antigravity.
Worked example
Suppose you have an 8-bit register containing 0b00001010 (10 decimal) and want to set Bit 0 (the rightmost bit) to 1.
- Value = 0000 1010
- Mask = 0000 0001 (which is 1 << 0)
- Result = Value | Mask
- Result = 0000 1011 (11 decimal)
Frequently Asked Questions
Why does shifting right sometimes keep the sign?
In many programming languages, a right shift (>>) on a signed integer is an arithmetic shift, meaning it duplicates the sign bit (the MSB) to preserve the sign of negative numbers. A logical shift (>>> in JS or shifting an unsigned type) always shifts in zeros.
Related Tools
Sources
- Bitwise operations in C/C++ and microcontroller programming.
Results are for engineering assistance. Verify important calculations and check official datasheets. See the Engineering Disclaimer.