Thursday, May 23, 2013

My Stikky (23 May)

get the max of two integers without using if/else or < , > operators

int z = x ^ y;
int b = getB(z); // the highest diff-bit index between x and y
int sX = (x & z) >> b;
int sY = (y & z) >> b;
int max = 
(x * sX + y * sY + x * (sX ^ sY ^ 1)) & ~((x >> 31) ^ (y >> 31))
+
(((x >> 31) ^ (y >> 31))&1) * (((x >> 31)&1)*y + ((y >> 31)&1)*x);

No comments:

Post a Comment