Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
& vs %
#1
which is better:
    foo = bar % 256
or:
    foo = bar & 255
???

i just benchmarked them for speed.  the 2nd is slightly faster in python3.5.2.  in a loop of 1000000000 times, it was about 89.36 seconds for the 1st one to 81.53 seconds for the 2nd one, on my laptop.  is that enough reason to use the latter?  the code i will putting this in will probably run at most no more than 20 times a day.

i just tried them in python2.7.12 ... 60.12 seconds for the 1st vs. 49.55 seconds for the 2nd.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
my error, sorry
Reply
#3
The & is a logical bitwise and operation:

111111112 & 1000000002 == 0
8bit               9 bit

The modulus operator % returns the rest of an division. It's a different behavior as the bitwise and operation.
111111112 % 1000000002 == 111111112 == 25510
8 bit               9 bit                 8 bit

The question which is faster, does not help to gain performance win, because are giving different results.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
for this case, both do give the same result ... the low order 8 bits.  sure what is going on is different.  i was just wondering how much this might influence people's thought of "better".
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
If you want to get the lower 8 bits use the & operator. It's more clear what is happening in the code. I haven't seen getting the bits that way ( using modulo ). Also, changing the value of the bar above 255 and it will not be the same anymore.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
turns out, at least one architecture (IBM 370) is faster when using % than when using &.  i believe the CDC 7600 is the same way.  so, at least, when coding in C, it is probably better to use %.  in the case of Python, the interpreter could, in theory, understand what are doing, and do it the best way.  but that didn't seem to happen.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
You guys have an experience. I am using Python for my own needs for now. Programming is not my job. Python is almost the only language I can actually do something. My C knowledge is too insignificant. A little HTML and JS. If I see getting the bits by modulo operator I will not recognize what is happening. Honestly  Roll Eyes
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
try to understand base85 encoding,  you have to use % for that.  it's not bits.  i implement base85 about 3 decades ago in assembler and since then, also in C.  i don't need to implement it in Python as long as i use python3 since it already exists.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020