-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathp16_7.py
More file actions
30 lines (23 loc) · 690 Bytes
/
p16_7.py
File metadata and controls
30 lines (23 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def sign(a: int):
return (a >> 32) & 0b1
def max_unconditional(a, b):
diff = a - b
both_negate = (sign(a)) & (sign(b))
sign_flag = sign(diff) ^ (not both_negate)
# print(f"Are both {a} and {b} negative {both_negate:b},\t"
# f"with signs {sign(a)} and {sign(b)}\t"
# f"Sign flag is {sign_flag}")
return ((not sign_flag) * a) | ((sign_flag) * b)
if __name__ == "__main__":
exs = [
(10, 20),
(20, 10),
(-10, -20),
(20, -10),
(-20, -10),
(0, 10),
(-20, 0),
]
for a, b in exs:
print(f"Max of {a} and {b} is:\t"
f"{max_unconditional(a,b)} == {max(a,b)}")