human
Compute the minimum or maximum of two integers without branching | absbit32 function.
max function.
min function.
Driver code"
Solution in Python:
 Write me a code to compute the minimum or maximum of two integers without branching and the other steps required?
assistant
#Here is code to compute the minimum or maximum of two integers without branching and the other steps required.
 def absbit32 ( x , y ) :
	sub = x - y
	mask = ( sub >> 31 )
	return ( sub ^ mask ) - mask
def max ( x , y ) :
	abs = absbit32 ( x , y )
	return ( x + y + abs ) // 2
def min ( x , y ) :
	abs = absbit32 ( x , y )
	return ( x + y - abs ) // 2
print ( max ( 2 , 3 ) )
print ( max ( 2 , - 3 ) )
print ( max ( - 2 , - 3 ) )
print ( min ( 2 , 3 ) )
print ( min ( 2 , - 3 ) )
print ( min ( - 2 , - 3 ) )