# Compute the minimum or maximum of two integers without branching

On some rare machines where branching is expensive, the below obvious approach to find minimum can be slow as it uses branching.

`/* The obvious approach to find minimum (involves branching) */` `int` `min(` `int` `x, ` `int` `y) ` `{ ` ` ` `return` `(x < y) ? x : y ` `} ` |

*chevron_right*

*filter_none*

Below are the methods to get minimum(or maximum) without using branching. Typically, the obvious approach is best, though.

**Method 1(Use XOR and comparison operator)**

Minimum of x and y will be

y ^ ((x ^ y) & -(x < y))

It works because if x < y, then -(x = y, then -(x < y) will be all zeros, so r = y ^ ((x ^ y) & 0) = y. On some machines, evaluating (x < y) as 0 or 1 requires a branch instruction, so there may be no advantage.

To find the maximum, use

x ^ ((x ^ y) & -(x < y));

## C++

`// C++ program to Compute the minimum ` `// or maximum of two integers without ` `// branching ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `class` `gfg ` `{ ` ` ` ` ` `/*Function to find minimum of x and y*/` ` ` `public` `: ` ` ` `int` `min(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `return` `y ^ ((x ^ y) & -(x < y)); ` ` ` `} ` ` ` ` ` `/*Function to find maximum of x and y*/` ` ` `int` `max(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `return` `x ^ ((x ^ y) & -(x < y)); ` ` ` `} ` ` ` `}; ` ` ` ` ` `/* Driver code */` ` ` `int` `main() ` ` ` `{ ` ` ` `gfg g; ` ` ` `int` `x = 15; ` ` ` `int` `y = 6; ` ` ` `cout << ` `"Minimum of "` `<< x << ` ` ` `" and "` `<< y << ` `" is "` `; ` ` ` `cout << g. min(x, y); ` ` ` `cout << ` `"\nMaximum of "` `<< x << ` ` ` `" and "` `<< y << ` `" is "` `; ` ` ` `cout << g.max(x, y); ` ` ` `getchar` `(); ` ` ` `} ` ` ` `// This code is contributed by SoM15242 ` |

*chevron_right*

*filter_none*

## C

`// C program to Compute the minimum ` `// or maximum of two integers without ` `// branching ` `#include<stdio.h> ` ` ` `/*Function to find minimum of x and y*/` `int` `min(` `int` `x, ` `int` `y) ` `{ ` `return` `y ^ ((x ^ y) & -(x < y)); ` `} ` ` ` `/*Function to find maximum of x and y*/` `int` `max(` `int` `x, ` `int` `y) ` `{ ` `return` `x ^ ((x ^ y) & -(x < y)); ` `} ` ` ` `/* Driver program to test above functions */` `int` `main() ` `{ ` `int` `x = 15; ` `int` `y = 6; ` `printf` `(` `"Minimum of %d and %d is "` `, x, y); ` `printf` `(` `"%d"` `, min(x, y)); ` `printf` `(` `"\nMaximum of %d and %d is "` `, x, y); ` `printf` `(` `"%d"` `, max(x, y)); ` `getchar` `(); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Compute the minimum ` `// or maximum of two integers without ` `// branching ` `public` `class` `AWS { ` ` ` ` ` `/*Function to find minimum of x and y*/` ` ` `static` `int` `min(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `return` `y ^ ((x ^ y) & -(x << y)); ` ` ` `} ` ` ` ` ` `/*Function to find maximum of x and y*/` ` ` `static` `int` `max(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `return` `x ^ ((x ^ y) & -(x << y)); ` ` ` `} ` ` ` ` ` `/* Driver program to test above functions */` ` ` `public` `static` `void` `main(String[] args) { ` ` ` ` ` `int` `x = ` `15` `; ` ` ` `int` `y = ` `6` `; ` ` ` `System.out.print(` `"Minimum of "` `+x+` `" and "` `+y+` `" is "` `); ` ` ` `System.out.println(min(x, y)); ` ` ` `System.out.print(` `"Maximum of "` `+x+` `" and "` `+y+` `" is "` `); ` ` ` `System.out.println( max(x, y)); ` ` ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to Compute the minimum ` `# or maximum of two integers without ` `# branching ` ` ` `# Function to find minimum of x and y ` ` ` `def` `min` `(x, y): ` ` ` ` ` `return` `y ^ ((x ^ y) & ` `-` `(x < y)) ` ` ` ` ` `# Function to find maximum of x and y ` `def` `max` `(x, y): ` ` ` ` ` `return` `x ^ ((x ^ y) & ` `-` `(x < y)) ` ` ` ` ` `# Driver program to test above functions ` `x ` `=` `15` `y ` `=` `6` `print` `(` `"Minimum of"` `, x, ` `"and"` `, y, ` `"is"` `, end` `=` `" "` `) ` `print` `(` `min` `(x, y)) ` `print` `(` `"Maximum of"` `, x, ` `"and"` `, y, ` `"is"` `, end` `=` `" "` `) ` `print` `(` `max` `(x, y)) ` ` ` `# This code is contributed ` `# by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`using` `System; ` ` ` `// C# program to Compute the minimum ` `// or maximum of two integers without ` `// branching ` `public` `class` `AWS ` `{ ` ` ` ` ` `/*Function to find minimum of x and y*/` ` ` `public` `static` `int` `min(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `return` `y ^ ((x ^ y) & -(x << y)); ` ` ` `} ` ` ` ` ` `/*Function to find maximum of x and y*/` ` ` `public` `static` `int` `max(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `return` `x ^ ((x ^ y) & -(x << y)); ` ` ` `} ` ` ` ` ` `/* Driver program to test above functions */` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` ` ` `int` `x = 15; ` ` ` `int` `y = 6; ` ` ` `Console.Write(` `"Minimum of "` `+ x + ` `" and "` `+ y + ` `" is "` `); ` ` ` `Console.WriteLine(min(x, y)); ` ` ` `Console.Write(` `"Maximum of "` `+ x + ` `" and "` `+ y + ` `" is "` `); ` ` ` `Console.WriteLine(max(x, y)); ` ` ` `} ` ` ` `} ` ` ` ` ` `// This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to Compute the minimum ` `// or maximum of two integers without ` `// branching ` ` ` `// Function to find minimum ` `// of x and y ` `function` `m_in(` `$x` `, ` `$y` `) ` `{ ` ` ` `return` `$y` `^ ((` `$x` `^ ` `$y` `) & ` ` ` `- (` `$x` `< ` `$y` `)); ` `} ` ` ` `// Function to find maximum ` `// of x and y ` `function` `m_ax(` `$x` `, ` `$y` `) ` `{ ` ` ` `return` `$x` `^ ((` `$x` `^ ` `$y` `) & ` ` ` `- (` `$x` `< ` `$y` `)); ` `} ` ` ` `// Driver Code ` `$x` `= 15; ` `$y` `= 6; ` `echo` `"Minimum of"` `,` `" "` `, ` `$x` `,` `" "` `,` `"and"` `, ` ` ` `" "` `,` `$y` `,` `" "` `,` `" is "` `,` `" "` `; ` ` ` `echo` `m_in(` `$x` `, ` `$y` `); ` ` ` `echo` `"\nMaximum of"` `,` `" "` `,` `$x` `,` `" "` `, ` ` ` `"and"` `,` `" "` `,` `$y` `,` `" "` `, ` `" is "` `; ` ` ` `echo` `m_ax(` `$x` `, ` `$y` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` ` ` |

*chevron_right*

*filter_none*

**Output:**

Minimum of 15 and 6 is 6 Maximum of 15 and 6 is 15

**Method 2(Use subtraction and shift)**

If we know that

INT_MIN <= (x - y) <= INT_MAX

, then we can use the following, which are faster because (x – y) only needs to be evaluated once.

Minimum of x and y will be

y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

This method shifts the subtraction of x and y by 31 (if size of integer is 32). If (x-y) is smaller than 0, then (x -y)>>31 will be 1. If (x-y) is greater than or equal to 0, then (x -y)>>31 will be 0.

So if x >= y, we get minimum as y + (x-y)&0 which is y.

If x < y, we get minimum as y + (x-y)&1 which is x.

Similarly, to find the maximum use

x - ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)))

`#include<stdio.h> ` `#define CHAR_BIT 8 ` ` ` `/*Function to find minimum of x and y*/` `int` `min(` `int` `x, ` `int` `y) ` `{ ` ` ` `return` `y + ((x - y) & ((x - y) >> ` ` ` `(` `sizeof` `(` `int` `) * CHAR_BIT - 1))); ` `} ` ` ` `/*Function to find maximum of x and y*/` `int` `max(` `int` `x, ` `int` `y) ` `{ ` ` ` `return` `x - ((x - y) & ((x - y) >> ` ` ` `(` `sizeof` `(` `int` `) * CHAR_BIT - 1))); ` `} ` ` ` `/* Driver program to test above functions */` `int` `main() ` `{ ` ` ` `int` `x = 15; ` ` ` `int` `y = 6; ` ` ` `printf` `(` `"Minimum of %d and %d is "` `, x, y); ` ` ` `printf` `(` `"%d"` `, min(x, y)); ` ` ` `printf` `(` `"\nMaximum of %d and %d is "` `, x, y); ` ` ` `printf` `(` `"%d"` `, max(x, y)); ` ` ` `getchar` `(); ` `} ` |

*chevron_right*

*filter_none*

Note that the 1989 ANSI C specification doesn’t specify the result of signed right-shift, so above method is not portable. If exceptions are thrown on overflows, then the values of x and y should be unsigned or cast to unsigned for the subtractions to avoid unnecessarily throwing an exception, however the right-shift needs a signed operand to produce all one bits when negative, so cast to signed there.

**Source:**

http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax

## Recommended Posts:

- Compute the integer absolute value (abs) without branching
- Find maximum XOR of given integer in a stream of integers
- Minimum number of elements to be removed to make XOR maximum
- Compute the parity of a number using XOR and table look-up
- Compute modulus division by a power-of-2-number
- How to sum two integers without using arithmetic operators in C/C++?
- Hamming distance between two Integers
- Compare two integers without using any Comparison operator
- Smallest of three integers without comparison operators
- Bitwise recursive addition of two integers
- Detect if two integers have opposite signs
- Check if given four integers (or sides) make rectangle
- Divide two integers without using multiplication, division and mod operator
- Number of mismatching bits in the binary representation of two integers
- Find the missing element in an array of integers represented in binary format