PDA

View Full Version : What are Bitwise operators and how/why do you use them?


bape
11-19-2004, 05:45 AM
Hi there, i came across these things called bitwise operators. After some research i didnt get any further than the fact that they alter bits directly.
Then i read something on shifting bits and stuff, and that this would be more efficient it this got me confused.
Can you use them to optimize your code?
Can someone give some examples of this, like a function and his alternative (using bitwise operators)?

icio
11-19-2004, 12:29 PM
check out the reference in flash

senocular
11-19-2004, 12:38 PM
They can help speed up some operations in other languages but Im pretty sure they wont help in Flash.

Basically they are best used with binary values. If you aren't using binary values (like flags) then they aren't that much use.

λ
11-19-2004, 02:05 PM
Hi there, i came across these things called bitwise operators. After some research i didnt get any further than the fact that they alter bits directly.

Then i read something on shifting bits and stuff, and that this would be more efficient it this got me confused.

Can you use them to optimize your code?

Can someone give some examples of this, like a function and his alternative (using bitwise operators)?

Bitwise operators are kind of advanced - they work on the binary bits of numbers. There are quite a few - for the examples, I'll use 56 (00110001 in binary) and 235 (11101011 in binary).

First, you have bitwise AND(&). This compares each bit. If the bits from both of the numbers are 1, then the resulting bit is 1. Otherwise, the resulting bit is 0. For example:


56 00111000
235 11101011
&-------
40 00101000



Next, you have bitwise OR(|). If either bit is 1, then the resulting bit is 1.

56 00111000
235 11101011
|-------
251 11111011

Also, you have bitwise XOR(^). If only one of the bits is 1, then the resulting bit is 1 (if both are 1, or neither are 1, then the resulting bit is 0).

56 00111000
235 11101011
^-------
211 11010011



The final bitwise operator is bitwise NOT (~). This works on only one operand - it simply flips the bits.

56 00111000
~-------
199 11000111




235 11101011
~-------
20 00010100



(Note: XOR might not work correctly in practice because of something called "the signed bit". If the variable can accept negative values, then XORing a number will turn it into -(number+1)).

EDIT: As sen said (before I did, gah, you're far too quick ;)), you'll have little use for them in Flash. However, if you ever learn Java or C, then understanding the basics is very useful indeed :)

pom
11-19-2004, 09:16 PM
They can be useful when you work with colors for instance:this.createEmptyMovieClip ("dot", 0).lineStyle (50, 0, 100) ;
dot.lineTo (.45, .15) ;
col = new Color (dot) ;
this.onEnterFrame = function () {
red = 127 * (1 + Math.cos (r+=.03)) ;
green = 127 * (1 + Math.cos (g+=.06)) ;
blue = 127 * (1 + Math.cos (b+=.02)) ;
var myColor = red << 16 | green << 8 | blue ;
col.setRGB (myColor) ;
} ;And I remember there was an interesting thread about on bit's forum, but it's down at the moment :(

tofuisonmyside
12-31-2004, 08:07 AM
maybe this is off topic, but i have to compare strings of numbers like
this and find the most similar amongst a list:

for example
the number is 401000
the list :[100000,110000,101000,200000,210000,201000,300000,3 10000,400000,410000]


in order to find the "closest" string of 401000 in the list, could bitwise ops be useful and how ?

pom
12-31-2004, 08:35 AM
No they wouldn't. Well, maybe they would, it's quite easy without, so...

yunnek
04-16-2007, 12:42 AM
No they wouldn't. Well, maybe they would, it's quite easy without, so...

can bitwise operator be used in comparing two characters? like a<b? instead of using "<" can i use bitwise operator?

mooska
04-16-2007, 02:16 AM
EDIT: As sen said (before I did, gah, you're far too quick ), you'll have little use for them in Flash. However, if you ever learn Java or C, then understanding the basics is very useful indeedIn as3 theres a ByteArray class, and binary sockets, this makes it very usefull, or even the basics.

Some practice example:
if you want to read a number (uint) from ByteArray, that is 3 bytes long, and you want to use readInt, wich reads 4 bytes, you may want to cut last byte

myInt = myInt & 0x00ffffff

With << and >> theres a problem with sign bit, like you said about xor, it may create wholly different numbers.

Dazzer
04-16-2007, 04:00 AM
I've seen it used in HSV - RGB conversion algorithms for flash. But that's it really lol