Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

I have the following value

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

What is the purpose of the & 0x7FFFFFFF?

like image 891
SeeDerekEngineer Avatar asked Oct 07 '17 22:10

SeeDerekEngineer


People also ask

What does the fox say Meaning?

The idea for“The Fox (What Does the Fox Say?)” was for it not to be hit. Instead, the brothers were going to use the footage of the song on their show in Norway to prove what absolute failures they were in the music business.

What Does the Fox Say Ylvis wiki?

"The Fox (What Does the Fox Say?)" is an electronic dance novelty song and viral video by Norwegian comedy duo Ylvis. The top trending video of 2013 on YouTube, "The Fox" was posted on the platform on 3 September 2013, and has received over 1 billion views as of September 2022.


2 Answers

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
like image 54
Filip Haglund Avatar answered Sep 21 '22 19:09

Filip Haglund


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.

like image 32
Clayton A. Alves Avatar answered Sep 21 '22 19:09

Clayton A. Alves