Skip to content

Commit 3f01f59

Browse files
authored
Update mult.py: branchless, compact
1 parent 11da0b7 commit 3f01f59

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

mult.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
def mult(x, y):
22
z = 0
33
while y > 0:
4-
z = z + x
5-
y = y - 1
4+
z += x
5+
y -= 1
66
return z
77

88

99
def russian_peasant(x, y):
1010
z = 0
1111
while y > 0:
12-
if y % 2 == 1: z = z + x
13-
x = x << 1
14-
y = y >> 1
15-
return z
12+
z += x * (y & 1)
13+
x <<= 1
14+
y >>= 1
15+
return z

0 commit comments

Comments
 (0)