Using bit fields in ActionScript 3
Update: I just confirmed with the player team that a Boolean actually takes up 4 bytes.
I’ve been doing a lot of research lately on different uses for ByteArrays. This is in preparation for my ByteArrays for Beginners session that I will be doing at FITC Toronto. While in Amsterdam recently, Thibault Imbert of bytearray.org mentioned how he uses bit fields in WiiFlash to make boolean values as small as possible. Basically when the WiiFlash server sends over the state of the Wiimote buttons to Flash, it sends over a single byte where each bit represents the state of a particular button.
So why would you want to do this? Well if you have eight boolean values it would take up 32 bytes of space if you use the native Boolean class, as each one takes up 4 bytes. But in reality that is 32 times as big as it needs to be. If you set each bit of a single byte to either 1 or 0 based on your boolean values it will only take up a single byte. Now this is not going to be worth it for most situations, but if you need really high-speed binary communication than it will be more efficient. Below is an example of how you could pack eight booleans into a single byte:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var ba:ByteArray = new ByteArray(); var bf:uint = 0; bf = 1<<7; //Set bit 8 to true bf |= 1<<6; //Set bit 7 to true bf |= 0<<5; //Set bit 6 to false bf |= 1<<4; //Set bit 5 to true bf |= 1<<3; //Set bit 4 to true bf |= 1<<2; //Set bit 3 to true bf |= 1<<1; //Set bit 2 to true bf |= 1; //Set bit 1 to true ba.writeByte(bf); trace(ba[0].toString(2)); //11011111 |
In the above code all of the boolean values are set to true except for bit number 6. This way of setting individual bits is probably the easiest to read, at least for me. Now when you want to retrieve the value of an individual bit you can simply do an AND on the byte like I do below.
1 | if(ba[0]&32) trace("Bit 6 is true"); //Won't trace anything |
The reason I chose the number 32 is that it is the perfect binary mask to retrieve the 6th bit of the byte. In binary the number 32 is represented as 00100000. I will go over this more during my session and will also do a tutorial on it soon.
Lee
New tutorial on 3D photo panels
| I just uploaded a new tutorial that shows how to make a cool 3D photo panel. This type of 3D effect is very common and is a good skill to have in your toolbox. When you click on an image it scales up into focus. This tutorial also serves a dual purpose as being a good introduction to using TweenLite. The tutorial uses the new 3D features of FP 10 rather than a library like Papervision3D. Click the image below to see the result. |
Lee
Want full microphone access in Flash?
Of course you do. This is a request that I hear quite often when I’m out traveling around. Let me first clarify what I mean by full microphone access. This would be the ability to get at the raw bytes coming over the microphone in a similar way that we can now get the raw bytes of a sound file. This would enable all kind of cool applications using voice control, sound manipulation, etc. There is of course the gratuitous spectrum display for microphone input.
The reason I bring this is up is that the guys over at Ribbit have started a new site at getmicrophone.com which serves a dual purpose. First of all, it is a place to share ideas and examples relating to the microphone in Flash. Secondly, it is similar to the Make Some Noise campaign in that it is trying to get us (Adobe) to add in the full microphone support. They want you to record a video telling us that you want it. So if you’ve ever wanted this type of low-level access to the microphone, then go over and record a response. My response is below.
Lee
Pixel Bender is NOT hardware accelerated
| After reading various tweets this morning coming out of the MIX09 conference I thought I needed to go on the record regarding Pixel Bender. The Pixel Bender toolkit runs filters using hardware acceleration. But Pixel Bender filters that are running inside of Flash Player 10 are not accelerated in any way and are instead rendered via software. This is a failure on our part for not communicating this clearly enough. |
Lee
Are you coming to Toronto?
![]() |
If you’re not then maybe I can convince you. I am of course talking about FITC Toronto which is coming up at the end of April. I highly recommend this conference as it is looking to be another great FITC event. The dates for the event are April 25-28 and is being held at the Hilton in downtown Toronto. I can vouch that this is a very nice hotel and the venue is great. The speaker lineup is also great and contains some hidden gems like Golan Levin. Here are some of the things that I will be doing during the conference: |
The Dead Drop
For those who have been following the dead drops that I have been doing, I will be doing the next one during FITC Toronto. I have some new tricks up my sleeve for this drop and hopefully Toronto will represent. It will again consist of clues located both in the physical and online space. So get some like-minded folks together, as teams have a big advantage.
What’s New in Flash CS4?
I will be doing an all-day intensive workshop on the new features in Flash CS4. I gave a similar workshop in Amsterdam which went very well. This will be a full day where you will be able to learn everything to get you up-to-speed with the new version. There may also be a hint about the above-mentioned dead drop.
ByteArrays for Beginners
This is a session I will be giving during the main conference where I will introduce people to the power of the ByteArray class. Once you master low-level programming in Flash you can literally do whatever you want. Upset that Flash doesn’t support a certain file type? No problem, just write its support yourself. Many examples of real-world uses will be shown and will be a great introduction to byte-level programming in Flash.
Lee









