Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About operators > Using bitwise operators | |||
Bitwise operators internally manipulate floating-point numbers to change them into 32-bit integers. The exact operation performed depends on the operator, but all bitwise operations evaluate each binary digit (bit) of the 32-bit integer individually to compute a new value. For a list of bitwise shift operators, see About bitwise shift operators. For a list of bitwise logical operators, see About bitwise logical operators.
Using bitwise operators in Flash isn't very common, but can be useful in some circumstances. For example, you might want to build a permissions matrix for a Flash project, but you don't want to create separate variables for each type of permission. In this case, you might use bitwise operators.
The following example shows how you can use the bitwise OR operator with the Array.sort() method to specify sort options.
To use the bitwise OR operator:
var myArr:Array = new Array("Bob", "Dan", "doug", "bill", "Hank", "tom");
trace(myArr); // Bob,Dan,doug,bill,Hank,tom
myArr.sort(Array.CASEINSENSITIVE | Array.DESCENDING);
trace(myArr); // tom,Hank,doug,Dan,Bob,bill
The first line defines an array of random names and traces them to the Output panel. Then you call the Array.sort() method and specify two sort options using the constant values Array.CASEINSENSITIVE and Array.DESCENDING. The result of the sort method causes the items in the array to be sorted in reverse order (z to a). The search is case-insensitive; a and A are treated the same, instead of having a case-sensitive search where Z comes before a.
Bob,Dan,doug,bill,Hank,tom tom,Hank,doug,Dan,Bob,bill
There are five options available in the sort method:
There are three different ways you can define the sort options for an array:
my_array.sort(Array.CASEINSENSITIVE | Array.DESCENDING); // constants my_array.sort(1 | 2); // numbers my_array.sort(3); // adding the numbers
Although it might not be immediately obvious, the number values for the sort options are actually bitwise digits (binary or base 2). The constant value Array.CASEINSENSITIVE equals the numeric value of 1, which also happens to be the binary value of 1. The constant value Array.DECENDING has a numeric value of 2 or a binary value of 10.
Working with binary numbers can get confusing. Binary only has two possible values, 1 or 0, which is why the value 2 is represented as 10. If you want to display the number 3 in binary, it would be 11 (1+10). The number 4 represented in binary is 100, representing 5 in binary is 101, and so on.
The following ActionScript demonstrates how to sort an array of numeric values in descending order by using the bitwise AND operator to add the Array.DESCENDING and Array.NUMERIC constants together.
var scores:Array = new Array(100,40,20,202,1,198); trace(scores); // 100,40,20,202,1,198 trace(scores.sort()); // 1,100,198,20,202,40 var flags:Number = Array.NUMERIC|Array.DESCENDING; trace(flags); // 18 (base 10) trace(flags.toString(2)); // 10010 (binary -- base2) trace(scores.sort(flags)); // 202,198,100,40,20,1
|
|
|
|