View Full Version : Bitwise manipulation with negatives

01-14-2004, 07:39 PM
I've been working on reading a certain encoding from a game's password system. I've successfully been able to make a script to output code, but now I'm making a script to read it as well.

Here's the interesting part. Not only does it use different bits in a 0 to 15 hex variable, but some bits are negative.

+1 = Event 1
-2 = Event 2
+4 = Event 3
8 = Event 4

The variable is 13 (D) by default, so I tried this:

X-=13 // now we can get the offsets of it's modification
if(X&-4){alert('event 1')}
if(X&2){alert('event 2')} // works
if(X&-1){alert('event 3')}

Let's say X was -4, then for some reason all 3 would fire as true. Is there a legal use of using negative integers with bitwise manipulation? Or should I multiply by -1 if a variable is less than 0?