What does "& 0x7fffffff" mean in "int(time.time()*1000.0) & 0x7FFFFFFF"

3

I have the following value

start = int(time.time()*1000.0) & 0x7FFFFFFF

What is the purpose of the & 0x7FFFFFFF?

python

2 Answers

7

It's a bitmask. In low-level computation, it's an efficient way to clear out bits of register. In this case, the mask has all bits of a 32 bit integer set, except the signed bit. The signed bit is the bit that determines if the number is positive or negative. ANDing (&) with this mask effectively sets the signed bit to 0, which means the number will always be positive.

a && b is True when both a and b are True.
a & b is 1 when both a and b are 1, for each binary digit in a and b.

Python has support for binary literals, with the 0b prefix. Here are some 3-bit numbers being anded together.

>>> 0b101 & 0b110 == 0b100
True
>>> 0b011 & 0b111 == 0b011
True
>>> 0b011 & 0b110 == 0b010
True
answered on Stack Overflow Oct 7, 2017 by Filip Haglund
2

0x7FFFFFFF is a number in hexadecimal (2,147,483,647 in decimal) that represents the maximum positive value for a 32-bit signed binary integer.

The & symbol is a bitwise operator, more specifically the and operator.

Let's call the value 0x7FFFFFFF 'A' and the expression int(time.time()*1000.0) 'B'.

When you do 'A' & 'B', each bit of the output is 1 if the corresponding bit of A AND of B is 1, otherwise it's 0.

answered on Stack Overflow Oct 7, 2017 by Clayton A. Alves

User contributions licensed under CC BY-SA 3.0