Skip to content

Latest commit

 

History

History
313 lines (214 loc) · 6.38 KB

README.md

File metadata and controls

313 lines (214 loc) · 6.38 KB

Chapter 15: Bitwise Operators

Table of Contents

Introduction to Bitwise Operations

Bitwise operations are operations that directly manipulate bits of binary numbers. These operations are useful in various low-level programming tasks, such as working with hardware, cryptography, and network protocols.

Example: Binary Representation

You can use the bin() function to get the binary representation of a number.

x = 5
print(bin(x))

Output:

0b101

Explanation: The bin() function converts the integer 5 to its binary representation 0b101.

Bitwise AND, OR, XOR, NOT

Bitwise operators perform operations on the individual bits of binary numbers.

Bitwise AND (&)

The bitwise AND operator returns 1 if both bits are 1, otherwise it returns 0.

x = 5  # 101
y = 6  # 110
result = x & y  # 100
print(result)

Output:

4

Bitwise OR (|)

The bitwise OR operator returns 1 if at least one of the bits is 1.

x = 5  # 101
y = 6  # 110
result = x | y  # 111
print(result)

Output:

7

Bitwise XOR (^)

The bitwise XOR operator returns 1 if only one of the bits is 1.

x = 5  # 101
y = 6  # 110
result = x ^ y  # 011
print(result)

Output:

3

Bitwise NOT (~)

The bitwise NOT operator inverts all the bits.

x = 5  # 0101
result = ~x  # 1010 (inverted)
print(result)

Output:

-6

Explanation: The bitwise NOT operator inverts all the bits of 5, resulting in -6 due to two's complement representation.

Bit Shifting Operators

Bit shifting operators shift the bits of a number to the left or right.

Left Shift (<<)

The left shift operator shifts the bits to the left, adding zeros from the right.

x = 5  # 101
result = x << 1  # 1010
print(result)

Output:

10

Right Shift (>>)

The right shift operator shifts the bits to the right, copying the leftmost bit.

x = 5  # 101
result = x >> 1  # 10
print(result)

Output:

2

Operator Precedence

Operator precedence determines the order in which operations are performed in an expression. Operators with higher precedence are evaluated before operators with lower precedence.

Precedence Table

Here are operators in Python, listed from highest to lowest precedence:

  1. ** (Exponentiation)
  2. +x, -x, ~x (Unary plus, Unary minus, Bitwise NOT)
  3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)
  4. +, - (Addition, Subtraction)
  5. <<, >> (Bitwise shift operators)
  6. & (Bitwise AND)
  7. ^ (Bitwise XOR)
  8. | (Bitwise OR)
  9. ==, !=, >, >=, <, <=, is, is not, in, not in (Comparisons, Identity, Membership)
  10. not (Logical NOT)
  11. and (Logical AND)
  12. or (Logical OR)
  13. if - else (Ternary operator)
  14. = (Assignment) and other assignment operators (+=, -=, *=, /=, //=, %=, **=, &=, |=, ^=, >>=, <<=)

Examples

Example 1: Exponentiation and Multiplication

result = 2 ** 3 * 4
print(result)

Output:

32

Explanation: Exponentiation (**) has higher precedence than multiplication (*), so 2 ** 3 is evaluated first, resulting in 8, and then 8 * 4 is evaluated, resulting in 32.

Example 2: Addition and Multiplication

result = 2 + 3 * 4
print(result)

Output:

14

Explanation: Multiplication (*) has higher precedence than addition (+), so 3 * 4 is evaluated first, resulting in 12, and then 2 + 12 is evaluated, resulting in 14.

Example 3: Parentheses

result = (2 + 3) * 4
print(result)

Output:

20

Explanation: Parentheses have the highest precedence, so the expression inside the parentheses (2 + 3) is evaluated first, resulting in 5, and then 5 * 4 is evaluated, resulting in 20.

Example 4: Logical Operators

result = not (True and False) or True
print(result)

Output:

True

Explanation: The and operator has higher precedence than not, so True and False is evaluated first, resulting in False. Then not False is evaluated, resulting in True. Finally, True or True is evaluated, resulting in True.

Example 5: Comparison and Logical Operators

x = 10
y = 20
result = x < y and y > 15
print(result)

Output:

True

Explanation: Comparison operators (<, >) have higher precedence than logical operators (and), so x < y and y > 15 are evaluated first, resulting in True and True respectively. Then True and True is evaluated, resulting in True.

Practical Applications of Bitwise Operations

Bitwise operations are used in various practical applications, such as setting, clearing, and toggling bits, and performing bitwise masks.

Example: Setting a Bit

To set a specific bit, use the bitwise OR operator with a mask.

x = 5  # 0101
mask = 1 << 1  # 0010
result = x | mask  # 0111
print(result)

Output:

7

Example: Clearing a Bit

To clear a specific bit, use the bitwise AND operator with a negated mask.

x = 5  # 0101
mask = ~(1 << 2)  # 1011
result = x & mask  # 0001
print(result)

Output:

1

Example: Toggling a Bit

To toggle a specific bit, use the bitwise XOR operator with a mask.

x = 5  # 0101
mask = 1 << 0  # 0001
result = x ^ mask  # 0100
print(result)

Output:

4

Summary

In this chapter, we covered bitwise operators, including AND, OR, XOR, and NOT, as well as bit shifting operators. We also discussed operator precedence and practical applications of bitwise operations.

Tasks

  1. Write a program that uses bitwise operators to set, clear, and toggle specific bits in a number.
  2. Write a program that uses bit shifting operators to multiply and divide a number by powers of two.
  3. Write a program that demonstrates the use of bitwise masks to extract specific bits from a number.
  4. Write a program that uses bitwise operators to perform basic arithmetic operations.

Next Chapter: Conclusion and Further Learning