David Mennenoh
David Mennenoh

Reputation: 290

Bitwise Not - Flash AS3?

I have a hex number: 0x01F4

I need the one's complement - which should be FE0B. But I can't seem to get what I need.

I do:

var n:int = 0xF4;
n = ~n;
trace(n.toString(16));

And get -F5 instead of 0B.

Anyone know what I can do?

Upvotes: 0

Views: 85

Answers (1)

Cameron
Cameron

Reputation: 98746

One's complement works on all the bits. In Flash, an int is 32 bits (or 8 hex digits). So ~0x01F4 is 0xFFFFFE0B (because 0x01F4 is 0x000001F4). If you want just the last 2 bytes' worth, simply do a mask after: ~n & 0xFFFF.

The reason you get -F5 as output is that int is a signed type -- so whenever the highest bit is set (as it is with your example), Flash thinks it's a negative 2's complement number and gives you that output. The bits are correct, but the formatted representation is unexpected because you're not asking for what you think you are. Change the type to uint and the output should become (the expected) FFFFFE0B.

Upvotes: 6

Related Questions