Vinay Garg Assistant Professor

Lesson 10: Bitwise operators

Bitwise operators work on the individual bits of a number, not on the number as a whole. Six of them — and, or, xor, not, shift left and shift right — with 5 and 3 as the examples.

A code window showing print(5 & 3, 5 | 3) and print(5 ^ 3, ~5)

In short

  • Bitwise operators work on the binary digits of a number, one position at a time.
  • `&` keeps a bit only where both numbers have it; `|` keeps it where either does.
  • `^` keeps a bit where exactly one number has it, which makes it useful for toggling.
  • `<<` and `>>` shift bits left and right, doubling or halving the number for each step.

6

bitwise operators: and, or, xor, not, shift left, shift right

Python docs

5 & 3

1 — only the last bit is set in both

this program

What a bit is

Everything inside a computer is stored as bits: 0 or 1. The number 5 is stored as 0101 and the number 3 as 0011. A bitwise operator works on each bit position separately instead of treating the number as one value.

You do not need bitwise operators for everyday arithmetic. They appear when a value is a set of flags — permissions, settings, features switched on or off.

The six operators

OperatorNameExampleAnswer
&AND5 & 31
|OR5 | 37
^XOR5 ^ 36
~NOT~5-6
<<Shift left5 << 220
>>Shift right5 >> 12

The program

# bitwise.py — working on the bits of a number

a = 5   # binary 0101
b = 3   # binary 0011

print("a & b  =", a & b)    # 1   AND: set in both
print("a | b  =", a | b)    # 7   OR: set in either
print("a ^ b  =", a ^ b)    # 6   XOR: set in exactly one
print("~a     =", ~a)       # -6  NOT: every bit flipped
print("a << 2 =", a << 2)   # 20  shifted two places left
print("a >> 1 =", a >> 1)   # 2   shifted one place right

print(bin(a), bin(b))       # 0b101 0b11

How it works, step by step

  1. 5 is 0101 and 3 is 0011. Write them one under the other to follow along.
  2. & looks down each column and keeps a 1 only where both rows have 1. Only the last column qualifies, so the answer is 0001, which is 1.
  3. | keeps a 1 where either row has one, giving 0111, which is 7.
  4. ^ keeps a 1 where the two rows differ, giving 0110, which is 6.
  5. ~ flips every bit, including the sign bit, so 5 becomes -6. That looks strange until you remember negative numbers are stored differently.
  6. << 2 moves every bit two places left, giving 10100: 20. Each step left doubles the number.
  7. >> 1 moves every bit one place right, giving 0010: 2. Each step right halves it and drops the remainder.

What it prints

a & b  = 1
a | b  = 7
a ^ b  = 6
~a     = -6
a << 2 = 20
a >> 1 = 2
0b101 0b11

Where this is actually used

Read a file permission as bits: 4 read, 2 write, 1 execute. Then 6 means read and write, and 7 means all three. Checking one flag is a single &: permission & 4 is non-zero when read is allowed.

Common mistakes

  • Confusing & with and, or | with or. The word versions work on True/False; the symbol versions work on bits.
  • Expecting >> to round. It always moves towards the smaller number.
  • Assuming ~n is simply -n. It is -n - 1.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Print 12 & 10, 12 | 10 and 12 ^ 10, and check each answer in binary with `bin()`.
  2. Use `<<` to multiply 7 by 8 in a single line.
  3. Use `>>` to divide 100 by 4 in a single line.
  4. Swap two variables without using a third one, using `^`.

Sources

Frequently asked questions

What does a bitwise operator do?

It works on the binary digits of a number one position at a time, instead of treating the number as a single value. 5 & 3 compares 0101 with 0011 and keeps only the bits set in both, giving 1.

Is 5 & 3 the same as 5 and 3?

No. 5 and 3 returns 3, because and works on whole values. 5 & 3 returns 1, because & compares the bits. Mixing them up is the most common bitwise mistake.

Why does ~5 give -6?

Python stores negative numbers in two's complement, so flipping every bit of 5 turns it into -6. The rule to remember is that ~n equals -n - 1.

Where are bitwise operators used in real programs?

Mostly for flags and permissions: a single number can hold several on/off settings, and &, | or ^ check and change them. They are also used in low-level code and in simple hashing.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.