X or Y
This problem was asked by Facebook.
Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, using only mathematical or bit operations. You can assume b can only be 1 or 0.
My Solution(Python):
def x_or_y(x, y, b):
return x*b + y*(1-b)
if __name__=='__main__':
print(x_or_y(5, 8, 1))
print(x_or_y(5, 8, 0))