MCN Professionals | Interview Question of the day
To receive one interview question in your mailbox daily, please subscribe to "A Question A Day" at MCN Professionals home page.-----------------------------------------------------------------------------------------------------------------------------------------
Today's Question:
Given 2 unsigned integers, write expressions to find the maximum and minimum of these 2 integers without using either if-else or conditional operator.
Solution:
If the integers are a and b, as given below then the max & min can be computed like below:
unsigned int a;
unsigned int b;
int min = b ^ ( (a^b) & -(a<b) ); // Computing minimum
int max = a ^ ( (a^b) & -(a<b) ); // Computing maximum
-----------------------------------------------------------------------------------------
Interview Questions Archive:
To see all the questions in the category click
here...