Re: [Flashcoders] Bitwise selection
On Fri, Apr 3, 2009 at 8:38 AM, Jiri wrote: > So each element in a ByteArray can hold 8 bits. > What about the readInt() method of the ByteArray, does an integer then span > over 4 elements of the bytearray. And if I start at position 0 and then call > the readInt(), is the position after that then 4? Yes, and you can use readUnsignedByte(), too, for example, but if you're trying to use a ByteArray to store bit flags, then we can probably help you find a more elegant way to do what you want. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Bitwise selection
Jiri wrote: > So each element in a ByteArray can hold 8 bits. > What about the readInt() method of the ByteArray, does an integer then > span over 4 elements of the bytearray. And if I start at position 0 and > then call the readInt(), is the position after that then 4? Essentially, yes. A ByteArray is a packed array of bytes. Or, put another way, it stores the most compact representation of a data type, but you can still access each byte with the [] array access. Consider the following (e-mail AS3--don't count on it to be bug-free): var myByteArr:ByteArray = new ByteArray(); myByteArr.writeInt (2695938256); trace (myByteArr.length); //4 trace (myByteArr[0]); //208 trace (myByteArr[1]); //192 trace (myByteArr[2]); //176 trace (myByteArr[3]); //160 trace (myByteArr.readInt());//2695938256 I chose that rather odd number because it was a convenient hex number, #A0B0C0D0. Doing a trace on a ByteArray element returns the decimal equivalent of the byte. It's possible I got the order reversed--I didn't test the code. #A0 might be in the 0th element. Nonetheless, I think the code represents the concept reasonably well. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Bitwise selection
So each element in a ByteArray can hold 8 bits. What about the readInt() method of the ByteArray, does an integer then span over 4 elements of the bytearray. And if I start at position 0 and then call the readInt(), is the position after that then 4? Jiri Kerry Thompson wrote: Jiri wrote: Nice, that you took the time to write that post. It is much appreciated. Thanks, Jiri. One other thing worth mentioning is that an integer is actually 4 bytes, so you have 32 bits. I kept my example to one byte for simplicity. If you want to do operations on a single byte, you can use ByteArray elements. Other bitwise operators to check out are XOR (exclusive OR, which means one or the other is on, but not both) and shift operators >> and << to check the value of a particular bit. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Bitwise selection
Jiri wrote: > Nice, that you took the time to write that post. It is much appreciated. Thanks, Jiri. One other thing worth mentioning is that an integer is actually 4 bytes, so you have 32 bits. I kept my example to one byte for simplicity. If you want to do operations on a single byte, you can use ByteArray elements. Other bitwise operators to check out are XOR (exclusive OR, which means one or the other is on, but not both) and shift operators >> and << to check the value of a particular bit. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Bitwise selection
Nice, that you took the time to write that post. It is much appreciated. Jiri Kerry Thompson wrote: Jiri has gotten some good answers. I got to work late today after working until 11:30 last night meeting my 5:00 deadline :-P I did occur to me that a fair number of us may not completely grok bitwise stuff. That's computer science stuff, and a lot of us got here by other routes-I see a lot of C programmers doing bitwise stuff, but relatively few AS programmers (at least, we don't talk about it a lot). Still, it can be blazingly fast, so when you have a need for speed, they are great. So I thought I'd talk a little about bit operations. If you already understand them, no need to read further (unless you wish to check my accuracy). First, the basics. We all probably know this, but a byte is no more than a series of on-off switches, or bits--eight of them on most modern computers. On is represented by 1, off by 0. So, a byte with every other bit on would be 10101010. When you do a bitwise operation, you are comparing bits. The bitwise OR operator, | , compares bits. If either of them is on (1), the result is 1. If both are off (0), the result is 0. Consider the following: 120 | 96 Compares 10101010 and 0110, giving the result 11101010 (234 decimal). Visually, this may help: 10101010 0110 11101010 We get that result because the | operator compares each bit. If either of them is on, the result is 1 for that bit. The AND operator & is similar in that it compares bit by bit, but both bits have to be on to get a 1 result. Comparing the same two numbers as before, 120 & 96, gives you the result 0010, or 32 decimal. 10101010 0110 00101010 Sometimes we use these as flags, where every bit can represent a Boolean value. They also can be used for fast math operations (check out http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/ This barely scratches the surface of bitwise operators and their power, but I hope it intrigues some of you enough to pursue it further. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Bitwise selection
Jiri has gotten some good answers. I got to work late today after working until 11:30 last night meeting my 5:00 deadline :-P I did occur to me that a fair number of us may not completely grok bitwise stuff. That's computer science stuff, and a lot of us got here by other routes-I see a lot of C programmers doing bitwise stuff, but relatively few AS programmers (at least, we don't talk about it a lot). Still, it can be blazingly fast, so when you have a need for speed, they are great. So I thought I'd talk a little about bit operations. If you already understand them, no need to read further (unless you wish to check my accuracy). First, the basics. We all probably know this, but a byte is no more than a series of on-off switches, or bits--eight of them on most modern computers. On is represented by 1, off by 0. So, a byte with every other bit on would be 10101010. When you do a bitwise operation, you are comparing bits. The bitwise OR operator, | , compares bits. If either of them is on (1), the result is 1. If both are off (0), the result is 0. Consider the following: 120 | 96 Compares 10101010 and 0110, giving the result 11101010 (234 decimal). Visually, this may help: 10101010 0110 11101010 We get that result because the | operator compares each bit. If either of them is on, the result is 1 for that bit. The AND operator & is similar in that it compares bit by bit, but both bits have to be on to get a 1 result. Comparing the same two numbers as before, 120 & 96, gives you the result 0010, or 32 decimal. 10101010 0110 00101010 Sometimes we use these as flags, where every bit can represent a Boolean value. They also can be used for fast math operations (check out http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/ This barely scratches the surface of bitwise operators and their power, but I hope it intrigues some of you enough to pursue it further. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Bitwise selection
Thanks for the helpfull feedback Mark Winterhalder wrote: Jiri, if() isn't too bad, especially since you will possibly want to permit multiple restrictions (like numbers /and/ letters, or Latin letters plus umlauts). If you use if() and combine that with appending to the restriction instead of setting it (+= instead of =), you gain flexibility. That way, you could also split lower case and upper case letter restrictions, and introduce a case insensitive restriction that is set to (UPPER_CASE | LOWER_CASE) -- it would have both bits set, so it would "hit" for both tests and append the restrictions to the "a-z A-Z " you already have now. That's just one example, the idea is not to have a fixed number of selections, but "groups" that you can switch on bit by bit, plus some predefined combinations (like the case insensitive letter example) for convenience. Also, don't test (restriction & 2), but (restriction & NUM_ONLY). That way, it's more readable, and you don't have to change all 2s if you decide to reorder your flags. Even more pedantic :), test ((restriction & NUM_ONLY) == NUM_ONLY) so you don't run into problems if you want to have flags that have multiple bits set later on. HTH, Mark On Thu, Apr 2, 2009 at 12:08 PM, Jiri wrote: I am new to bitwise operators, so I am trying to learn it. I have the following code and it works half. I am using a switch case to get the result, but this is messing things up. I could revert to and if - else statement, but I was wondering if there is a more elagant way of doing it. I post my code below, and would have some advice. var NO_RESTRICTION:int = 1; var NUM_ONLY:int = 2; var CHAR_ONLY:int = 4; var RESTRICTION:int = NUM_ONLY ; function setInputCharRestriction(tInt:int):void { RESTRICTION = tInt | tInt&2 | tInt&3; } function getRestrict():String{ var tRestrict:String = ''; trace('all ' , Boolean(RESTRICTION&1)) trace('num ' , Boolean(RESTRICTION&2)) trace('char ' ,Boolean(RESTRICTION&4)) switch(RESTRICTION){ case RESTRICTION&1 : tRestrict +="\u0020-\u007E"; trace('all') case RESTRICTION&2: tRestrict =" 0-9"; trace('num') case RESTRICTION&4: tRestrict =" A-Z a-z"; trace('char') } trace('restrict field ' , tRestrict) return tRestrict; } getRestrict() Thank you. Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Bitwise selection
Jiri, if() isn't too bad, especially since you will possibly want to permit multiple restrictions (like numbers /and/ letters, or Latin letters plus umlauts). If you use if() and combine that with appending to the restriction instead of setting it (+= instead of =), you gain flexibility. That way, you could also split lower case and upper case letter restrictions, and introduce a case insensitive restriction that is set to (UPPER_CASE | LOWER_CASE) -- it would have both bits set, so it would "hit" for both tests and append the restrictions to the "a-z A-Z " you already have now. That's just one example, the idea is not to have a fixed number of selections, but "groups" that you can switch on bit by bit, plus some predefined combinations (like the case insensitive letter example) for convenience. Also, don't test (restriction & 2), but (restriction & NUM_ONLY). That way, it's more readable, and you don't have to change all 2s if you decide to reorder your flags. Even more pedantic :), test ((restriction & NUM_ONLY) == NUM_ONLY) so you don't run into problems if you want to have flags that have multiple bits set later on. HTH, Mark On Thu, Apr 2, 2009 at 12:08 PM, Jiri wrote: > I am new to bitwise operators, so I am trying to learn it. > > I have the following code and it works half. I am using a switch case to get > the result, but this is messing things up. I could revert to and if - else > statement, but I was wondering if there is a more elagant way of doing it. I > post my code below, and would have some advice. > > var NO_RESTRICTION:int = 1; > var NUM_ONLY:int = 2; > var CHAR_ONLY:int = 4; > > var RESTRICTION:int = NUM_ONLY ; > > function setInputCharRestriction(tInt:int):void { > RESTRICTION = tInt | tInt&2 | tInt&3; > } > > function getRestrict():String{ > var tRestrict:String = ''; > > trace('all ' , Boolean(RESTRICTION&1)) > trace('num ' , Boolean(RESTRICTION&2)) > trace('char ' ,Boolean(RESTRICTION&4)) > > switch(RESTRICTION){ > case RESTRICTION&1 : > tRestrict +="\u0020-\u007E"; > trace('all') > case RESTRICTION&2: > tRestrict =" 0-9"; > trace('num') > case RESTRICTION&4: > tRestrict =" A-Z a-z"; > trace('char') > } > trace('restrict field ' , tRestrict) > return tRestrict; > } > > getRestrict() > > Thank you. > > Jiri > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Bitwise selection
Jiri, I think Hans's advice will get you what you want. case RESTRICTION&1 : tRestrict +="\u0020-\u007E"; trace('all'); break; case RESTRICTION&2: tRestrict =" 0-9"; trace('num'); break; case RESTRICTION&4: tRestrict =" A-Z a-z"; trace('char'); break; I think there might be communication confusion. On Thu, Apr 2, 2009 at 7:44 AM, Jiri wrote: > Thanks Hans, > > I am aware of that, so if-else would be the only way to go I guess. > > Jiri > > > > Hans Wichman wrote: > >> Hi, >> >> isn't the absence of break statements messing things up? >> >> greetz >> JC >> >> On Thu, Apr 2, 2009 at 12:08 PM, Jiri >> wrote: >> >> I am new to bitwise operators, so I am trying to learn it. >>> >>> I have the following code and it works half. I am using a switch case to >>> get the result, but this is messing things up. I could revert to and if - >>> else statement, but I was wondering if there is a more elagant way of >>> doing >>> it. I post my code below, and would have some advice. >>> >>> var NO_RESTRICTION:int = 1; >>> var NUM_ONLY:int = 2; >>> var CHAR_ONLY:int = 4; >>> >>> var RESTRICTION:int = NUM_ONLY ; >>> >>> function setInputCharRestriction(tInt:int):void { >>> RESTRICTION = tInt | tInt&2 | tInt&3; >>> } >>> >>> function getRestrict():String{ >>> var tRestrict:String = ''; >>> >>> trace('all ' , Boolean(RESTRICTION&1)) >>> trace('num ' , Boolean(RESTRICTION&2)) >>> trace('char ' ,Boolean(RESTRICTION&4)) >>> >>> switch(RESTRICTION){ >>> case RESTRICTION&1 : >>> tRestrict +="\u0020-\u007E"; >>> trace('all') >>> case RESTRICTION&2: >>> tRestrict =" 0-9"; >>> trace('num') >>> case RESTRICTION&4: >>> tRestrict =" A-Z a-z"; >>> trace('char') >>> } >>> trace('restrict field ' , tRestrict) >>> return tRestrict; >>> } >>> >>> getRestrict() >>> >>> Thank you. >>> >>> Jiri >>> ___ >>> Flashcoders mailing list >>> Flashcoders@chattyfig.figleaf.com >>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders >>> >>> ___ >> Flashcoders mailing list >> Flashcoders@chattyfig.figleaf.com >> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders >> >> ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- -jonathan howe ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Bitwise selection
Thanks Hans, I am aware of that, so if-else would be the only way to go I guess. Jiri Hans Wichman wrote: Hi, isn't the absence of break statements messing things up? greetz JC On Thu, Apr 2, 2009 at 12:08 PM, Jiri wrote: I am new to bitwise operators, so I am trying to learn it. I have the following code and it works half. I am using a switch case to get the result, but this is messing things up. I could revert to and if - else statement, but I was wondering if there is a more elagant way of doing it. I post my code below, and would have some advice. var NO_RESTRICTION:int = 1; var NUM_ONLY:int = 2; var CHAR_ONLY:int = 4; var RESTRICTION:int = NUM_ONLY ; function setInputCharRestriction(tInt:int):void { RESTRICTION = tInt | tInt&2 | tInt&3; } function getRestrict():String{ var tRestrict:String = ''; trace('all ' , Boolean(RESTRICTION&1)) trace('num ' , Boolean(RESTRICTION&2)) trace('char ' ,Boolean(RESTRICTION&4)) switch(RESTRICTION){ case RESTRICTION&1 : tRestrict +="\u0020-\u007E"; trace('all') case RESTRICTION&2: tRestrict =" 0-9"; trace('num') case RESTRICTION&4: tRestrict =" A-Z a-z"; trace('char') } trace('restrict field ' , tRestrict) return tRestrict; } getRestrict() Thank you. Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Bitwise selection
Hi, isn't the absence of break statements messing things up? greetz JC On Thu, Apr 2, 2009 at 12:08 PM, Jiri wrote: > I am new to bitwise operators, so I am trying to learn it. > > I have the following code and it works half. I am using a switch case to > get the result, but this is messing things up. I could revert to and if - > else statement, but I was wondering if there is a more elagant way of doing > it. I post my code below, and would have some advice. > > var NO_RESTRICTION:int = 1; > var NUM_ONLY:int = 2; > var CHAR_ONLY:int = 4; > > var RESTRICTION:int = NUM_ONLY ; > > function setInputCharRestriction(tInt:int):void { >RESTRICTION = tInt | tInt&2 | tInt&3; > } > > function getRestrict():String{ >var tRestrict:String = ''; > >trace('all ' , Boolean(RESTRICTION&1)) >trace('num ' , Boolean(RESTRICTION&2)) >trace('char ' ,Boolean(RESTRICTION&4)) > >switch(RESTRICTION){ >case RESTRICTION&1 : >tRestrict +="\u0020-\u007E"; >trace('all') >case RESTRICTION&2: >tRestrict =" 0-9"; >trace('num') >case RESTRICTION&4: >tRestrict =" A-Z a-z"; >trace('char') >} >trace('restrict field ' , tRestrict) >return tRestrict; > } > > getRestrict() > > Thank you. > > Jiri > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders