The usage of Python

In Python, the & operator is used for bitwise AND operations and can be used with integers and boolean values.

When used with integers, & performs a binary bitwise AND operation on two integers and returns the result. For example:

a = 5  # 二进制表示为 0101
b = 3  # 二进制表示为 0011
c = a & b  # 二进制按位与操作,结果为 0001,即十进制的 1
print(c)  # 输出 1

When used with boolean values, & performs a logical AND operation and returns a boolean result. For example:

a = True
b = False
c = a & b  # 逻辑与操作,结果为 False
print(c)  # 输出 False

In addition, & can also be used for set operations, such as finding the intersection of two sets. For example:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
intersection = set1 & set2  # 求两个集合的交集
print(intersection)  # 输出 {3, 4}
bannerAds