betabits

I went down a few algorithmic roads recently, digging into path finding and – for some obscure reasons – bit manipulations. Or byte. Whatever.

Along this way some utility methods (or functions) were born, and I thought: May be some day some of them may be in use to any of you ;)

For my dear non-geeky readers: A bit is the smallest part in software. It’s either this or that, either 0 or 1, either false or true. With a group of 2 bits you already have 4 states: 00, 01, 10 and 11. With 8 it’s 256 and so on (2^n).

As it would be too boring to just type 0 or 1, and because we have more than 2 fingers, man invented numbers to accumulate these bits: so 9 stands for 1001, and because 9 is shorter than 1001, we prefer 9. Some even write AB for 10101011, but that’s where we come back to geeky world.

So after this highly informative introduction, let’s get to some code. First, let’s count bits:

static public function countBits( value : uint ) : uint {
	var count:uint = 0;
	while (value) {
		if ( value & 1 ) {
			count++;
		}
		value >>>= 1;
	}
	return count;
}

Example:
countBits( 0xAB ) -> 5

Now sometimes you might wanna know: Does this data contain no more than 1 bit? We could just ask countBits( value ) == 1. But that’s not as speedy as it should be, right? So here we go:

static public function is_1_bit( value : uint ) : Boolean {
	var count:uint = 0;
	while (value) {
		if ( value & 1 ) {
			if (count == 1) return false;
			count++;
		}
		value >>>= 1;
	}
	return count == 1;
}

Examples:
is_1_bit( 0xAB ) -> false
is_1_bit( 0×400 ) -> true

uint are by the way 32bit data, so a maximum of 32 of these bits we’re talking about can be turned on or off. That’s a lot of data. 4′294′967′296 combinations (though not that high compared to the numbers we read every day in the newspapers recently). Anyway, sometimes we might wanna access and set only a group of bits (usually 4 or 8) within this quite large row of bits:

static public function getBitGroup( value : uint , group : uint , len : uint = 4 ) : uint {
	return ( value >> (group*len) ) % (1 << len);
}
 
static public function setBitGroup( value : uint , groupValue :uint , group : uint , len : uint = 4 ) : uint {
	var pos:uint = group * len;
	var mask:uint = n_bits(pos);
	var right_bits:uint = value & mask;
	value >>>= pos + len;
	value <<= len;
	value |= groupValue;
	value <<= pos;
	value |= right_bits;
	return value;
}

Don’t they look just groovy?! Yeah baby!

Anyway, that’s all for now. Stay tuned for some crazy path finding. If I find time (sometimes I wonder how all those bloggers find their time to write so much..) Not to mention Twitter. Boohaa.

Yesterday I had a presentation at the SFUG meeting covering some bits of my rewritten BBML framework (originated from the project laax.com). I’ve tried to share some insights into the technical concept and strategies for CSS parsing, CSS selectors and layout validation.

Sev presenting...
Picture by Marc Liyanage

It’s been surprisingly fun (I give credits to the beer sponsored by Nemos). People even managed to pretend they’d be interested in what I was prosing, so credit to them too!

Flash at the lake Swiss Flash User Group Conference. I’d also like to mention that there’ll be the swiss flash event soon: Flash at the Lake will not only pamper you with appearances of great national and international Flash enthustiacs, it will also give anybody attending the pleasure to enjoy one of Zurich’s greatest locations with people who don’t think of you as a storm lightning adorer when you sit in the sun twittering Flash into the clear lake. And all that to a fantastic price. Check it out at fatl.ch

So here we go with the presentation (Quicktime so you can enjoy the marvelous effects):

This is a follow-up of this.

metatunnel-pixelbender

Yeah well, I was more than optimistic to show those JS guys how fast Flash can be with the help of some brand new Adobe magic – but Pixel Bender was, unfortunately, quite disappointing:
(more…)

I did a quick port of a «graphic demo» called «metatunnel» (created by FRequency).

Paulo Falcão ported this to Javascript using canvas.

To make the set complete I ported Paulos JS version to Actionscript, just quick’n'dirty.

Click on it to start the animation:

The Flash plugin is required to view this object.

(more…)

GridFitType has a great impact on Text rendering:

The Flash plugin is required to view this object.

Well not really much to say here:

  • NONE Good for animation and people who prefer Font appearance over readability
  • SUBPIXEL Good compromise between readability (small sizes) and appearance
  • PIXEL Pure nonsense if you ask me

What I really wonder though is: Why on earth can’t I adjust fractioned text sizes (aka float)??? In the IDE, yes you can. By actionscript, no you can’t. I don’t get it.. anybody knows why that is? And more importantly: is there some fancy workaround? (in the example I’m just scaling the text fields, but that’s not really a cool solution).

I don’t discover as many bugs nowadays as I’ve used to in the old days when I was beta testing for Macromedia. But it happened today, and I’ve just installed the newest Flash Player 10.0.22.87 to be sure.

It happens to DisplayObjects A inside DisplayObjectContainers B inside DisplayObjectContainer C, when

  • A was not initially visible (not inside initial scroll rect of C)
  • A is in 3D mode (I just change rotationY for that)
  • B is in «cached as Bitmap» (cacheAsBitmap would do, I go with DropShadowFilter in the example)
  • C’s scrollrect property is set, so A is shows up (well, it doesn’t – that’s the bug after all ;)

Here the example:

bug-3d-shadow.swf
(more…)

Update: I’ve found an easier way without FlashCommand

Once you got used to developing for Flash in Flex Builder, you hate to do any programming in Flash IDE. Still you sometimes might have to: Flex Builder won’t allow to publish into a FLA file. (There are of course many other reasons, like supporting older AS1/AS2 projects, we don’t go into that here.) Thanks to Eclipse’s ability to be customized, there are ways to make things at least a little easier. I give here a little overview of how I’ve set up my environment, based on several helpful resources I’ve found in the web.

  1. Install FlashCommand
  2. Install Ant

And for each project:

  1. Create/modify Ant file
  2. Create Actionscript project
  3. Set up project and FLA file

(more…)

Adobe today released Alchemy, a SDK to compile C/C++ code into AVM2 Bytecode.

This has been sneak peaked a while ago (see Video below) – now it’s here. And I’m amazed!! Think of the possiblities: XSLT interpreter (as demoed in the vid), 3D speed, super fast parsing, physics, AI – everything that’s CPU intensive and won’t need any platform specific stuff.

So far this C code still looks pretty fucked up to me (see examples at the labs), but hell, I’m sure that one day we’re gonna see some real fancy stuff thanks to this. I’m looking forward!

Sometimes you want to pass an objects function as an argument and store that function (along its arguments) for later calling. Still you don’t want that storage to prevent its object (instance) from being destroyed (from being removed by the garbage collector). But storing a function does so, because it holds a reference to the function which belongs to an object.

It’s one of the biggest traps you can fall into when developing a larger project where many objects are created and destroyed and created and so on… geeks call that kind of thing memory leaks I think :)

So if you can avoid it: avoid it! If you can’t.. may be this little thing can help:
(more…)

I did a lot of string parsing in the recent time: CSS Selectors, XML Display Objects, Stylesheets, … I also need XML selection from String expressions – I formerly (AS2) used the great XPath4AS2 from XFactorStudio which did it’s job well (though a bit slow, it’s AS2 after all).

There’s also one for Actionscript 3 (xpath-as3). But.. well… I wanted to go for some real speed! I like XPath a lot, but we now have native E4X selection in Actionscript 3, quite a different concept of node selection, and the conversion of XPath to E4X obviously results in quite a compromise in performance.

So all I need is a decent E4X parser. And hey, I found one! E4XParser from Digital Primates. It does its job really well, especially considering the very compact code it consists of. Thanks to some preparsing and caching, it’s also quite fast.

Still I thought I can do better :-) So I planted myself for a day (and a night) in front of my displays and hacked the hell out of it. The result is a little library which does pretty much the same thing as E4XParser, though pretty much more and a little faster too (15% to 50%). It’s about half as fast as the native E4X selection (once parsed). You can do nearly anything you can do with E4X. Use it like this:

 

import com.betabong.xml.e4x.E4X;
var result : XMLList = E4X.evaluate( xmllist , "author.( name.@last == 'Jobs' )" );
 
// E4X.evaluate( source : XMLList , expression : String ) : XMLList

 
If your source is XML, just do XMLList( xml ), if your result should be xml, do xml = result[0]

Test it here

Restrictions: You can’t use AND/OR in comparisions. So, this won’t go: author.( name.@first == ‘Steve’ && name.@last == ‘Jobs’ ) – though this is only a real limitations for OR. do this for AND: author.( name.@first == ‘Steve’ ).( name.@last == ‘Jobs’ ).

What you can do: Yes, you can do quite advanced stuff like author.( name.@first == name.@last ) or car.@rating.average() (one of the few proprietary functions I added). Or even

*..car.( @brand.toLowerCase() == 'volvo' ).( parent().( localName() == 'group' ).@rating > @rating )

– a weird example, I admit, but fancy, ain’t it? :-)

This is the first time ever I’m releasing part of my library as Open Source (MIT licence). As soon as I’ll find some time (and if I see any interest), I’m gonna put this into Google Code, so everybody can easily checkout and participate. Until then download it from here:

Download (zip 13kb)