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 <jiriheitla...@googlemail.com> 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

Reply via email to