Mind Your ints and uints

ActionScript 3.0

ActionScript 3.0 introduces new data types galore, and two of the funniest looking are easily int and uint.  For one, they belong to an ultra-elite group whose names are fully lowercase (the third is void).  For two … well, I mean … uint.  Come on.  ;)

What are these data types, and what are they good for?  Let’s take a look. 

The words and numbers

The words themselves are abbreviations of “integer” and “unsigned integer,” so that’s at least a start.  Contrasted with the familiar Number data type, which can be an integer or fractional number, like 6.26, int can only be an integer.  What about uint, then?  What does the unsigned part mean?  This gets a little interesting, actually.

Technically, the Number data type is an “IEEE-754 double-precision floating-point number” (see the ActionScript 3.0 Language and Components Reference’s Number entry for full detail).

A number typed as Number can take up as much as 53 bits of memory (over 6.5 bytes), ranging all the way from 1.79e+308 down to 5e-324.  To put this in perspective, one million — a number which, in dollar bills, would make any of us happy — is 1e+6.  That means 1.79e+308 is a 1 with 308 digits after it, most of which are zeros.  This is an insanely huge range!  It effortlessly blows away the measly six zeroes of a million.

On the other hand, int is a 32-bit number (only 4 bytes), ranging from 2,147,483,647 down to -2,147,483,648.  That’s a range of over 4 billion positive and negative integers, which seems plenty good to me.  In all my years of working with Flash, I haven’t yet asked it to count to two billion in either direction.  If you’re doing a lot of number crunching in AS3, you can save a bit of memory by using ints instead of Numbers, if integers are all you need.

So … unsigned integers.  The word “sign” in this context refers to the implied “+” in front of positive integers or the “-” in front of negative ones.  Unsigned integers don’t have a sign, and in Flash that means they’re positive — always.  uint numbers range from 0 to 4,294,967,295 — the same range as ints, just shoved completely into the positive half of the number line.  When might you use a uint in Flash?  Well, any time you want to shave bits off your number … but only for integers, and specifically only for positive integers.

A “gotcha”

A friend of mine, Rothrock, recently pointed out an interesting quality of uints that he noted on the Adobe support forums.  Here’s a quick illustration via code:

var n:uint = 0;
trace(n);

What would you expect to see in the Output panel?  Zero?  You betcha.  The variable n, typed as a uint, is set to 0, and 0 it is.  Now let’s subtract 1:

n -= 1;
trace(n);

What will the Output panel show this time?  Negative one?  No way!  uints can’t be negative.  What you get is 4294967295, which means the imaginary pointer has jumped from the left edge of the uint number line to the right edge.

Why is this good to know?  Well, if you happen to be using a uint in a for loop, and you happen to be counting down rather than up …

for (var i:uint = 0; i > -10; i--) {
  // code here
}

… that i > -10 expression will never evaluate to falsei will always be greater than -10, so this for loop will go on looping forever.

The moral of the story?  Don’t use uints to count backwards — unless you want to be counting for a very long time.  ;)

Leave a Reply