Skip to the tool
Binary Translator Free tools

Bitwise operators

Bitwise operators work on the individual bits of a number. Try them in the calculator, then see the truth tables, examples and the mask tricks programmers use them for.

Updated · Written and checked by Murugan Vellaichamy

Waiting for input
Result ()

Truth tables

ABA AND BA OR BA XOR BNOT A
000001
010111
100110
111100

A bitwise operator applies its rule to each pair of bits in the same position, independently of the others.

The operators

OperatorSymbolWhat it doesExample
AND&1 only where both bits are 11100 & 1010 = 1000
OR|1 where either bit is 11100 | 1010 = 1110
XOR^1 where the bits differ1100 ^ 1010 = 0110
NOT~Flips every bit~1100 = 0011 (in 4 bits)
Left shift<<Moves bits left, fills with 0; multiplies by 2 per place0011 << 2 = 1100
Right shift>>Moves bits right; drops the bits that fall off1100 >> 2 = 0011

Masks: testing, setting, clearing and toggling bits

A mask is a number with 1s in the bit positions you care about. Combined with the right operator, it lets you work on single bits, for example the on/off flags packed into one byte. Here the flags start as 0101.

TaskCodeResult
Test bit 2flags & 0b01000101 & 0100 = 0100 (not zero, so it’s set)
Set bit 1flags | 0b00100101 | 0010 = 0111
Clear bit 0flags & ~0b00010101 & 1110 = 0100
Toggle bit 3flags ^ 0b10000101 ^ 1000 = 1101

Shifts as multiplication and division

Shifting left by one place doubles a number, and by n places multiplies it by 2n, as long as no 1s fall off the top. Shifting right by one place halves it, rounding down. 13 << 1 is 26, and 13 >> 1 is 6.

Bitwise operators in JavaScript

  • The operators &, | and ^ first turn both numbers into 32-bit two’s complement integers, so anything beyond 32 bits is lost.
  • >> is a sign-filling right shift: negative numbers stay negative. >>> fills with zeros instead.
  • The shift count is taken modulo 32, so x << 33 is the same as x << 1.
  • For wider numbers, BigInt values (such as 1n << 40n) work at any size. The calculator above uses them, which is why it can go past 64 bits.

Sources and method

Results use exact whole-number arithmetic at any size. With a bit width chosen, the result is wrapped to that width the way a CPU register does, and overflow is checked separately for unsigned and signed (two’s complement) readings.

Questions

What is the difference between AND and OR?

AND gives 1 only where both bits are 1; OR gives 1 where at least one bit is 1.

What is XOR used for?

Toggling bits (XOR with 1 flips a bit), comparing values bit by bit (XOR is 0 where they match), and Gray code, which is a number XOR itself shifted right by one. The Gray code converter shows each XOR step.

Why does ~5 give −6?

NOT flips every bit, including the sign bit of a two’s complement number. ~x is always −x − 1.

Are bitwise and logical operators the same?

No. Logical operators such as && and || treat each whole value as true or false; bitwise operators work on every bit separately.

More converters

Copied