Compute the minimum or maximum of two integers without branching | javascript implementation of above approach ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver code
var CHAR_BIT = 4 ; var INT_BIT = 8 ; function min ( x , y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( INT_BIT * CHAR_BIT - 1 ) ) ) ; } function max ( x , y ) { return x - ( ( x - y ) & ( ( x - y ) >> ( INT_BIT * CHAR_BIT - 1 ) ) ) ; } var x = 15 ; var y = 6 ; document . write ( " " + x + " " + y + " " + min ( x , y ) + " " ) ; document . write ( " " + x + " " + y + " " + max ( x , y ) ) ;