10-30-2006, 12:25 AM
|
Former Classic Staff
|
|
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
|
|
Bitwise confusion
-1 >> 1 returns -1 instead of 2147483647.
Meanwhile, 1 << 31 returns -2147483648.
I'm trying to make a more efficient function for returning sign.
In a mathematical sense, you can return val!=0?val/abs(val):0
in a piecewise sense, you can do:
if (val > 0)
return 1;
if (val < 0)
return -1;
return 0;
But really, all I need to do is test the value of the highest bit.
Maybe a getSign function wouldn't be a bad idea because the bitwise ops don't preserve sign consistently.
Also,
i = 1;
i <<= 1;
is giving me i as 0 instead of 2.
However,
i = 1;
i = i << 1;
works. |
Last edited by jake13jake; 10-30-2006 at 12:53 AM..
|
|