PDA

View Full Version : Bitwise confusion


jake13jake
10-30-2006, 12:25 AM
-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.

Yen
10-30-2006, 04:41 AM
I'm pretty sure <<= isn't a valid operator. >_>

Admins
10-30-2006, 03:29 PM
There was a bug with the shiftleft-assign, it was actually doing a shiftright-assign. Thanks for reporting, a new version of the npcserver will be uploaded soon.
Need to check the bit-operation to improve the compatibility with C-code.