Compute the minimum or maximum of two integers without branching | absbit32 function ; max function ; min function ; Driver code
function absbit32 ( x , y ) { var sub = x - y ; var mask = ( sub >> 31 ) ; return ( sub ^ mask ) - mask ; } function max ( x , y ) { var abs = absbit32 ( x , y ) ; return ( x + y + abs ) / 2 ; } function min ( x , y ) { var abs = absbit32 ( x , y ) ; return ( x + y - abs ) / 2 ; } document . write ( max ( 2 , 3 ) + " " ) ; document . write ( max ( 2 , - 3 ) + " " ) ; document . write ( max ( - 2 , - 3 ) + " " ) ; document . write ( min ( 2 , 3 ) + " " ) ; document . write ( min ( 2 , - 3 ) + " " ) ; document . write ( min ( - 2 , - 3 ) ) ;