A bitwise calculator applies bit-level operations to whole numbers: AND, OR, XOR, NOT, NAND, NOR, XNOR, left shift and right shift. Enter your numbers in decimal, binary or hexadecimal, choose a width of 8, 16, 32 or 64 bits, and press the button. The result is shown in binary, decimal, signed decimal and hexadecimal. The rules follow the Wikipedia pages on bitwise operations, binary numbers and two's complement, and the work is done in your browser. It is also a bit shift calculator and an AND calculator.
How to use the bitwise calculator
- Choose the operation: AND, OR, XOR, NOT, NAND, NOR, XNOR, left shift or right shift.
- Choose the base you will type in and the width in bits.
- Enter the first number, and the second number or the shift amount.
- Press Calculate.
AND, OR and XOR
These operations compare two numbers bit by bit. AND gives 1 only where both bits are 1, OR gives 1 where at least one bit is 1, and XOR gives 1 where the bits differ. The Wikipedia page gives the examples with 4-bit numbers: 0101 AND 0011 is 0001, 0101 OR 0011 is 0111, and 0101 XOR 0011 is 0110 [1]. AND is often used as a mask to test or clear bits: AND with 0001 tests whether a number is odd. XOR flips the bits that are 1 in the second number, so applying it twice returns the original.
NOT
NOT flips every bit. On the page, NOT 0111 is 1000, and in 8 bits NOT 10101011, which is 171, is 01010100, which is 84. For unsigned numbers of the chosen width, NOT x equals the largest value minus x, so 255 minus 171 is 84. For signed numbers in two's complement, NOT x equals −x − 1 [1].
NAND, NOR and XNOR
These are the logic gates that follow AND, OR and XOR with a NOT. NAND negates AND, NOR negates OR, and XNOR is the complement of XOR [4] [5] [6]. For two inputs, NAND gives 1 for every pair except 1 and 1, NOR gives 1 only when both inputs are 0, and XNOR gives 1 when the two bits are equal [4] [5] [6]. The calculator applies the matching operation and then NOT at the chosen width, so the width matters. For the 8-bit numbers F0 and CC in hexadecimal, NAND gives 3F, NOR gives 03 and XNOR gives C3, which is our own working.
Left and right shifts
A left shift moves the bits toward the high end and puts zeros on the right. The page says that a left shift by n equals multiplication by 2 to the power n, as long as nothing overflows. The 8-bit number 00010111, which is 23, shifted left once gives 00101110, which is 46, and shifted left twice gives 01011100, which is 92. A right shift moves the bits down. The arithmetic right shift copies the sign bit into the empty places, so for two's complement numbers it divides by 2 to the power n and rounds down. The page's example is 10010111, which is −105, shifted right once to give 11001011, which is −53. The logical right shift fills with zeros, so the same bits give 01001011, which is 75. The calculator offers both.
Width and two's complement
Computers store whole numbers in a fixed number of bits [2]. In two's complement, the top bit is the sign, and an n-bit number can hold from −2 to the power n − 1 up to 2 to the power n − 1, minus 1. In 8 bits that is −128 to 127 for signed numbers, or 0 to 255 for unsigned ones [3]. The calculator shows the result both ways, so you can see how the same bits read as a signed and an unsigned number. A shift that moves a bit past the left end drops it, which is why 1 shifted left by 31 in 32 bits is 2,147,483,648 as an unsigned number and −2,147,483,648 as a signed one.
Typing numbers
In binary mode, you can type spaces or a 0b prefix. In hexadecimal mode, a 0x prefix is accepted. In decimal mode, a negative number is converted to its two's complement form for the chosen width. A number that does not fit in the width is rejected, so you will not get a silently wrong answer.
A worked example with a mask
Suppose you want the low four bits of the 8-bit number 10110110, which is 182. AND it with the mask 00001111, which is 15, and you get 00000110, which is 6. To set the top bit without changing the others, OR the number with 10000000, which is 128. To flip the lowest bit, XOR with 00000001. These three tricks, masking, setting and toggling, are the everyday uses of the bitwise operations, and you can try each of them in the calculator by choosing the operation and typing the numbers in binary.
Bitwise operations in programming
Programming languages write the operations with symbols: & for AND, | for OR, ^ for XOR, ~ for NOT, and << and >> for shifts. They are used for flags, in which each bit stands for an option that is on or off, for packing several small values into one number, for fast multiplication and division by powers of 2, and in checksums and cryptography. Most languages treat numbers as having a fixed width, such as 32 bits, which is why the calculator lets you choose the width.
Reading the output
The first line is the answer in binary, in groups of four bits, followed by the unsigned decimal, the signed decimal and the hexadecimal. The inputs are echoed in binary below, so you can line them up.
Common mistakes
- Confusing logical and arithmetic right shifts. They differ for negative numbers.
- Forgetting the width. The same NOT gives different decimals in 8 and 16 bits.
- Reading overflow as a bug. Bits shifted out are lost.
- Mixing up AND and OR. AND needs both bits to be 1.
- Typing hexadecimal in decimal mode. Choose the right base first.
What next
Use this often? Add Bitwise to your Home screen.
Frequently asked questions
What is a bitwise calculator?
A tool that applies AND, OR, XOR, NOT and shifts to the binary digits of whole numbers, with results in binary, decimal and hex.
What is 0101 AND 0011?
0001. AND gives 1 only where both bits are 1.
What does a left shift do?
It moves bits to the left and adds zeros on the right. Shifting left by n multiplies by 2ⁿ if nothing overflows. 00010111 (23) shifted left once is 00101110 (46).
What is the difference between arithmetic and logical right shift?
Arithmetic keeps the sign bit, so 10010111 (−105) >> 1 gives 11001011 (−53). Logical fills with 0, giving 01001011 (75).
What is NOT of a number?
It flips every bit. In 8 bits NOT 10101011 (171) is 01010100 (84).
What are NAND, NOR and XNOR?
NAND is NOT AND, NOR is NOT OR and XNOR is NOT XOR. For 8-bit F0 and CC, NAND is 3F, NOR is 03 and XNOR is C3.
Sources
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.