Find the maximum of two numbers. You should not use if-else or any other comparison operator. I found this question on online bulletin board, so i thought i should ask in StackOverflow
EXAMPLE Input: 5, 10 Output: 10
I found this solution, can someone help me understand these lines of code
int getMax(int a, int b) {
int c = a - b;
int k = (c >> 31) & 0x1;
int max = a - k * c;
return max;
}
Guess we can just multiply the numbers with their bitwise comparisons eg:
static int mymax(int a, int b)
If b > a then (a-b) will be negative, sign will return -1, by adding 1 we get index 0 which is b, if b=a then a-b will be 0, +1 will give 1 index so it does not matter if we are returning a or b, when a > b then a-b will be positive and sign will return 1, adding 1 we get index 2 where a is stored.
There is one way
and one
The code which I am providing is for finding maximum between two numbers, the numbers can be of any data type(integer, floating). If the input numbers are equal then the function returns the number.
Description
The sign bit is saved as the Most Significant Bit(MSB) in the memory. If MSB is 1 and vice-versa. To check if MSB is 1 or 0 we shift the MSB to the LSB position and Bitwise & with 1, if the result is 1 then the number is -ve else no. is +ve. This result is obtained by the statement:
int_diff >> (sizeof(int) * 8 - 1 ) & 1
Here to get the sign bit from the MSB to LSB we right shift it to k-1 bits(where k is the number of bits needed to save an integer number in the memory which depends on the type of system). Here k= sizeof(int) * 8 as sizeof() gives the number of bytes needed to save an integer to get no. of bits, we multiply it with 8. After the right shift, we apply the bitwise & with 1 to get the result.
Now after obtaining the result(let us assume it as r) as 1(for -ve diff) and 0(for +ve diff) we multiply the result with the difference of the two numbers, the logic is given as follows:
Now there are two remaining points 1. the use of while loop and 2. why I have used the variable 'int_diff' as an integer. To answer these properly we have to understand some points:
The simplest answer is below.
Let's dissect this. This first line seems like it out to be straightforward - it stores the difference of
a
andb
. This value is negative ifa < b
and is nonnegative otherwise. There's actually a bug here - if the difference of the numbersa
andb
is so big that it can't fit into an integer, this will lead to undefined behavior - oops! So let's assume that doesn't happen here.In the next line, which is
the idea is to check if the value of
c
is negative. In virtually all modern computers, numbers are stored in a format called two's complement in which the highest bit of the number is 0 if the number is positive and 1 if the number is negative. Moreover, most ints are 32 bits.(c >> 31)
shifts the number down 31 bits, leaving the highest bit of the number in the spot for the lowest bit. The next step of taking this number and ANDing it with 1 (whose binary representation is 0 everywhere except the last bit) erases all the higher bits and just gives you the lowest bit. Since the lowest bit ofc >> 31
is the highest bit ofc
, this reads the highest bit ofc
as either 0 or 1. Since the highest bit is 1 iffc
is 1, this is a way of checking whetherc
is negative (1) or positive (0). Combining this reasoning with the above,k
is 1 ifa < b
and is 0 otherwise.The final step is to do this:
If
a < b
, thenk == 1
andk * c = c = a - b
, and soWhich is the correct max, since
a < b
. Otherwise, ifa >= b
, thenk == 0
andWhich is also the correct max.