Bitwise operators explained

Published by InstantToolDeck · Updated November 2026

Bitwise operators work on the individual bits of a number instead of the number as a whole. They are fast, and they appear in permissions, graphics, networking, compression and embedded code. This guide explains each operator with small examples you can check in the Hex Calculator.

The basic operators

OperatorNameRule for each bitExample (decimal)
&AND1 only if both bits are 112 & 10 = 8
|OR1 if either bit is 112 | 10 = 14
^XOR1 if the bits differ12 ^ 10 = 6
~NOTflips every bit~5 = -6
<<Left shiftmoves bits left, filling with 03 << 2 = 12
>>Right shiftmoves bits right12 >> 2 = 3

To see why 12 & 10 is 8, write both in binary: 12 is 1100 and 10 is 1010. Only the leftmost bit is 1 in both, so the result is 1000, which is 8.

Shifts are multiplication and division by powers of two

Shifting left by one place doubles a number and shifting right by one halves it (dropping the remainder). 1 << 8 is 256, which is a quick way to write 2 to the power 8. Compilers often turn multiplication by powers of two into shifts.

Masks: reading and setting bits

A mask is a number whose set bits mark the ones you care about. To read the low four bits of a byte, AND it with 0x0F. To check whether bit 3 is set, test value & (1 << 3). To set a bit, OR with the mask. To clear a bit, AND with the NOT of the mask. To toggle a bit, XOR with the mask.

Flags

Many systems store several yes/no options in one integer. File permissions are the classic case: read is 4, write is 2 and execute is 1, so 7 means all three and 5 means read and execute. Combine flags with OR and test them with AND.

XOR tricks

XOR has useful properties: a ^ a is 0 and a ^ 0 is a. Applying the same XOR twice restores the original value, which is why it appears in simple checksums and toy ciphers. It is not secure encryption on its own.

Negative numbers and NOT

In most languages integers use two’s complement, so ~5 equals -6: flipping all the bits of a number gives the negative of the number minus one. In languages with a fixed width such as 8 or 32 bits you may need to mask the result, for example (~5) & 0xFF is 250. The calculator here uses unlimited-size integers, so it shows -6.

Precedence

Among the bitwise operators, shifts bind tightest, then AND, then XOR, then OR, and shifts rank below addition. The surprise is comparison: in C, Java and JavaScript, == binds tighter than &, so x & 1 == 0 means x & (1 == 0). Python orders them the other way. Always add parentheses around bitwise expressions.

Frequently asked questions

What is the difference between & and &&?

A single & combines the bits of two numbers. A double && is a logical AND that works on true/false values and short-circuits.

How do I check if a number is even using bits?

AND it with 1. If the result is 0 the number is even, because the lowest bit decides odd or even.

What does x << n do?

It shifts the bits of x left by n places, which multiplies it by 2 to the power n.

Is XOR the same as power?

No. In C, JavaScript, Python and most languages ^ is XOR. Use ** or a pow function for powers.

← All guides