Compute the minimum or maximum of two integers without branching | absbit32 function ; max function ; min function ; Driver code
def absbit32 ( x , y ) : NEW_LINE INDENT sub = x - y NEW_LINE mask = ( sub >> 31 ) NEW_LINE return ( sub ^ mask ) - mask NEW_LINE DEDENT def max ( x , y ) : NEW_LINE INDENT abs = absbit32 ( x , y ) NEW_LINE return ( x + y + abs ) // 2 NEW_LINE DEDENT def min ( x , y ) : NEW_LINE INDENT abs = absbit32 ( x , y ) NEW_LINE return ( x + y - abs ) // 2 NEW_LINE DEDENT print ( max ( 2 , 3 ) ) NEW_LINE print ( max ( 2 , - 3 ) ) NEW_LINE print ( max ( - 2 , - 3 ) ) NEW_LINE print ( min ( 2 , 3 ) ) NEW_LINE print ( min ( 2 , - 3 ) ) NEW_LINE print ( min ( - 2 , - 3 ) ) NEW_LINE