Find the minimum and maximum of integers without using branching

Here given code implementation process.

// 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


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







© 2021, kalkicode.com, All rights reserved