Bitwise Calculator
Enter A and B in decimal, 0x hex, 0b binary or 0o octal, choose the width and see every bitwise operation at once, with signed and unsigned values.
Worked examples
Mask
A = 0xF0F0, B = 0xFF00, 16 bits, A AND B
0xF000
Only the top four bits are set in both.
Sign-keeping shift
A = -16, B = 2, 32 bits
-4
Negative numbers stay negative.
NOT
A = 5, 8 bits
250
The bits of 00000101 flip to 11111010.
About Bitwise Calculator
Bitwise operations work on the individual bits of a number. AND keeps the bits set in both inputs, OR keeps bits set in either, XOR keeps bits that differ and NOT flips every bit. Shifts move the bits left or right, which multiplies or divides by powers of two, and rotations move them around the end. They are used for flags, masks, hashes, colour values, permissions and low-level protocols.
The width matters, because a number such as -1 has a different bit pattern in 8 and in 64 bits. This calculator uses two's complement at the width you pick, and shows the result of every operation at once as hex, as an unsigned number, as a signed number and in binary with the bits grouped in fours. It counts the bits set in A, and uses exact big number arithmetic so 64 bit values keep every digit. A number that does not fit the width is reported.
How to use Bitwise Calculator
- Enter A and, if you need it, B. Use decimal, 0x hex, 0b binary or 0o octal.
- Choose 8, 16, 32 or 64 bits.
- Read the table of results. B is the second operand and the shift or rotate count.
- Copy the value you need in hex, decimal or binary.
Common uses
- Working out a mask or a set of flags
- Checking what a shift does to a negative number
- Reading a register value from a datasheet
- Learning how two's complement works
Frequently asked questions
What is two's complement?
The usual way to store negative whole numbers. The top bit is the sign, and -1 is all ones. In 8 bits, -1 is 11111111, which is 255 if read as unsigned.
What is the difference between the two right shifts?
A logical shift fills the top with zeros. The sign-keeping shift copies the top bit, so negative numbers stay negative: -16 shifted right by 2 is -4.
Why does the width change the answer?
NOT, shifts that overflow and rotations all depend on how many bits there are. NOT 5 is 250 in 8 bits and 65530 in 16 bits.
Can I use very large numbers?
Yes up to 64 bits. The arithmetic is exact, so no digits are lost.
How do I test a single bit?
AND the number with a mask that has only that bit set. If the result is not zero, the bit is on.