Maximum of two number in C using bitwise operators -


possible duplicate:
find maximum of 2 numbers without using if-else or other comparison operator

isgreater:

if x > y  return 1, else return 0  

example:

  • isgreater(4,5) = 0
  • isgreater(5,4) = 1

legal operators: ! ~ & ^ | + << >>

isgreater function..

i tried:

int isgreater(int x, int y) {     return (y+(~x+1)) >> 31 & 1; } 

but not working.. :(( let me know else can do?

given x, y

try x + -y if < 0 y greater, if > 0 x greater.

-y = binary complement of y:

-y = (~(y-1)) <==> -y = (~y)+1 

from see, binary complement (~y +1)), same.

then bitshift >> 31 msb, , equal 1.
make sure set parantheses, operator precedence !

 (y+-x) >> (31 & 1); !=  ((y+-x) >> 31) & 1; 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -