User loginTags in TagsTags for Making friends with binary representation |
Making friends with binary representationThis post is inspired by Nick’s aversion to bitwise operators all over the place. Despite this glaring personality flaw, he’s still worth hiring. Many developers, and unfortunately almost every Flash developer, are far too comfortable with their abstract data types like For this article (and every article), the terms
Put simply, in performance-critical code, you don’t want to use The bitshiftWith an The number 573 isn’t particularly friendly, and most of you probably can’t multiply or divide it in your head. However, you could multiply it by 10 if I asked you to — you’d immediately respond with 5,730. You could also divde it by 10 — you’d respond with 57. In base-10 math, shifting the digits left is the same as multiplying by 10, and shifting the digits right is the same as dividing by 10. It works similarly in binary base-2 math — shifting left is multiplying by 2, and shifting right divides by 2. What’s less obvious is that, in a computer, a shift is a ridiculously easy operation to perform — significantly easier than a multiply and ridiculously easier than a divide. They’re something you should use whenever appropriate. Nick thinks they clutter your code with unreadable nonsense, to which I always reply, “well, you should learn to read code.” :) The bitwise logical operatorsYou’ve probably used the logical operators AND and OR ( An For example, lets say you have an ARGB color. You want to extract the green value. In an ARGB color, each of the alpha, the red, the green, and the blue channels take up 8 bits. We can use a bitwise AND to zero-out everything but the green portion, and then use a bitshift to move it to the position where it’s readable by a human. function getGreenPart(color:uint):uint { // For sake of example, let's say color is 0x3377AA const greenMask:uint = 0x00FF00; // Just get the green parts -- everything else in the int is binary 0s, so they'll AND to false, giving binary 0s in the result var greenByItself = color & greenMask; // Now greenByItself is 0x007700 var greenWithoutPadding = greenByItself >> 8; // Now greenWithoutPadding is 0x000077. return greenWithoutPadding; // We can display this in an editable field and it will make sense. } You can also use bitwise operators to store a bunch of class Ingredients { // Bitfields are easy in hex -- just double the digit until it wraps around. public static const ROAST_BEEF:uint = 0x01; public static const SWISS_CHEESE:uint = 0x02; public static const LETTUCE:uint = 0x04; public static const MAYO:uint = 0x08; public static const SALT:uint = 0x10; public static const PEPPER:uint = 0x20; public static const JALAPENOS:uint = 0x40; // Some convenience entries public static const EVERYTHING:uint = 0xFFFF; // Has all ingredients, even future ones public static const NOTHING:uint = 0x0000; // Just the bread -- we make good bread, some people buy it plain } class Sandwich { public var ingredients:uint = Ingredients.NOTHING; public function Sandwich(ingredients:uint) { this.ingredients = ingredients; } } // Now we can start making sandwiches var justBeefSandwich = new Sandwich(Ingredients.ROAST_BEEF); var veggieSandwich = new Sandwich(Ingredients.LETTUCE | Ingredients.JALAPENOS); var myFavoriteSandwich = new Sandwich(Ingredients.ROAST_BEEF | Ingredients.LETTUCE | Ingredients.SALT); var theWorksSandwich = new Sandwich(Ingredients.EVERYTHING & !Ingredients.JALAPENOS); // It's for my mom, she can't take the spicy // We can also detect which ingredients a sandwich has function calculatePrice(sandwich:Sandwich):Number { var price:Number = 1.50; // Start with a little cost, for the bread. if (sandwich.ingredients & Ingredients.ROAST_BEEF) price += 3.00; if (sandwich.ingredients & Ingredients.SWISS_CHEESE) price += 2.00; if (sandwich.ingredients & Ingredients.JALAPENOS) price += 0.50; return price; } We can use a bitwise-OR to combine two flags into a single field, and we can chain them together to add as many ingredients as we want. We can use a bitwise-AND plus the NOT operator ( Anyway, this is getting longish, so I’ll leave it at this. You should at least appreciate that bitwise operations exist now, and hopefully you can find a use for them in your next project.
|
Recent blog posts
Recent comments
|
Yeah I should make more use
Yeah I should make more use of this in my projects as well. I will come back to this post for review for sure as I forget this stuff all the time. Hey, it saves you the time explaining it to me again! Good thinkin’! ;-)
Nice article :) Although, for
Nice article :) Although, for the record, I don’t have an aversion to bitwise operations. Especially in situations where they make sense, like the examples you just posted. I was just saying that they make your code harder for noobs to read! Perhaps that’s actually a secret benefit, it keeps them from messin with your stuff.
Duly corrected. Man, I wish I
Duly corrected. Man, I wish I could get n00bs to stop messing with my stuff here… though in fairness, it’s usually me messing with the noobs’ stuff, and them IMing me frantically later wondering why stuff looks funny now.
Post new comment