What Does & Mean in Python [Duplicate]

What Does & Mean in Python [Duplicate]

I came across the following code

numdigits = len(cardNumber)
oddeven = numdigits & 1

what exactly is going on here? I'm not sure what the "&" is doing.

2

5 Answers

Answer

The & symbol is a bitwise AND operator. Used with 1, it basically masks the value to extract the lowest bit, or in other words will tell you if the value is even or odd.

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.