An all-in-one bitwise calculator
This bitwise calculator applies logic operations to integers one bit at a time. Choose AND, OR, XOR, NAND, NOR, XNOR, NOT, shift left or shift right, pick a bit width, and get the answer in several bases, with the operands aligned in binary so you can follow every bit.
Operations at a glance
| Operation | Result bit is 1 when… | In C-style code |
|---|---|---|
| AND | all inputs are 1 | a & b |
| OR | any input is 1 | a | b |
| XOR | the inputs differ | a ^ b |
| NAND | not all inputs are 1 | ~(a & b) |
| NOR | all inputs are 0 | ~(a | b) |
| XNOR | the inputs are equal | ~(a ^ b) |
| NOT | the input is 0 | ~a |
Example with hex masks
The two values 0xF0F0 and 0x0FF0 give a good feel for the three basic operations:
0xF0F0 AND 0x0FF0 = 0x00F0 (240)
0xF0F0 OR 0x0FF0 = 0xFFF0 (65520)
0xF0F0 XOR 0x0FF0 = 0xFF00 (65280)
AND keeps the overlap, OR merges both, and XOR keeps only the bits that are in one value but not the other.
Bit shifts
For shifts, Operand A is the value and the second field, relabelled "Shift by (bits)", is how many positions to move it.
- Shift left (<<) moves bits toward the high end and fills with zeros. Each step multiplies by 2 while nothing overflows: 5 << 2 = 20. Bits pushed past the chosen width are dropped, so 0xF0F0 << 4 is 0xF0F00 in 32-bit mode but 0x0F00 in 16-bit mode.
- Shift right (>>) moves bits toward the low end, discarding the lowest ones, which divides by 2 per step and rounds down: 100 >> 3 = 12, and 0xF0F0 >> 4 = 0x0F0F.
Right shifts here are logical: zeros always come in from the top. In 8 bits, -1 is stored as 255, and 255 >> 1 gives 127. An arithmetic shift, like Java's or C#'s >> on a signed int, would copy the sign bit and keep −1 instead. JavaScript's >>> is the logical form.
Bit width and signed results
Every input is reduced to the chosen width (8, 16, 32 or 64 bits), and negative numbers are stored in two's complement. When the result's top bit is set, a signed line is added: 1 << 7 in 8 bits is 128 unsigned, but −128 signed.
Input details
- Each operand has its own input base for plain values; a 0x, 0b or 0o prefix overrides it for that value.
- AND, OR, XOR and their inverted forms take two operands; NOT inverts Operand A alone. For a longer chain, type the result back into Operand A.
- Only whole numbers are allowed; a fraction produces an error.
A precedence trap in code
In C, C++ and JavaScript, equality comparison binds more tightly than the bitwise operators, so x & 1 == 0 means x & (1 == 0). Always write (x & 1) == 0. Checking the expected value here first makes such bugs easier to spot.
Single-operation pages such as the AND calculator and XOR calculator explain each operation in more depth. To convert a result into another base, use the all number converter.