Free preview

86 lessons

Python Essentials (PCEP)

Free preview

Python Essentials (PCEP) · 86 lessons

Prepare for the PCEP certification, properly

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

Bitwise operators

Introduction to bits

Explanation

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 2×2=22=42 \times 2 = 2^2 = 4 possible combinations: 00, 01, 10 and 11.

In general, with N bits, each bit can take 2 values, so there are:

2N possible combinations.2^N \text{ possible combinations.}
WidthNumber of bitsPossible values
1 bit121=22^1 = 2
8 bits (1 byte)828=2562^8 = 256
16 bits16216=65,5362^{16} = 65{,}536
32 bits322324.3 billion2^{32} \approx 4.3 \text{ billion}

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

Explanation

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 ABit BA & B
000
010
100
111

Bitwise OR (|)

The | operator compares each pair of bits and returns 1 if at least one bit is 1:

Bit ABit BA | B
000
011
101
111
Bitwise vs logical operators

Details

Example

Bitwise AND example: 12 & 10

First, convert both numbers to binary:

  • 1212 in binary is 1100
  • 1010 in binary is 1010

Then apply AND to each pair of bits:

Bit position3210
1212 in binary1100
1010 in binary1010
Result (AND)1000

The result 1000 in binary equals 88 in decimal.

print(12 & 10)  # Output: 8

Bitwise OR example: 12 | 10

Using the same binary representations:

Bit position3210
1212 in binary1100
1010 in binary1010
Result (OR)1110

The result 1110 in binary equals 1414 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

Explanation

Bitwise XOR (^)

The ^ operator (exclusive or) compares each pair of bits and returns 1 if exactly one bit is 1:

Bit ABit BA ^ B
000
011
101
110

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:

x=(x+1)\sim x = -(x + 1)

For example:

ExpressionResult
~0-1
~1-2
~5-6
~(-1)0
~(-6)5
Why does NOT give negative numbers?

Details

Example

Bitwise XOR example: 12 ^ 10

Convert both numbers to binary:

  • 1212 in binary is 1100
  • 1010 in binary is 1010

Apply XOR to each pair of bits:

Bit position3210
1212 in binary1100
1010 in binary1010
Result (XOR)0110

The result 0110 in binary equals 66 in decimal.

print(12 ^ 10)  # Output: 6

Bitwise NOT example: ~5

Using the pattern x=(x+1)\sim x = -(x + 1):

5=(5+1)=6\sim 5 = -(5 + 1) = -6
print(~5)  # Output: -6

Practice questions

4 questions

What is the result of 7 ^ 3?

Select the correct answer:

+ 3 more questions

Shift operators

Explanation

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 22:

xn=x×2nx \ll n = x \times 2^n

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 22:

xn=x//2nx \gg n = x \mathbin{//} 2^n

Key Point

  • Left shift by NN: multiplies by 2N2^N
  • Right shift by NN: floor divides by 2N2^N (discards the remainder)
Why does shifting multiply or divide by 2?

Details

Example

Left shift example: 5 << 2

55 in binary is 101. Shifting left by 2 positions:

StepBinaryDecimal
Start10155
Shift left 110101010
Shift left 2101002020

Using the formula:

52=5×22=5×4=205 \ll 2 = 5 \times 2^2 = 5 \times 4 = 20
print(5 << 2)  # Output: 20

Right shift example: 20 >> 2

2020 in binary is 10100. Shifting right by 2 positions:

StepBinaryDecimal
Start101002020
Shift right 110101010
Shift right 210155

Using the formula:

202=20//22=20//4=520 \gg 2 = 20 \mathbin{//} 2^2 = 20 \mathbin{//} 4 = 5
print(20 >> 2)  # Output: 5

Practice questions

4 questions

What is the result of 3 << 3?

Select the correct answer:

+ 3 more questions