Be a Supporter!

AS: Binary and Bitwise operators

  • 2,037 Views
  • 14 Replies
New Topic Respond to this Topic
Claxor
Claxor
  • Member since: Oct. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
AS: Binary and Bitwise operators Jan. 1st, 2006 @ 06:48 PM Reply

AS: main

So, I didn't find any tutorial on binary numbers in AS: Main, so I thought I'd have a go. It's not really AS related, but heck, it's worth a shot =P. I'm using Flash MX 2004 for this, but it really shouldn't be a problem, as all the operators works on Flash 5 and above.

What is binary?
Binary is the way that computers sees numbers, in simply one's and zero's, giving it the advantage of being able to be represented by a number of booleans (or switches, in computers). An example of a binary number would be 100011, which represents 35.

The way we're used to see numbers is based by ten's. So the number 32604 consists of, from left to right, 3 x 10000, 2 x 1000, 6 x 100, 0 x 10, and 4 x 1. In other words 30000 + 2000 + 600 + 4 = 32604. But instead of saying 3 x 10000 and so on, it could be said as 3 x 10^4, and then 2 x 10^3 and so on, as it's ten based. Converting the number 32604 would look like this: 111111101011100. Representing, from left to right, 1 x 2^14, 1 x 2^13 and so on, as it's two based. So, taking on a number a bit smaller, like 9 in decimal (ten based), would be 1001 in binary (two based), consisting of 1 x 2^3 (8) and 1 x 2^0 (1), as in 8 + 1, that equals 9. If you like to have it visualised, .here's a picture to make it easier to understand.

You could think of it as a number of bowls, in which you either put one stone (1), or no stone(0). Each bowl would represent two times the previous bowl, so staying at the number 9, 1001 would be four bowls, the one most to the right would represent 1, the next 2 (1x2), the next 4 (2x2) and the last 8 (4x2).
Here's a converter, in which you can simply try a number of numbers, and see if it was the way you expected, if not, then look closer at the binary number, thinking of it as bowls. Hexadecimal numbers is based on 16 instead, using the numbers 0-9 and A-F, so the first bowl would be how many 1's (16^0), the next would be how many 16's (16^1), the next how many 256's (16^2), and so on.

So now, when you (hopefully) understand binary, on to the bitwise operators.

Bitwise operators
The two main bitwise operators are << (bitwise left shift) and >> (bitwise right shift). First, I'll explain the bitwise left shift operator, as it, in my opinion, is simpler to understand.

Bitwise left shift
First, an example. 8 << 10 equals 8192. You can try it in flash using
trace(8 << 10);
Now, explaining it. 8 in binary is, as you hopefully could figure out, 1000. So, left shifting it by 10 basicly means adding 10 zero's to the end of the binary number, and converting back to decimal, now having 13 zero's (2^13). Try trace(Math.pow(2, 13)); in flash, and you'll hopefully get 8192.

Taking a simpler example, 8 << 1, is the same as adding 1 zero at the end, so 1000 becomes 10000. And you have hopefully figured out that that will move the "stone", if you think in bowls, one bowl to the left. And as every bowl represents twice the value of the one before, 8 (1000) becomes 16 (10000).

That pretty much covers the left shift, on to the right.

Bitwise right shift
If you understood the left shift, this shouldn't be to hard to grasp. Instead of adding zero's at the end, you remove numbers at the end. So 8>>1, in other words, removing one number at the end of 1000, would be 100 in binary, and 4 in decimal. So far, so good. But if a one gets in the way, then what? Take 9 for example, 1001 in binary. if you do 9>>1, that would remove the first digit, which in this case is a 1 instead of a zero. So that also becomes 100 in binary, and 4 in decimal. Try it yourself, with this code:

trace(8>>1); // Outputs 4
trace(9>>1); // Also outputs 4

So 8>>10, in other words removing 10 digits from 1000, would, not too surprisingly, become 0.

There are two more bitwise operators, bitwise right shift and assignment and bitwise left shift and assignment, that pretty much is a shortcut. They are written like this: >>= and <<=. Looking at the example below should make things clear.

var a:Number = 8;
a<<=1;
trace(a); // Outputs 16

var a:Number = 8;
a>>=1;
trace(a); // Outputs 4

I'm pretty new to all the bitwise operators, so if I missed anything, or said anything wrong, just point it out. And also, you can ask any questions and I'll try to answer them =D


BBS Signature
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 06:55 PM Reply

Nice job - this had already been done briefly in Inglor's "Maths - Intermediate".

There are also >>> and <<< operators, I have no idea what they do exactly though. I've never even used the single shift ones.

Claxor
Claxor
  • Member since: Oct. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 06:57 PM Reply

At 1/1/06 06:55 PM, T-H wrote: Nice job - this had already been done briefly in Inglor's "Maths - Intermediate".

Oh, sorry, I didn't know :'(


There are also >>> and <<< operators, I have no idea what they do exactly though. I've never even used the single shift ones.

I've never used any either, it's just that I decided to get into them, after looking at a program about math on the tele =P, so I turned to the only one I can trust, Flash's help file. I'll go straight away and read about the <<< and >>> operators now =D


BBS Signature
Claxor
Claxor
  • Member since: Oct. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 07:09 PM Reply

Well, I've found that >>> is called bitwise unsigned right shift, but it really confuses me =/, and It's kinda late, so I can't think properly. But it doesn't seem to be that much of a use unless you are a pro mathmatician (sp?).


BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 07:13 PM Reply

At 1/1/06 06:55 PM, T-H wrote: Nice job - this had already been done briefly in Inglor's "Maths - Intermediate".

Not only breifly but 2 or 3 of us posted extensive information on his thread about it too so this is a useless AS topic


- Matt, Rustyarcade.com

Begoner
Begoner
  • Member since: Oct. 10, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 07:19 PM Reply

At 1/1/06 06:55 PM, T-H wrote: Nice job - this had already been done briefly in Inglor's "Maths - Intermediate".

There are also >>> and <<< operators, I have no idea what they do exactly though. I've never even used the single shift ones.

It's confusing. It doesn't preserve the sign of the original number, or something. For example:

32 >> 1 = 16
32 >>> 1 = 16
-32 >> 1 = -16
-32 >>> = 2147483632

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 07:22 PM Reply

At 1/1/06 07:19 PM, Begoner wrote: It's confusing. It doesn't preserve the sign of the original number, or something. For example:

exactly. the last binary digit is just a true or false for negativity. so an "unsigned" shift means a shift with no regards to the sign...


BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 07:49 PM Reply

first of all, binary is not "10's", its base 2, its composed of 0 and 1's like our decimal number system is composed of the number 0 through 9

the << and >> shift the the number to teh specified places left and right

a<<b = a*2^b; for example, 3<<2 = 3*2^2 = 12, 5<<4 = 5*2^4 = 80

a>>b = a*2^-b (or a/2^b); for example, 8>>1 = 8/2 = 4, 16>>4 = 16/16 = 1;

however, as >> and << only deal with integers, the remainder in a right shift is discarded for example

3>>1 = 3/2 = 1 (.5)
7>>2 = 7/4 = 1 (.75)

the >>> and <<< are unsigned binary left and right shift wheras >> and << are signed (however flash only has >>>)

a signed 32 bit integer takes values of -2^31 to 2^31-1
the primary bit determines whether the number is negative or positive, and the other 31 bits describe the number,
the method is called twos a pair (i think) as its the fastest physical way to use negative numbers such that -a = !a+1

so 23 as a signed 32 bit integer would be

0 0000000000000000000000000010111

and -23 = !23+1 = 1 1111111111111111111111111101000 + 1 = 1 1111111111111111111111111101001

workign with postive integers, >>> and <<< will act as >> and <<, but as its unsigned, if you try to use negative integers youll get strange results

in signed 32bit integers, 1 1111111111111111111111111101001 = -23
but as an unsigned32bit integer, 1 1111111111111111111111111101001 = 4294967273

(unsigned 32bit integers range from 0 to 2^32 and use all bits to describe the number)

so that if you do -23>>>1, youre not doing -23/2, but 4294967273/2

so that -23>>>1 = 2147483636 (.5)

the other bitwise operators

&

the & is a bitwise AND operation: a&b is true, when both a and b are true

01110
10111 & = 00110 for example, here, 14&23 = 6

|

the | is a bitwise OR operation: a|b is true, when either a or b are true

01110
10111 | = 11111 for example, here, 14|23 = 31

~

the ~ is a bitwise NOT (i used the ! sign above)

~01110 = 10001 for example, here, ~14 = 17

^

the ^ is a bitwise XOR operation: a^b is true, when either a or b are true, but not both

01110
10111 ^ = 11001 for example, here, 14^23 = 25


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 07:58 PM Reply

At 1/1/06 07:49 PM, -dELta- wrote: numbers and symbols

delta, I'm a bit worried about you. I think you need to get out more =)

I mean, saying "When you shift three two points left bitwise, the result equals three times two raised to two, which equals twelve" doesn't turn very many girls on. ;)

Just kidding, it does really

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 08:01 PM Reply

At 1/1/06 07:49 PM, -dELta- wrote: first of all, binary is not "10's", its base 2, its composed of 0 and 1's like our decimal number system is composed of the number 0 through 9

sorry i realised that you were talking about decimal when you mentioned 10's however later on you do use the term adding and removing 10's which is incorrect

in any system of base n, binary being base 2, decimal being base 10, hex being base 16, multiplying by 10 will always shift the number one place to the right

in binary, 1*10 = 10
in decimal, 5*10 = 50
in hex, 5a3*10 = 5a30

but you are not adding a 10

lol if you are working in binary, then 10*10 = 10+10 hahaha try giving that to youre maths teacher (in decimal, its 2*2 = 2+2)


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 08:02 PM Reply

At 1/1/06 07:58 PM, Rantzien wrote:
At 1/1/06 07:49 PM, -dELta- wrote: numbers and symbols
delta, I'm a bit worried about you. I think you need to get out more =)

I mean, saying "When you shift three two points left bitwise, the result equals three times two raised to two, which equals twelve" doesn't turn very many girls on. ;)

Just kidding, it does really

lol its hard to get out often when you live in the middle of nowhere, during school time i do go into town every saturday to meet up with friends though


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 1st, 2006 @ 08:15 PM Reply

At 1/1/06 08:02 PM, -dELta- wrote: lol its hard to get out often when you live in the middle of nowhere, during school time i do go into town every saturday to meet up with friends though

I used to live in the middle of nowhere too, so I know what it's like. Live in my own apartment now though, just at my parents' for the holiday.


BBS Signature
Claxor
Claxor
  • Member since: Oct. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Binary and Bitwise operators Jan. 2nd, 2006 @ 12:01 PM Reply

At 1/1/06 08:01 PM, -dELta- wrote: sorry i realised that you were talking about decimal when you mentioned 10's however later on you do use the term adding and removing 10's which is incorrect

Did I say that? Please quote the part I did it in


BBS Signature
Madferit
Madferit
  • Member since: Jul. 29, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Binary and Bitwise operators Jun. 20th, 2006 @ 01:30 PM Reply

At 1/1/06 08:02 PM, -dELta- wrote:
lol its hard to get out often when you live in the middle of nowhere, during school time i do go into town every saturday to meet up with friends though

You bring your laptop in to town? :)

<3333 Delta

Chandlerklebs
Chandlerklebs
  • Member since: Jul. 10, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Binary and Bitwise operators Jul. 27th, 2006 @ 07:46 PM Reply

"I mean, saying "When you shift three two points left bitwise, the result equals three times two raised to two, which equals twelve" doesn't turn very many girls on. ;)"

I'll have 2 remember that. How do I find a girl that understands binary?
I recently taught binary 2 a deaf boy.