ScienceComputer Science

Bitwise
calculator.

AND, OR, XOR, NOT and shifts on whole numbers, bit by bit, at 8 to 64 bits wide.

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.

The rule for one column
A and BANDORXOR
0 and 0000
0 and 1011
1 and 0011
1 and 1110

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.

A≪s\displaystyle A \ll s=A×2s\displaystyle {}= A \times 2^{s}

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 2s2^{s} rounded down.

A≫s\displaystyle A \gg s=⌊A2s⌋\displaystyle {}= \left\lfloor \frac{A}{2^{s}} \right\rfloor

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.

Open this example in the tool

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.

Open this example in the tool

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.

Open this example in the tool

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.

Questions people ask

NOT flips every bit, so 0000 0101 becomes 1111 1010. Read as a signed number in two's complement, that pattern is −6. Read as an unsigned byte, the same bits are 250, so the answer depends on how the value is read.

Sources