Posted on by Kalkicode
Code Mathematics

Find the minimum and maximum of integers without using branching

Find the minimum and maximum values of two integers without branching involves computing the sign of the difference between the two integers using bitwise operations, and then using that sign to compute the minimum and maximum values.

Here's how the code works:

int max = a - ((a - b) & ((a - b) >> 31));
int min = b + ((a - b) & ((a - b) >> 31));
  1. Compute the difference between a and b, which will be positive if a is greater than b and negative otherwise.

    a - b
  2. Shift the difference right by 31 bits, which will give a number whose binary representation is all 0's if the difference is non-negative (i.e., a is greater than or equal to b) and all 1's otherwise.

    (a - b) >> 31
  3. Apply a bitwise AND operation to the difference and the result of the shift, which will give you either the difference (if a is greater than or equal to b) or 0 (if b is greater than a).

    (a - b) & ((a - b) >> 31)
    
  4. Compute the maximum value by subtracting the result of step 3 from a. If a is greater than or equal to b, then the difference is positive and the maximum value is a - (a - b), which simplifies to b. If b is greater than a, then the difference is negative and the maximum value is a - 0, which simplifies to a.

    a - ((a - b) & ((a - b) >> 31))
    
  5. Compute the minimum value by adding the result of step 3 to b. If a is greater than or equal to b, then the difference is positive and the minimum value is b + (a - b), which simplifies to a. If b is greater than a, then the difference is negative and the minimum value is b + 0, which simplifies to b.

    b + ((a - b) & ((a - b) >> 31))
    

Code Solution

// C program for 
// Find the minimum and maximum of integers without using branching
#include <stdio.h>

void minAndMax(int a, int b)
{
	// Find largest 
	int max = a - ((a - b) & ((a - b) >> 31));
	// Find smallest
	int min = b + ((a - b) & ((a - b) >> 31));
	// Display given numbers
	printf(" Given number a : %d  b : %d \n", a, b);
	// Display the calculated result
	printf(" Minimum : %d\n", min);
	printf(" Maximum : %d\n", max);
}
int main(int argc, char
	const *argv[])
{
	// Test cases
	minAndMax(23, 2);
	minAndMax(-5, -3);
	minAndMax(52, 234);
	minAndMax(-1, -4);
	return 0;
}

input

 Given number a : 23  b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5  b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52  b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1  b : -4
 Minimum : -4
 Maximum : -1
/*
  Java Program for 
  Find the minimum and maximum of integers without using branching
*/
public class Compare
{
	public void minAndMax(int a, int b)
	{
		// Find largest 
		int max = a - ((a - b) & ((a - b) >> 31));
		// Find smallest
		int min = b + ((a - b) & ((a - b) >> 31));
		// Display given numbers
		System.out.println(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		System.out.println(" Minimum : " + min);
		System.out.println(" Maximum : " + max);
	}
	public static void main(String[] args)
	{
		Compare task = new Compare();
		// Test Cases
		task.minAndMax(23, 2);
		task.minAndMax(-5, -3);
		task.minAndMax(52, 234);
		task.minAndMax(-1, -4);
	}
}

input

 Given number a : 23 b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5 b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52 b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1 b : -4
 Minimum : -4
 Maximum : -1
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for 
  Find the minimum and maximum of integers without using branching
*/
class Compare
{
	public: void minAndMax(int a, int b)
	{
		// Find largest 
		int max = a - ((a - b) &((a - b) >> 31));
		// Find smallest
		int min = b + ((a - b) &((a - b) >> 31));
		// Display given numbers
		cout << " Given number a : " << a << " b : " << b << endl;
		// Display the calculated largest number
		cout << " Minimum : " << min << endl;
		cout << " Maximum : " << max << endl;
	}
};
int main()
{
	Compare *task = new Compare();
	// Test Cases
	task->minAndMax(23, 2);
	task->minAndMax(-5, -3);
	task->minAndMax(52, 234);
	task->minAndMax(-1, -4);
	return 0;
}

input

 Given number a : 23 b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5 b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52 b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1 b : -4
 Minimum : -4
 Maximum : -1
// Include namespace system
using System;
/*
  Csharp Program for 
  Find the minimum and maximum of integers without using branching
*/
public class Compare
{
	public void minAndMax(int a, int b)
	{
		// Find largest 
		int max = a - ((a - b) & ((a - b) >> 31));
		// Find smallest
		int min = b + ((a - b) & ((a - b) >> 31));
		// Display given numbers
		Console.WriteLine(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		Console.WriteLine(" Minimum : " + min);
		Console.WriteLine(" Maximum : " + max);
	}
	public static void Main(String[] args)
	{
		Compare task = new Compare();
		// Test Cases
		task.minAndMax(23, 2);
		task.minAndMax(-5, -3);
		task.minAndMax(52, 234);
		task.minAndMax(-1, -4);
	}
}

input

 Given number a : 23 b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5 b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52 b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1 b : -4
 Minimum : -4
 Maximum : -1
<?php
/*
  Php Program for 
  Find the minimum and maximum of integers without using branching
*/
class Compare
{
	public	function minAndMax($a, $b)
	{
		// Find largest 
		$max = $a - (($a - $b) & (($a - $b) >> 31));
		// Find smallest
		$min = $b + (($a - $b) & (($a - $b) >> 31));
		// Display given numbers
		echo " Given number a : ".$a.
		" b : ".$b.
		"\n";
		// Display the calculated largest number
		echo " Minimum : ".$min.
		"\n";
		echo " Maximum : ".$max.
		"\n";
	}
}

function main()
{
	$task = new Compare();
	// Test Cases
	$task->minAndMax(23, 2);
	$task->minAndMax(-5, -3);
	$task->minAndMax(52, 234);
	$task->minAndMax(-1, -4);
}
main();

input

 Given number a : 23 b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5 b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52 b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1 b : -4
 Minimum : -4
 Maximum : -1
/*
  Node JS Program for 
  Find the minimum and maximum of integers without using branching
*/
class Compare
{
	minAndMax(a, b)
	{
		// Find largest 
		var max = a - ((a - b) & ((a - b) >> 31));
		// Find smallest
		var min = b + ((a - b) & ((a - b) >> 31));
		// Display given numbers
		console.log(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		console.log(" Minimum : " + min);
		console.log(" Maximum : " + max);
	}
}

function main()
{
	var task = new Compare();
	// Test Cases
	task.minAndMax(23, 2);
	task.minAndMax(-5, -3);
	task.minAndMax(52, 234);
	task.minAndMax(-1, -4);
}
main();

input

 Given number a : 23 b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5 b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52 b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1 b : -4
 Minimum : -4
 Maximum : -1
#  Python 3 Program for 
#  Find the minimum and maximum of integers without using branching
class Compare :
	def minAndMax(self, a, b) :
		max = a - ((a - b) & ((a - b) >> 31))
		min = b + ((a - b) & ((a - b) >> 31))
		#  Display given numbers
		print(" Given number a : ", a ," b : ", b)
		#  Display the calculated largest number
		print(" Minimum : ", min)
		print(" Maximum : ", max)
	

def main() :
	task = Compare()
	#  Test Cases
	task.minAndMax(23, 2)
	task.minAndMax(-5, -3)
	task.minAndMax(52, 234)
	task.minAndMax(-1, -4)

if __name__ == "__main__": main()

input

 Given number a :  23  b :  2
 Minimum :  2
 Maximum :  23
 Given number a :  -5  b :  -3
 Minimum :  -5
 Maximum :  -3
 Given number a :  52  b :  234
 Minimum :  52
 Maximum :  234
 Given number a :  -1  b :  -4
 Minimum :  -4
 Maximum :  -1
#  Ruby Program for 
#  Find the minimum and maximum of integers without using branching
class Compare 
	def minAndMax(a, b) 
		#  Find largest 
		max = a - ((a - b) & ((a - b) >> 31))
		#  Find smallest
		min = b + ((a - b) & ((a - b) >> 31))
		#  Display given numbers
		print(" Given number a : ", a ," b : ", b, "\n")
		#  Display the calculated largest number
		print(" Minimum : ", min, "\n")
		print(" Maximum : ", max, "\n")
	end

end

def main() 
	task = Compare.new()
	#  Test Cases
	task.minAndMax(23, 2)
	task.minAndMax(-5, -3)
	task.minAndMax(52, 234)
	task.minAndMax(-1, -4)
end

main()

input

 Given number a : 23 b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5 b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52 b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1 b : -4
 Minimum : -4
 Maximum : -1
/*
  Scala Program for 
  Find the minimum and maximum of integers without using branching
*/
class Compare()
{
	def minAndMax(a: Int, b: Int): Unit = {
		// Find largest 
		var max: Int = a - ((a - b) & ((a - b) >> 31));
		// Find smallest
		var min: Int = b + ((a - b) & ((a - b) >> 31));
		// Display given numbers
		println(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		println(" Minimum : " + min);
		println(" Maximum : " + max);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Compare = new Compare();
		// Test Cases
		task.minAndMax(23, 2);
		task.minAndMax(-5, -3);
		task.minAndMax(52, 234);
		task.minAndMax(-1, -4);
	}
}

input

 Given number a : 23 b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5 b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52 b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1 b : -4
 Minimum : -4
 Maximum : -1
/*
  Swift 4 Program for 
  Find the minimum and maximum of integers without using branching
*/
class Compare
{
	func minAndMax(_ a: Int, _ b: Int)
	{
		// Find largest 
		let max: Int = a - ((a - b) & ((a - b) >> 31));
		// Find smallest
		let min: Int = b + ((a - b) & ((a - b) >> 31));
		// Display given numbers
		print(" Given number a : ", a ," b : ", b);
		// Display the calculated largest number
		print(" Minimum : ", min);
		print(" Maximum : ", max);
	}
}
func main()
{
	let task: Compare = Compare();
	// Test Cases
	task.minAndMax(23, 2);
	task.minAndMax(-5, -3);
	task.minAndMax(52, 234);
	task.minAndMax(-1, -4);
}
main();

input

 Given number a :  23  b :  2
 Minimum :  2
 Maximum :  23
 Given number a :  -5  b :  -3
 Minimum :  -5
 Maximum :  -3
 Given number a :  52  b :  234
 Minimum :  52
 Maximum :  234
 Given number a :  -1  b :  -4
 Minimum :  -4
 Maximum :  -1
/*
  Kotlin Program for 
  Find the minimum and maximum of integers without using branching
*/
class Compare
{
	fun minAndMax(a: Int, b: Int): Unit
	{
		// Find largest 
		val max: Int = a - ((a - b) and((a - b) shr 31));
		// Find smallest
		val min: Int = b + ((a - b) and((a - b) shr 31));
		// Display given numbers
		println(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		println(" Minimum : " + min);
		println(" Maximum : " + max);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Compare = Compare();
	// Test Cases
	task.minAndMax(23, 2);
	task.minAndMax(-5, -3);
	task.minAndMax(52, 234);
	task.minAndMax(-1, -4);
}

input

 Given number a : 23 b : 2
 Minimum : 2
 Maximum : 23
 Given number a : -5 b : -3
 Minimum : -5
 Maximum : -3
 Given number a : 52 b : 234
 Minimum : 52
 Maximum : 234
 Given number a : -1 b : -4
 Minimum : -4
 Maximum : -1

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment