int a,b
Math.max(a,b)
Input: 5,10
Output: 10
1. 5<10 X, can not have <
2. 5 -10 =-5 <0 X, can not have <
3. 5 - 10 = -5 = -101 => 010 + 1 = 011 => 1111 1111 .....1011
Negative means the most significant bit is 1
4. 5-10 = -5 ==> 5 - (-5) = 10-> Max
=== =======> a - sigBit*(a-b)
5. E.g., 10,5 ===> 10 - 0*(10-5) = 10
E.g., 5, 10===> 5 - 1*(5-10) = 10
2. Implementation
public int getMax(int a , int b) { int c = a-b; int sigBit = (c >> 31) & 0x1; int max = a - sigBit*c; return max; }3. Similar ones
No comments:
Post a Comment