Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Operator &

Keeps the bit positions set in both integers.

Version: Python 3.14; repository image, production runtime not attested

Syntax

left & right

Keeps the bit positions set in both integers.

Intersect bit masks to test which enabled flags belong to an allowed group. For every position the result is one only when both operand bits are one.

The built-in integer operation accepts integers rather than arbitrary floating-point values. Sets also define this operation, producing the intersection of their members.

Negative one acts as an infinite sequence of one bits in integer bitwise operations.

Syntax

left & right

__and__, __rand__ · TypeError

Worked example

0b1010 & 0b0110 → 0b0010
0b1010 & 0 → 0
-1 & 10 → 10

set &

For set and frozenset, & returns a new intersection containing only elements in both operands. Mixed operations return the left set's type. A list on the right raises TypeError, although intersection accepts an iterable.

left={1,2,4}
right=frozenset((2,4,8))
assert left & right == {2,4} and left=={1,2,4}
assert type(right & left) is frozenset
try: left & [2]
except TypeError: pass
else: raise AssertionError
assert left.intersection([2])=={2}

Examples

print(0b1100 & 0b1010)
print(-1 & 0b1010)

Result

8
10

Negative one acts as an infinite sequence of one bits in integer bitwise operations.

Sources