Free preview
86 lessons
Python Essentials (PCEP)
Free preview
Python Essentials (PCEP) · 86 lessons
No surprise gaps
Actually remember it
Skip what you know
One subscription. All learning paths included.
Our content is best on a larger screen
Introduction to bits
A bit (short for binary digit) is the smallest unit of data in computing. It can hold one of two possible values: 0 or 1.
If we have 2 bits, each bit can independently be 0 or 1. This gives possible combinations: 00, 01, 10 and 11.
In general, with N bits, each bit can take 2 values, so there are:
| Width | Number of bits | Possible values |
|---|---|---|
| 1 bit | 1 | |
| 8 bits (1 byte) | 8 | |
| 16 bits | 16 | |
| 32 bits | 32 |
Info
When we say a system is 8-bit or 64-bit, we mean it processes or stores data in chunks of that width. For instance, a 64-bit CPU can work with 64 bits (8 bytes) of data in a single step.
AND and OR
Bitwise operators work on integers at the level of individual bits. Unlike logical operators (and, or) which treat values as simply True or False, bitwise operators examine each bit position independently.
Bitwise AND (&)
The & operator compares each pair of bits and returns 1 only if both bits are 1:
| Bit A | Bit B | A & B |
|---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Bitwise OR (|)
The | operator compares each pair of bits and returns 1 if at least one bit is 1:
| Bit A | Bit B | A | B |
|---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Details
Bitwise AND example: 12 & 10
First, convert both numbers to binary:
11001010Then apply AND to each pair of bits:
| Bit position | 3 | 2 | 1 | 0 |
|---|---|---|---|---|
| in binary | 1 | 1 | 0 | 0 |
| in binary | 1 | 0 | 1 | 0 |
Result (AND) | 1 | 0 | 0 | 0 |
The result 1000 in binary equals in decimal.
print(12 & 10) # Output: 8
Bitwise OR example: 12 | 10
Using the same binary representations:
| Bit position | 3 | 2 | 1 | 0 |
|---|---|---|---|---|
| in binary | 1 | 1 | 0 | 0 |
| in binary | 1 | 0 | 1 | 0 |
Result (OR) | 1 | 1 | 1 | 0 |
The result 1110 in binary equals in decimal.
print(12 | 10) # Output: 14
Practice questions
4 questions
What is the result of 6 & 3?
Select the correct answer:
+ 3 more questions
XOR and NOT
Bitwise XOR (^)
The ^ operator (exclusive or) compares each pair of bits and returns 1 if exactly one bit is 1:
| Bit A | Bit B | A ^ B |
|---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
XOR is sometimes described as "one or the other, but not both."
Bitwise NOT (~)
The ~ operator inverts all bits in a number. However, due to how Python represents negative numbers internally, the result follows this pattern:
For example:
| Expression | Result |
|---|---|
~0 | -1 |
~1 | -2 |
~5 | -6 |
~(-1) | 0 |
~(-6) | 5 |
NOT give negative numbers?Details
Bitwise XOR example: 12 ^ 10
Convert both numbers to binary:
11001010Apply XOR to each pair of bits:
| Bit position | 3 | 2 | 1 | 0 |
|---|---|---|---|---|
| in binary | 1 | 1 | 0 | 0 |
| in binary | 1 | 0 | 1 | 0 |
Result (XOR) | 0 | 1 | 1 | 0 |
The result 0110 in binary equals in decimal.
print(12 ^ 10) # Output: 6
Bitwise NOT example: ~5
Using the pattern :
print(~5) # Output: -6
Practice questions
4 questions
What is the result of 7 ^ 3?
Select the correct answer:
+ 3 more questions
Shift operators
The shift operators move all bits in a number left or right by a specified number of positions.
Left shift (<<)
The << operator shifts bits to the left, filling empty positions on the right with 0s. Each left shift by one position multiplies the number by :
Right shift (>>)
The >> operator shifts bits to the right, discarding bits that fall off the end. Each right shift by one position floor divides the number by :
Key Point
Details
Left shift example: 5 << 2
in binary is 101. Shifting left by 2 positions:
| Step | Binary | Decimal |
|---|---|---|
| Start | 101 | |
| Shift left 1 | 1010 | |
| Shift left 2 | 10100 |
Using the formula:
print(5 << 2) # Output: 20
Right shift example: 20 >> 2
in binary is 10100. Shifting right by 2 positions:
| Step | Binary | Decimal |
|---|---|---|
| Start | 10100 | |
| Shift right 1 | 1010 | |
| Shift right 2 | 101 |
Using the formula:
print(20 >> 2) # Output: 5
Practice questions
4 questions
What is the result of 3 << 3?
Select the correct answer:
+ 3 more questions