Bitwise
calculator.
AND, OR, XOR, NOT and shifts on whole numbers, bit by bit, at 8 to 64 bits wide.
- Hex
- 0x8
- Bits set
- 1
- Bits changed from A
- 1
AND kept only the 1 bits that A and B share: bit 3. Every other bit is 0.
The bits, lined up
Click a bit of A or B to flip it0b1100 AND 0b1010 at 8 bits: A 0000 1100, B 0000 1010, result 0000 1000. The result has one 1 bit, bit 3.
Working
- 1Write A in 8 bits0b1100 = 12 → 0000 1100
- 2Write B in 8 bits0b1010 = 10 → 0000 1010
- 3AND: 1 only where both bits are 1A 0000 1100B 0000 1010= 0000 1000
- 4Read the result0000 1000 = 8
- 5Check with arithmetic: A AND B + A OR B = A + B8 + 14 = 22 and 12 + 10 = 22
Every operation on A and B
| Operation | Result | Hex | Signed |
|---|---|---|---|
| A AND B0x08 | 8signed 8 | 0x08 | 8 |
| A OR B0x0E | 14signed 14 | 0x0E | 14 |
| A XOR B0x06 | 6signed 6 | 0x06 | 6 |
| A NAND B0xF7 | 247signed −9 | 0xF7 | −9 |
| A NOR B0xF1 | 241signed −15 | 0xF1 | −15 |
| A XNOR B0xF9 | 249signed −7 | 0xF9 | −7 |
| NOT A0xF3 | 243signed −13 | 0xF3 | −13 |
Shifts appear when B is 0 to 8.
How it's worked out
A bitwise operation works on each bit position on its own. Write both numbers in binary at the same width, line them up, and apply one rule to each column. Nothing carries from one column to the next, which is what makes these operations different from adding.
| A and B | AND | OR | XOR |
|---|---|---|---|
| 0 and 0 | 0 | 0 | 0 |
| 0 and 1 | 0 | 1 | 1 |
| 1 and 0 | 0 | 1 | 1 |
| 1 and 1 | 1 | 1 | 0 |
AND gives 1 only where both bits are 1. OR gives 1 where either is. XOR, short for exclusive or, gives 1 where the two bits differ. NOT flips every bit of one number, and NAND, NOR and XNOR are NOT applied to AND, OR and XOR.
For 12 and 10, that is 0000 1100 and 0000 1010. AND gives 8, OR gives 14 and XOR gives 6. The working checks each answer with plain arithmetic. For AND it uses a fact that holds for any pair: AND plus OR equals A plus B, here 8 + 14 = 22.
A shift moves every bit along by a number of places. Bits pushed off the end are lost. A left shift fills the right with 0s, so it multiplies by 2 for each place, as long as nothing falls off the top.
Right shifts come in two kinds. A logical shift fills the left with 0s. An arithmetic shift copies the sign bit, so a negative number stays negative, and the Java Language Specification defines it as division by rounded down.
The width matters because a register holds a fixed number of bits. The tool reads each value at 8, 16, 32 or 64 bits, stores negatives in two's complement (the leftmost bit counts as minus its place value), and works with exact whole numbers, so 64-bit masks come out right to the last bit.
Worked examples
Sam keeps the low four bits of a status byte
A sensor sends Sam the status byte 0xB7, which is 1011 0111. Only the low four bits hold the reading he wants. The mask 0x0F is 0000 1111, so AND keeps the bits under the 1s and clears the rest.
The result is 0000 0111, which is 7. To turn on bit 5 instead, he would OR with 0x20 and get 0xB7. To flip bit 3 of 1001 0001, XOR with 1000 gives 1001 1001.
Ines tests whether a buffer size is a power of two
A power of two has exactly one 1 bit. Subtracting 1 turns that bit off and every bit below it on, so the two share no bits. 64 is 0100 0000 and 63 is 0011 1111, and 64 AND 63 is 0.
For 96 the test fails: 96 AND 95 is 64, because 96 has two 1 bits, at positions 6 and 5. Watch out for 0, which also gives 0 and is not a power of two, so real code checks that the number is above 0 first.
Leo shifts a negative number right
In 8 bits, −16 is 1111 0000. An arithmetic shift right by 2 copies the sign bit in from the left and gives 1111 1100, which is −4: −16 divided by 4.
A logical shift fills with 0s instead and gives 0011 1100, which is 60. The same shift at 32 bits gives 1,073,741,820, so the width you pick changes the answer. Leo also notes that −15 >> 2 is −4, not −3, since the division rounds down.
Reading your result
The big number is the result read as unsigned, every bit counting as a positive place value. The line under it gives the same bits in hex and read as signed two's complement, which is how a byte, short, int or long in most languages would print it.
The bit grid lines up A, B and the result by position, highest bit first, in blocks of one byte, with each digit written out. Click a bit of A or B to flip it; the box keeps the base you typed it in, so 0xB7 stays in hex.
Signed and unsigned are two readings of the same bits. NOT 0 is 255 at 8 bits and 4,294,967,295 at 32 bits when read as unsigned, and −1 at both widths when read as signed.
The table of every operation shows what AND, OR, XOR, the NOT forms and the three shifts give for the same pair. A shift row appears only when B is a shift amount from 0 to the width.
Common mistakes
- Expecting NOT to give a small positive number. ~5 flips every bit, so at 32 bits it is −6 as a signed int, not 2 or 10.
- Forgetting bits fall off the top. In 8 bits, 0x81 << 1 is 0x02, because the top 1 is pushed out.
- Using a logical shift on a signed value. A logical right shift on a negative number gives a large positive one. Use the arithmetic shift when the value is signed.
- Leaving out brackets around a mask. In Java the operands of & come from the equality level of the grammar, so x & 1 == 0 is read as x & (1 == 0). Write (x & 1) == 0.
- Mixing up AND and the logical &&. && treats each whole number as true or false; & works on every bit.