What does & mean in python

2020-01-25 06:24发布

Hi 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.

标签: python
5条回答
我命由我不由天
2楼-- · 2020-01-25 06:57

In addition, & is also used for taking the intersection of two Python sets:

set1 = {0,1,2,3}
set2 = {2,3,4,5}
print(set1 & set2)
>>>set([2, 3])
查看更多
孤傲高冷的网名
3楼-- · 2020-01-25 07:01

& is a bitwise and, which is an efficient way to do bit-level calculations. It is taking numdigits and and-ing it with 1, bit-by-bit.

查看更多
孤傲高冷的网名
4楼-- · 2020-01-25 07:02

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.

More Info on Python's & operator

For more information, see: http://wiki.python.org/moin/BitwiseOperators

Why it Works to check Odd vs. Even

EDIT: Adding this section since this answer is getting some love

The reason why ANDing a value with 1 tells if the value is odd or even may not be obvious at first.

The binary representation of a number is essentially the sum of a series of YES or NO for each power of 2 moving leftward starting in the rightmost digit with 1, 2, 4, 8, ...

There is only one way to represent any number in this way. E.g. the number 13 (base 10) can be written in binary as "1101" (or hexadecimal as 0xD, but that's beside the point). See here:

    1   1   0   1
    x   x   x   x
    8   4   2   1
    =   =   =   =
    8 + 4 + 0 + 1  =  13

Notice that aside from the rightmost binary digit, all other 1 digits will add an even number (i.e. a multiple of 2) to the sum. So the only way to get an odd final sum is to add that odd 1 from the rightmost digit. So if we're curious if a number is odd or even, we can look at its binary representation and ignore everything except for the rightmost digit.

To do this, we use the bitwise AND operator. The value 1 in binary is expressed as 1:

    0   0   0   1
    x   x   x   x
    8   4   2   1
    =   =   =   =
    0 + 0 + 0 + 1  =  1

ANDing a value with 1 like this will result in 1 if the value's rightmost bit is set, and 0 if it is not.

And because 0 is generally considered "false" in most languages, and non-zero values considered "true", we can simply say as a shortcut:

if (value & 1): do_something_with_odd_value()...
查看更多
ら.Afraid
5楼-- · 2020-01-25 07:09
家丑人穷心不美
6楼-- · 2020-01-25 07:15

It's a bitwise operation, in this case assigning zero to oddeven if cardNumber has an even number of elements (and one otherwise).

As an example: suppose len(cardNumber) == 235. Then numdigits == 235, which is 0b11101011 in binary. Now 1 is '0b00000001' in binary, and when you "AND" them, bitwise, you'll get:

  11101011
  &
  00000001
  ----------
= 00000001

Similarly, if numdigits were 234, you would get:

  11101010
  &
  00000001
  ----------
= 00000000

So, it's basically a obfuscated way of checking if len(cardNumber) % 2. Probably written by someone with a C background, because it is not very pythonic - readability counts!

查看更多
登录 后发表回答