- Introduction to Bitwise Operations
- Bitwise AND, OR, XOR, NOT
- Bit Shifting Operators
- Operator Precedence
- Practical Applications of Bitwise Operations
- Summary
- Tasks
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.
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 operators perform operations on the individual bits of binary numbers.
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
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
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
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 shift the bits of a number to the left or right.
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
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 determines the order in which operations are performed in an expression. Operators with higher precedence are evaluated before operators with lower precedence.
Here are operators in Python, listed from highest to lowest precedence:
**
(Exponentiation)+x
,-x
,~x
(Unary plus, Unary minus, Bitwise NOT)*
,/
,//
,%
(Multiplication, Division, Floor Division, Modulus)+
,-
(Addition, Subtraction)<<
,>>
(Bitwise shift operators)&
(Bitwise AND)^
(Bitwise XOR)|
(Bitwise OR)==
,!=
,>
,>=
,<
,<=
,is
,is not
,in
,not in
(Comparisons, Identity, Membership)not
(Logical NOT)and
(Logical AND)or
(Logical OR)if
-else
(Ternary operator)=
(Assignment) and other assignment operators (+=
,-=
,*=
,/=
,//=
,%=
,**=
,&=
,|=
,^=
,>>=
,<<=
)
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
.
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
.
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
.
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
.
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
.
Bitwise operations are used in various practical applications, such as setting, clearing, and toggling bits, and performing bitwise masks.
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
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
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
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.
- Write a program that uses bitwise operators to set, clear, and toggle specific bits in a number.
- Write a program that uses bit shifting operators to multiply and divide a number by powers of two.
- Write a program that demonstrates the use of bitwise masks to extract specific bits from a number.
- Write a program that uses bitwise operators to perform basic arithmetic operations.