Home Tutorials Tutorial on Bitwise Operators in MicroPython

Tutorial on Bitwise Operators in MicroPython

by shedboy71

Bitwise operators in MicroPython are used to manipulate individual bits within integer values. These operators work directly on the binary representations of numbers, allowing you to perform operations like AND, OR, XOR, and shifting bits.

Bitwise operations are fast and often used in low-level programming, embedded systems, and performance-critical code.

This tutorial will walk you through all the bitwise operators supported in MicroPython, with code examples for each.

Table of Contents:

1. Overview of Bitwise Operators

Bitwise operators operate directly on the bits of integers. Here is a summary of the operators:

Operator Name Description Example
& AND Performs a bitwise AND between two numbers a & b
` ` OR Performs a bitwise OR between two numbers
^ XOR Performs a bitwise XOR between two numbers a ^ b
~ NOT Inverts the bits of a number ~a
<< Left Shift Shifts bits to the left a << 2
>> Right Shift Shifts bits to the right a >> 2

Before moving to examples, let’s quickly recall binary numbers. For instance, the binary representation of the integer 6 is 110, and for 3 it’s 011. These binary representations help you understand how bitwise operations work.

2. Bitwise AND (&)

The & (AND) operator compares each bit of two integers and returns 1 only if both bits are 1. Otherwise, it returns 0.

Example:

a = 6 # Binary: 110
b = 3 # Binary: 011
result = a & b
print(result) # Output: 2 (Binary: 010)

Explanation:

6 (binary 110) AND 3 (binary 011) gives 010, which is 2 in decimal.

3. Bitwise OR (|)

The | (OR) operator compares each bit of two integers and returns 1 if either of the bits is 1. If both are 0, it returns 0.

Example:

a = 6 # Binary: 110
b = 3 # Binary: 011
result = a | b
print(result) # Output: 7 (Binary: 111)

Explanation:

6 (binary 110) OR 3 (binary 011) gives 111, which is 7 in decimal.

4. Bitwise XOR (^)

The ^ (XOR) operator compares each bit of two integers and returns 1 if the bits are different (i.e., one is 1 and the other is 0). If both bits are the same, it returns 0.

Example:

a = 6 # Binary: 110
b = 3 # Binary: 011
result = a ^ b
print(result) # Output: 5 (Binary: 101)

Explanation:

6 (binary 110) XOR 3 (binary 011) gives 101, which is 5 in decimal.

5. Bitwise NOT (~)

The ~ (NOT) operator inverts all the bits of the number. This means all 1s become 0s and all 0s become 1s. However, the result is typically expressed using two’s complement, which includes a sign bit (positive or negative).

Example:

a = 6 # Binary: 110
result = ~a
print(result) # Output: -7

Explanation:

The binary representation of 6 is 0000 0110. The NOT operation inverts all the bits to 1111 1001, which corresponds to -7 in two’s complement.

6. Left Shift (<<)

The << (left shift) operator shifts the bits of the number to the left by a specified number of positions. It is equivalent to multiplying the number by 2^n, where n is the number of positions shifted.

Example:

a = 3 # Binary: 011
result = a << 2
print(result) # Output: 12 (Binary: 1100)

Explanation:

Shifting the bits of 3 (011) two places to the left gives 1100, which is 12 in decimal.

7. Right Shift (>>)

The >> (right shift) operator shifts the bits of the number to the right by a specified number of positions. It is equivalent to integer division by 2^n, where n is the number of positions shifted.

Example:

a = 8 # Binary: 1000
result = a >> 2
print(result) # Output: 2 (Binary: 0010)

Explanation:

Shifting the bits of 8 (1000) two places to the right gives 0010, which is 2 in decimal.

8. Practical Example: Manipulating Bits

Bitwise operators are often used in low-level programming for tasks like setting or clearing specific bits. Here’s a practical example of how you can manipulate specific bits using bitwise operators.

Example: Checking, Setting, and Clearing a Specific Bit

Suppose you have an integer, and you want to check, set, or clear the third bit (counting from 0).

Checking a Bit (AND):

number = 5 # Binary: 101
bit_to_check = 1 << 2 # The third bit is 2^2 = 4 (Binary: 100)

# Check if the third bit is set (1)
if number & bit_to_check:
print("Third bit is set")
else:
print("Third bit is not set")

Output:

Third bit is set

Setting a Bit (OR):

number = 5 # Binary: 101
bit_to_set = 1 << 1 # Second bit (Binary: 010)

# Set the second bit to 1
number = number | bit_to_set
print(bin(number)) # Output: 0b111 (Binary: 111, Decimal: 7)

Clearing a Bit (AND with NOT):

number = 7 # Binary: 111
bit_to_clear = ~(1 << 2) # Create a mask to clear the third bit

# Clear the third bit
number = number & bit_to_clear
print(bin(number)) # Output: 0b011 (Binary: 011, Decimal: 3)

 

Conclusion

Bitwise operators in MicroPython are powerful tools that allow you to manipulate individual bits of numbers.

They are particularly useful in low-level embedded systems programming where you need to control hardware devices or handle binary data.

AND (&), OR (|), and XOR (^) operators let you perform logical operations on bits.
NOT (~) inverts the bits.

Left Shift (<<) and Right Shift (>>) move bits in a number.

You can use bitwise operations to manipulate individual bits, such as checking, setting, or clearing them.

By mastering bitwise operators, you’ll be able to perform highly efficient operations, especially when working with hardware, flags, and memory-level data manipulation.

You may also like

Leave a Comment