 |
|
| View previous topic :: View next topic |
| Author |
Message |
ZaxisNet Administrator

Joined: 01 Aug 2002 Posts: 276 Location: Toronto, ON
|
Posted: Tue Oct 14, 2003 1:31 pm Post subject: TUTORIAL: A little Bitwise-er |
|
|
Tutorial: A little bitwise-er
Author: Thomas Schemmer (Zaxisnet - Creative Media)
Date: Oct 14, 2003
+ Preface
Basics before complexity. I believe that this is a more efficient way of learning. You learn this way in school,
and why not the same way when learning on your own, especially for when there are really no schools that teach you
actionscripting to its full extent.
I've gone back to the basics. I failed my senior year in Calculus, and never thought I would use Math to the point
I do now, and if I could go back I would put more effort into my Math classes. Alas I cannot, and that is why I'm
relearning everything. And why not share this knowledge with you.
o What are Bits?
As you may know, computers store information using a Base-2 number system (Binary). We normally think
in a Base-10 number system.
In the binary number system, each bit or placeholder contains either a 1 or 0. This number system is read from right to left.
Ex. 1.
The number 4 is represented by 100.
The third bit is set to "On", or mathamatically shown as:
2^2 (two to the power of two).
A More complete representation would be thus:
(1*2^2) + (0*2^1) + (0*2^0) = 4 + 0 + 0 = 4
Ex. 2.
5 Represented by 101.
(1*2^2) + (0*2^1) + (1*2^0) = 4 + 0 + 1 = 5
Ex. 3.
12 Represented by 1100.
(1*2^3) + (1*2^2) + (0*2^1) + (0*2^0) = 8 + 4 + 0 + 0 = 12
o The Bitwise Operators
The Bitwise Operators allow integers to be manipulated att he bit level, which means that you are able to set a particular bit to "on" or "off", compare bits in two integers or shift the bits left or right.
While using the bitwise operators, you must think about an Integer value as an array of 32 bits. The bits are numbered from 0 to 31, from right to left.
The Operators are the folling symbols: &, |, ~, ^, <<, >>.
Do not confuse the & and | for && and ||, these are Logical Operators used mostly for comparison.
o The '&' (And) Operator
The 'And' Operator tells us which bits are turned "on" in both integers.
Ex. 4.
Consider the numbers 5 and 12 (like in example 2 and 3). Both representaiton of these in base-2 are:
101 and 1100. (Can you see what bits are set in both of these integers? If you guessed the third bit, you would be correct. Therefore, 5 & 12 would return 100.
Look at it this way:
| Code: |
5 = 0101
12 = 1100
----
0100
|
Ok, this is all good, but why would I use this? Ever wonder how to find out if a number is odd or even? Well you can use this Operator for just that.
13 & 1 would return 1 (TRUE) conversely, 12 & 1 would return 0 (FALSE).
in Actionsscript you could use this:
| Code: |
function isEven(decimalNumber) {
return (decimalNumber & 1 == 0);
}
trace(isEven(13));
//output:
False
/* Equation
5 = 0101
1 = 0001
----
0001 = 1 = TRUE,
return (decimalNumber & 1 == 0)
return (5 & 1 == 0)
return (1 == 0);
return (FALSE); - This is not even!
*/
|
o The '|' (Or) Operator
The 'Or' Operator tells us which bits are turned "On" in both integers.
Ex. 5.
Consider the same number 5 and 12. 5 | 12 would return 13.
| Code: |
5 = 0101
12 = 1100
----
1101 = (1*2^3) + (1*2^2) + (0*2^1) + (1*2^0) = 8 + 4 + 0 + 1 = 13
|
o The Other operators (^, ~, <<, >>)
The '^' Operator (Xor) returns "On" when the two operands are different
| Code: |
5 = 0101
12 = 1100
----
1001 = (1*2^3) + (0*2^2) + (0*2^1) + (1*2^0) = 8 + 0 + 0 + 1 = 9
|
The '~' Operator (Not) returns the the inverse of a single operand. That is, all "On" bits are turned "Off" and vice versa.
The '<<' Operator (Left Shift) shifts the operand a specified number of bits to the left.
Left Shifting a number n places would be the same as multiplying it by 2^n.
All the bits that are shifted out to the left are discarded, and the new bits coming in from the right are 0.
Ex. 6.
Consider the number 5 and left shift it by 1
| Code: |
5 = 0101
5 << 1 = 1010
|
Shifting either way (left or right) preserves the operands sign, therefore, if you were using a negative number,
it would stay a negative.
the '>>' (Right Shift) Operator is the same as the left, but the opposite way.
+ Practical use
If you understand all of the above, you may be ready to tackly this little tutorial.
How can we use this to our advantage in Flash. Well, consider an application (online or offline). and we are storing a lot of information within a database or a textfile. If your database (textfile) uses alot of boolean values (True or False). your going to be needing a lot of room for all these values.
Think of a database that contained a permissions and login table. This table would hold the persons name, password, status (admin or user) and their permissions to use for this application.
Instead of having 32 boolean fields in each table (which will add up in size) you could use a Bitfield. All those boolean fields are reduced to one.
The next section has the .AS file with all the functions you will need to create and modify a bitfield.
+ NOTES:
Credit goes to Web Reference for the information on the Bitwise Operators.
Credit goes to bokel from Layer 51 prototypes for the basic functions, to which I modified. _________________ Thomas Schemmer
ZaxisNet - Creative Media
o Please do a search before you post
o Reply here for the benfit of all
Last edited by ZaxisNet on Thu Dec 04, 2003 2:17 pm; edited 1 time in total |
|
| Back to top |
|
 |
ZaxisNet Administrator

Joined: 01 Aug 2002 Posts: 276 Location: Toronto, ON
|
Posted: Tue Oct 14, 2003 1:59 pm Post subject: The .AS file |
|
|
The BitField Class
Allows you to replace a small Array (length <= 31) of booleans
Due to a bug int he Mac Flash Player, you can use 31 bits. the 32 bit must always be set to 0. (giving you 0-30).
| Code: |
/* +-------------------------------------------------------+
| BitField |
| @Author: Thomas Schemmer (ZaxisNet - Creative Media) |
| @Date: October 14, 2003 |
+-------------------------------------------------------+ */
function BitField(initVal) {
if (typeof arguments[0] == "string") {
this.set(initVal);
} else {
this.val = initVal;
}
}
BitField.prototype.set = function() {
if (typeof arguments[0] == "string") {
for (var i = 0; i<arguments[0].length; i++) {
this.val |= (1 << arguments[0].indexOf(arguments[0].substr(i, 1)));
}
} else {
for (var i = 0; i<arguments.length; i++) {
this.val |= (1 << arguments[i]);
}
}
};
BitField.prototype.clear = function() {
var alpha = "abcdefghijklmnopqrstuvwxyz";
if (typeof arguments[0] == "string") {
for (var i = 0; i<arguments[0].length; i++) {
this.val &= ~(1 << alpha.indexOf(arguments[0].substr(i, 1)));
}
} else {
for (var i = 0; i<arguments.length; i++) {
this.val &= ~(1 << arguments[i]);
}
}
};
BitField.prototype.toggle = function() {
var alpha = "abcdefghijklmnopqrstuvwxyz";
if (typeof arguments[0] == "string") {
for (var i = 0; i<arguments[0].length; i++) {
this.val ^= (1 << alpha.indexOf(arguments[0].substr(i, 1)));
}
} else {
for (var i = 0; i<arguments.length; i++) {
this.val ^= (1 << arguments[0]);
}
}
};
BitField.prototype.setAll = function () {
this.val = 67108863;
};
BitField.prototype.clearAll = function () {
this.val = 0;
};
BitField.prototype.allSet = function () {
return (this.val == 67108863);
};
BitField.prototype.isSet = function(bit) {
return ((this.val & (1 << bit)) != 0);
};
BitField.prototype.getAll = function(val) {
return (this.val);
};
|
o Usage
| Code: |
myBitField = new BitField();// Create Class
myBitField.set("abcdefghijklmnopqrstuvwxyz"); // 67108863 (Set bits from 0-25, a = 0)
myBitField.clearAll(); // 0
myBitField.setAll(); // 67108863
myBitField.clear("acegikmoqsuwy"); // 44739242 (Clear every other bit)
myBitField.isSet("a"); // false (is the first bit (0) set?)
myBitField.toggle("acegikmoqsuwy"); // 67108863 (Turn the following bits to the reverse of what are, if 1, then turn 0 vice versa)
myBitField.allSet(); // true (Are all Set?)
myBitField.getAll()); // 67108863 (get the decimal number of all bits)
|
You should note, that usuing the alphabet only uses 15 bits, and you have 5 left over that you can use 26-30.
You can also use the base-10 system to control your bitfield.
ex.
this would set the BitField to 2 (2^1) (two to the power of one)
Hope this will help some people, please let me know if you need any more assaitance.
Cheers. _________________ Thomas Schemmer
ZaxisNet - Creative Media
o Please do a search before you post
o Reply here for the benfit of all
|
|
| Back to top |
|
 |
K-dog Moderator

Joined: 23 Jul 2002 Posts: 772 Location: Toronto, ON
|
Posted: Wed Oct 15, 2003 9:17 am Post subject: |
|
|
WOW!
very cool.
--K |
|
| Back to top |
|
 |
Flash Goddess Administrator

Joined: 18 Jul 2002 Posts: 1228 Location: Ontario, Canada
|
|
| Back to top |
|
 |
ZaxisNet Administrator

Joined: 01 Aug 2002 Posts: 276 Location: Toronto, ON
|
Posted: Thu Oct 16, 2003 2:18 am Post subject: |
|
|
Thanks!
I'm actually using BitFields in something I'm doing now, so I thought I would post it to everyone.
Enjoy. _________________ Thomas Schemmer
ZaxisNet - Creative Media
o Please do a search before you post
o Reply here for the benfit of all
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|