The Python ampersand (&) is a bitwise operator that logically ANDs the relevant bits of two integer values.
Python ampersand is a bitwise operator.
Python ampersand performs a bitwise AND operation between two integers.
What is bitwise AND operation?
The bitwise AND operation compares two numbers bitwise.
And generates a new number based on the outcome of the comparison.
It is a fundamental operation in digital circuits and computer programming.
You can use the Python ampersand (&) operator to perform the bitwise AND operation.
Python ampersand and binary numbers
You must first have a basic understanding of binary numbers in order to understand the bitwise AND operation.
Binary numbers are base-2 numbers that are made up of only 0s and 1s.
Each digit in a binary number represents a power of 2.
For example, the binary number 1010 represents the decimal number 10, because it is made up of 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 0 * 2^0.
By comparing the corresponding bits of two binary numbers,
the bitwise AND operation creates a new binary number that only contains a 1 bit in locations where both input numbers contain a 1 bit.
Here’s a good example:
a = 0b1010 # binary representation of 10
b = 0b1100 # binary representation of 12
c = a & b # bitwise AND operation
print(c)
Output
8
In this example, we used the Python ampersand operator to perform a bitwise AND operation on the binary numbers a and b.
The result is a binary number that has a 1 bit only in positions where both a and b have a 1 bit.
In this case, the binary number c is 0b1000, which is the binary representation of the decimal number 8.
Now you have a good understanding of bitwise and operation.
Let’s understand Python ampersand.
Python Ampersand
In Python, the ampersand (&
) is a bitwise operator.
It is used to perform a bitwise AND operation between two integers.
The bitwise AND operation compares the corresponding bits of two integers and sets the bits in the result to 1 only if both bits are 1.
Here is an example:
a = 0b1101 # binary representation of 13
b = 0b1011 # binary representation of 11
c = a & b # bitwise AND operation
print(c) # Output: 0b1001 (binary representation of 9)
We have two binary numbers in this example, a and b.
We used the ampersand operator to perform the bitwise AND operation.
And, we assigned the outcome to c.
The outcome is a binary that only contains a 1 bit in the locations where both a and b are 1.
Python Ampersand in Sets
In Python, you can use the ampersand operator for other types like sets and boolean values.
Python carries out the intersection operation for sets using the & operator.
It then produces a brand-new set that only includes the elements that are present in both sets.
Here is a good demonstration of it:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set3 = set1 & set2
print(set3)
Output
{3, 4}
In this example, we have two sets set1 and set2
We used the ampersand operator to perform an intersection operation.
And then we assigned the result to set3
The result is a new set that contains only the elements that are present in both set1 and set2
Python Ampersand in Booleans
You can use the ampersand operator to carry out a logical AND operation on boolean values.
If both operands are True, it returns True; otherwise, it returns False.
Here is an example:
a = True
b = False
c = a & b
print(c)
Output
False
We have two boolean values in this example, a and b.
We assigned the result of a logical AND operation to c.
Because a is True and b is False, the result is False.
Next, You can read this detailed explanation of all major Python operators.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.