r/badcode • • May 11 '22

python It works but.... NSFW

Post image
472 Upvotes

57 comments sorted by

View all comments

Show parent comment

2

u/MrJankles May 30 '22

Easier than the solution I was thinking of

player = (player+1) % 2

1

u/EvilStevilTheKenevil May 30 '22

This also works, but the modulus operator involves division. If the compiler doesn't recognize %2 as logically equivalent to a bitwise operation on the least significant bit then this might slow things down.

 

Multiplication is as simple as several adders chained together with some bit-shifting for their inputs, and you could somewhat easily implement this algorithm entirely in hardware such that any two 64 bit ints could be multiplied together in a single cycle (floats would be slightly more complicated, but more or less the same).

Meanwhile, division is very complicated (relative to the other 3 fundamental arithmetic operations you learn in grade school, anyway), and there are many different algorithms out there which can get the job done. The naive approaches converge linearly, and some of the more interesting ones do it quadratically, but in any case division is likely to be a multi-cycle operation. In other words, slow.

1

u/LuckyNumber-Bot May 30 '22

All the numbers in your comment added up to 69. Congrats!

  2
+ 64
+ 3
= 69

Click here to have me scan all your future comments.
Summon me on specific comments with u/LuckyNumber-Bot.