Cadmeo

Bitwise Calculator

A AND B

8 · 0b1000 · 0x8

A OR B

14 · 0b1110 · 0xE

A XOR B

6 · 0b110 · 0x6

NOT A

-13 · 0b11111111111111111111111111110011 · 0xFFFFFFF3

A << 1 (left shift)

24 · 0b11000 · 0x18

A >> 1 (arithmetic right shift)

6 · 0b110 · 0x6

A >>> 1 (logical right shift)

6 · 0b110 · 0x6

The bitwise calculator applies AND, OR, XOR, NOT and the three shift operators to two values, showing every result in decimal, binary and hexadecimal at once. Input can be in any of four bases, so a hex mask and a decimal value can be combined without converting either first.

How it works

  • AND sets a bit only where both inputs have it. Used to mask bits off.
  • OR sets a bit where either input has it. Used to turn bits on.
  • XOR sets a bit where exactly one input has it. Applying it twice returns the original, which is why it appears in checksums and simple ciphers.
  • NOT inverts every bit. In a signed 32-bit representation, ~n equals −(n+1).
  • << shifts left, multiplying by two per place. >> shifts right preserving the sign bit; >>> shifts right filling with zeros.

JavaScript coerces operands to signed 32-bit integers for bitwise operations, which is why the results here are 32-bit and why NOT of a small positive number is negative. Values above 2^31 − 1 wrap.

The difference between >> and >>> only shows on negative numbers. −8 >> 1 is −4, preserving the sign; −8 >>> 1 is 2147483644, because the sign bit is treated as an ordinary bit.

Examples

Masking with AND

A

12 (1100)

B

10 (1010)

Result

AND 8 (1000) · OR 14 (1110) · XOR 6 (0110)

Only the 8 bit is set in both, so AND gives 8. XOR gives the bits that differ, which are 4 and 2.

Shifting as multiplication

A

12

Result

12 << 1 = 24 · 12 >> 1 = 6

Each left shift doubles and each right shift halves, discarding any remainder. This was once a meaningful optimisation; compilers now do it for you.

NOT on a positive value

A

12

Result

NOT A = −13

In two’s complement, inverting every bit of n gives −(n+1). Not a bug. It is how signed integers are represented.

Frequently asked questions

Why is NOT 12 equal to −13 rather than 3?

Because the value is treated as a signed 32-bit integer in two’s complement. Inverting all 32 bits of 12 gives a number whose top bit is set, which represents −13. Mask with 0xFF or 0xFFFF if you want to work in a narrower unsigned width.

What is the difference between >> and >>>?

Only visible on negative numbers. >> is an arithmetic shift that copies the sign bit, so −8 >> 1 is −4. >>> is a logical shift that fills with zeros, so −8 >>> 1 is 2147483644. Use >> for signed arithmetic and >>> when treating the value as a bit pattern.

Why are the results limited to 32 bits?

Because JavaScript converts operands to signed 32-bit integers before any bitwise operation, regardless of the number’s original size. Values above 2^31 − 1 wrap around. Working with larger values needs BigInt, which uses different operators.

What is XOR actually used for?

Toggling bits and detecting difference. Because a XOR b XOR b returns a, it underpins simple encryption, parity checks and RAID, and it is the standard trick for swapping two variables without a temporary.