TTKTheTextKit

Bitwise Calculator

Type two numbers in decimal, hexadecimal (0xCA) or binary (0b1100_1010), choose 8, 16, 32 or 64 bits, and see AND, OR, XOR, their inverses, NOT, shifts and rotations at once. Each result appears in binary, hex, unsigned decimal and signed decimal. Everything runs in your browser.

Type decimal (202 or -54), hexadecimal (0xCA) or binary (0b1100_1010). A negative number is stored in two's complement.

Results of each bitwise operation
OperationBinaryHexUnsignedSigned
A1111 00000xF0240-16
B0011 11000x3C6060
A AND B0011 00000x304848
A OR B1111 11000xFC252-4
A XOR B1100 11000xCC204-52
A NAND B1100 11110xCF207-49
A NOR B0000 00110x0333
A XNOR B0011 00110x335151
NOT A0000 11110x0F1515
A << 21100 00000xC0192-64
A >> 2 (logical)0011 11000x3C6060
A >> 2 (arithmetic)1111 11000xFC252-4
Rotate A left 21100 00110xC3195-61
Rotate A right 20011 11000x3C6060

A has 4 bits set to 1 out of 8.

AND keeps a bit only where both inputs have it, OR keeps it where either has it, and XOR keeps it where exactly one has it. NAND, NOR and XNOR are those results flipped. NOT flips every bit of A within the chosen width. A left shift moves bits up and fills with zeros. A logical right shift fills the top with zeros, while an arithmetic right shift copies the sign bit, so a negative number stays negative. Rotations move bits out of one end and back in at the other. Results are exact for 64 bits. Everything is worked out in your browser and nothing is uploaded.

Related Tools

[5 suggestions]

About the Bitwise Calculator

Bitwise operations work on the individual bits of integers. AND keeps a bit where both numbers have it, OR where either has it, XOR where exactly one has it, and NAND, NOR and XNOR are those results with every bit flipped. NOT flips every bit of the first number within the chosen width, so NOT 5 is 250 in 8 bits and -6 as a signed value. A left shift moves the bits toward the top and fills with zeros, which multiplies by a power of two until bits fall off the top. A logical right shift fills the top with zeros, and an arithmetic right shift copies the sign bit, so a negative number stays negative: -8 shifted right by 1 is -4. A rotation moves bits out of one end and back in at the other, so nothing is lost. Signed values use two's complement, in which the top bit has a negative weight, so 0xFF is 255 unsigned and -1 signed in 8 bits. A negative decimal input is converted to that form, and a number that does not fit the width is refused with the allowed range. Arithmetic uses exact big integers, so 64-bit values keep every digit, which JavaScript numbers cannot do. The results were checked against JavaScript's own 32-bit operators. Nothing is uploaded.

What do AND, OR and XOR do?▾

AND gives a 1 only where both bits are 1. OR gives a 1 where either bit is 1. XOR gives a 1 where the bits differ. For 0xF0 and 0x3C in 8 bits, AND is 0x30, OR is 0xFC and XOR is 0xCC.

What is the difference between a logical and an arithmetic shift?▾

A logical right shift fills the new top bits with zeros. An arithmetic right shift copies the sign bit, so a negative number stays negative. Shifting the 8-bit value 0xF8 right by 1 gives 0x7C logically but 0xFC (which is -4) arithmetically.

How are negative numbers stored?▾

In two's complement: flip all the bits and add 1. In 8 bits, -1 is 11111111, which is 255 when read as unsigned. The top bit is the sign. The tool shows both the signed and unsigned reading of every result.

Why does NOT 5 give 250?▾

NOT flips every bit within the width. In 8 bits, 5 is 00000101, so NOT is 11111010, which is 250 unsigned or -6 signed. In 32 bits the same operation gives 4,294,967,290, so the width matters.

Can it handle 64-bit numbers exactly?▾

Yes. The calculator uses big integers, so every one of the 64 bits is kept. Ordinary JavaScript numbers lose precision above 2 to the power of 53. Nothing you enter is uploaded.