Friday, September 12, 2008

Arithmetic in AS3

What is wrong in the following ActionScript3 code. It tries to
keep a (bit) vector for high-order and low-order bits of a two-byte
number. Assume somearray is an array of two-byte numbers.

var a:Array = new Array(256), b:Array = new Array(256);
for each (var n:uint in somearray) {
a[n/256] = 1;
b[n%256] = 1;
}

1 comment:

Kundan Singh said...

Unlike C/C++ or Java where integer division results in an integer, in AS3 the integer division can result in a floating point number (Number).

Secondly, an Array can be used as an Object (hash-table) hence even "a" is an Array, you can set a[1.2].

Thus, if "n" is 0x0304, then n%256 is 4 but n/256 is 3.0156 hence instead of setting a[3] it sets a[3.0156].