Hello all,
wasn’t sure which section this would be best in. I’ve been trying to get the hang of bitwise operators and how to use them in as3. So far i have really liked the stuff you can do with it and wish i’d learnt it ages ago. One thing that is really puzzling me though is negative binaries.
for example:
00001100 is 12
11110011 is -13?
I have come up with an explanation that makes sense in my brain, although i’m not sure it’s the real reason.
0001 = 1
0000 = 0
1111 = -1
1110 = -2
So because 1111 is already -1 rather than (-)0 all the minus numbers need to have an additional 1 subtracted from them to get the value, assuming you use the 8,4,2,1 method to work them out.
However, i have just read that there are other methods for showing negative binaries.
10001100 = -12 because of the first bit.
11110011 = -12 with one method.
11110011 = -13 with the method described above.
I am becoming more and more confused. If anyone can explain this that would hugely appreciated. Basically I would like to know
a) Which method is most commonly used, and more importantly which should i use to help my understanding in actionscript 3?
b) How the hell are you supposed to know when a number is negative or not. Like if you are using 4 bit binaries how do you know if 1100 is supposed to be 12 or -13?
Any help from you brainboxes that can clear this up?
Cheers
Simple google search:
http://www.allaboutcircuits.com/vol_4/chpt_2/3.html
zero 0000
positive one 0001 negative one 1111
positive two 0010 negative two 1110
positive three 0011 negative three 1101
positive four 0100 negative four 1100
positive five 0101 negative five 1011
positive six 0110 negative six 1010
positive seven 0111 negative seven 1001
. negative eight 1000
a) twos-complementary is the commonest method and is the one used in AS.
b) There’s no way in hell of telling, unless you know which of the methods commonly used to express negative binary numbers is actually being used, as they each produce different results.
There are three common methods:
- Signed magnitude. This simply appends an additional 0 or 1 to the left of the number to indicate positive or negative. So, as you say, in an 8 bit model, 00001100 would equal 12. To convert it into -12, you would add a left-most bit thus, 100001100.
- Ones-complementary reverses the bits. So 00001100 would still be 12 while 111110011 would be -12. Note that there’s still an additional left-most bit added to indicate that the bits have been flipped.
- Twos-complementary follows the same technique as ones-complementary, but adds a 1 to the result, rather than dumping it into the left-most bit. So 12 would be 00001100 which is flipped to arrive at its complement, like so 11110011. Add 1 to this number and you get 11110110 which is -12.
edit Haha…ignore my drivel once again and check out the link in the previous post. It confirms what I’ve said but with the added bonus that it uses longer words.
01001001 00100000 01100100 01101111 00100000 01101110 01101111 01110100 00100000 01110101 01101110 01100100 01100101 01110010 01110011 01110100 01100001 01101110 01100100 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00101110 .
01010100 01101000 01100001 01110100 01110011 00100000 01110111 01101000 01100001 01110100 00100000 01110100 01110010 01100001 01101110 01110011 01101100 01100001 01110100 01101111 01110010 01110011 00100000 01100001 01110010 01100101 00100000 01100110 01101111 01110010 00101110
Thanks guys.
@temp: Yes i had seen that, but it didn’t really answer the question of which to use in AS.
@glos: Thanks, that pretty much covers what i needed to know. Most importantly that there is no way in hell of knowing which polarity the number is.
@dan: I would, if i was more bored, try and figure out if that was a real code. But i think i’ll assume it is something very clever and leave it at that!
You can usually tell by the operator you’re applying. If you check out the bitwise operators in the Flash manual, it will tell you whether the result is ones or twos-complementary and how negative numbers are treated. It would appear (certainly from the AS2 manual) that all negative numbers are both twos-complementary and signed magnitude:
“The return value is interpreted as a two’s complement number with sign, so the return is an integer in the range -2147483648 to 2147483647”
Negative binary is sort of a misnomer, as it implies there is a -1 and a -0 (bi-), which the 2s compliment method set out to fix (the -0 part). It’s just a means of representing negative numerical values using binary.