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.
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
| Operator | Name | Example | Answer |
|---|---|---|---|
& | AND | 5 & 3 | 1 |
| | OR | 5 | 3 | 7 |
^ | XOR | 5 ^ 3 | 6 |
~ | NOT | ~5 | -6 |
<< | Shift left | 5 << 2 | 20 |
>> | Shift right | 5 >> 1 | 2 |
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
5is0101and3is0011. Write them one under the other to follow along.&looks down each column and keeps a 1 only where both rows have 1. Only the last column qualifies, so the answer is0001, which is 1.|keeps a 1 where either row has one, giving0111, which is 7.^keeps a 1 where the two rows differ, giving0110, which is 6.~flips every bit, including the sign bit, so 5 becomes -6. That looks strange until you remember negative numbers are stored differently.<< 2moves every bit two places left, giving10100: 20. Each step left doubles the number.>> 1moves every bit one place right, giving0010: 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
&withand, or|withor. The word versions work on True/False; the symbol versions work on bits. - Expecting
>>to round. It always moves towards the smaller number. - Assuming
~nis simply-n. It is-n - 1.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Print 12 & 10, 12 | 10 and 12 ^ 10, and check each answer in binary with `bin()`.
- Use `<<` to multiply 7 by 8 in a single line.
- Use `>>` to divide 100 by 4 in a single line.
- 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.