Computer Science, asked by andrewkevin1234567, 5 months ago

Write a program that finds the maximum of two integers using a method and displays the result.​

Answers

Answered by samairasharma026
3

Answer:

Compute the minimum or maximum of two integers without branching

Last Updated: 25-07-2019

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

filter_none

edit

play_arrow

brightness_4

/* The obvious approach to find minimum (involves branching) */

int min(int x, int y)

{

  return (x < y) ? x : y

}

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

Similar questions