Module 4 · Operators — topic 12 of 16
Bitwise operators
Bitwise operators (&, |, ^, ~, <<, >>) work on the binary digits of integers. Learn how each works, how to read binary with bin(), and real uses such as flags and permissions.
Learning objectives
- Read integers in binary with bin()
- Apply &, |, ^, ~, << and >>
- Use bit flags to store several options in one number
Definition
Bitwise operators (&, |, ^, ~, <<, >>) work on the binary digits of integers. Learn how each works, how to read binary with bin(), and real uses such as flags and permissions.
Introduction
Computers store integers as patterns of bits, 0s and 1s. Bitwise operators work directly on those bits. 6 is 110 in binary and 3 is 011, so 6 & 3 (bitwise AND) is 010, which is 2.
Why it matters
Bitwise operations are less common in everyday scripts but essential in networking, file permissions, graphics, embedded devices and compact storage of on/off options. They also explain why ^ isn’t exponentiation in Python.
Detailed explanation
bin(n) shows the binary form: bin(6) is '0b110'. Each position is a power of two: 110 means 4 + 2 + 0 = 6.
& (AND): a bit is 1 only if both bits are 1. | (OR): 1 if either bit is 1. ^ (XOR): 1 if the bits differ.
~ (NOT) flips every bit. For Python integers this gives -(n + 1): ~5 is -6.
<< shifts bits left, multiplying by powers of two: 3 << 2 is 12. >> shifts right, floor-dividing by powers of two: 20 >> 2 is 5.
Bit flags: give each option its own bit (1, 2, 4, 8, …). Combine options with |, test one with &, and remove one with & ~flag.
Bitwise operators work only on integers (and Booleans). and/or/not are the logical operators for conditions; don’t mix them up.
Syntax
- &, | and ^ combine two integers bit by bit.
- ~ inverts all bits.
- << and >> shift bits left or right by n places.
Code examples
Seeing the bits
0b110 0b11 2 7 5 0b10 0b111 0b101
Shifts and NOT
12 5 -6
More examples
Permission flags
6 rw- 7 rwx 5 r-x
Step-by-step explanation
- Each permission is a different power of two, so each occupies its own bit.
- READ | WRITE combines two flags into 6 (binary 110).
- perms & READ is non-zero (truthy) only if the READ bit is set.
- |= adds EXECUTE; &= ~WRITE clears the WRITE bit, leaving 5 (binary 101).
Expected output
Running “Permission flags” prints exactly:
Practical applications
- Linux file permissions such as 755 are bit flags for read, write and execute.
- Network code uses & with subnet masks to compare IP addresses.
- Graphics code packs red, green and blue values into one integer with << and |.
Best practices
- Give flags named constants that are powers of two.
- Use and/or/not for conditions, &/|/^ for bits.
- Use bin() and format(n, "08b") to inspect bits while debugging.
Common mistakes
Using & for logical conditions
Avoid
Prefer
& binds tighter than >, so this computes 18 & has_id first. Use and for conditions.
Expecting ~ to flip to a small positive number
Avoid
Prefer
Python integers have no fixed width; ~n is always -(n + 1).
Key takeaways
- & | ^ ~ << >> operate on binary digits of integers.
- << and >> multiply and floor-divide by powers of two.
- Bit flags combine with |, test with &, and clear with & ~.
Try it yourself
- 1Print the numbers 0 to 8 in 4-digit binary using format(n, "04b").
- 2Use XOR to check whether two integers have the same parity (both even or both odd).
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?