>- see footer for list info -<
Hi Dominic
It's interesting but are you sure the space you're saving warrants the
complexity of how you're doing it. 

I'm no guru, but it sounds like it would be a right pain to maintain.  

Regards
Mark

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of dominic watson
Sent: 26 October 2005 10:10
To: Coldfusion Development
Subject: Re: [CF-Dev] Bitwise operators

>- see footer for list info -<
Well, I wrote a few UDFs that make the bitwise operators more readable for
using with flags. I've put my thoughts in the bottom of the udf....

<!--------------------------------------------------------------------------
--------------------------------->

<!---    
        Flags.cfm
        Functions to select and deselect bit flags in an integer.
       
        THIS ENABLES YOU TO RECORD MULTIPLE SELECTIONS OF ITEMS IN A SINGLE
INTEGER.
        See notes at the bottom of the page for further info and example.
       
        Dominic Watson, 2005
--->
       


<cfscript>
    // int setFlag(int flag, int flagSet, boolean selected = true)
    function setFlag(flag, flagSet)
    {
        var selected = true;
        if(arrayLen(arguments) GT 2)
            selected = arguments[3];
           
        if(selected)
        {
            return bitOr(flag, flagSet);
        }
       
        return arguments.flagSet - bitAnd(arguments.flagSet,
arguments.flag);
    }
   
    // int toggleFlag(int flag, int flagSet)
    function toggleFlag(flag, flagSet)
    {
        return bitXOr(flag,flagSet);
    }
   
    // boolean isFlagSet(int flag, int flagSet)
    function isFlagSet(flag, flagSet)
    {
        if(arguments.flag EQ bitAnd(arguments.flagSet, arguments.flag))
        {
            return true;
        }
       
        return false;
    }
</cfscript>



<!---    NOTES:
   
    What's a bit flag?
    ------------------
   
    An integer is made up of 32 bits (normally). Those bits look like this:
   
    10100010 00000000 00000000 00000000
   
    Each bit represents a number. The bit on the far left represents 1. 
The next one along is 2, then 4,8,16,32,etc.
   
    An integer number is calculated by adding all the selected bits up, so,
in the example above we have:
   
           
    1 2 4 8 16 32 64 128, etc.
    1 0 1 0  0  0  1  0 , etc.
   
    1 + 4 + 64 = 69!
   
    There is no other way of expressing 69 in binary other than:
    10100010 00000000 00000000 00000000
   
   
    SO WHAT?
   
    Well, we can utilise this on-off nature of bits to set 'flags'.
    Simply put, a flag is an option that you can either select or not
select.
    So, if we say that each of the bits in our integer represents an option
that
    we want to be able to select or deselect, we can store the status of
those options
    in a single integer. Give each option an id that corresponds to a bit
and we can turn
    the option on and off just by setting a single bit.
   
    Now there are 32 bits in an integer, so thats a maximum of 32 options,
which in most cases is more than plenty.
    Other languages use long ints and double words that can store more
options, but I can't see that that is available in CF.
   
    How can I use these functions?
    ------------------------------
   
    The functions I have written make readable code of the bit functions
that come with CF that enable
    you to utilise bit flags.
   
    Below is an example of how they may be used.
   
    <cfscript>
        // Our options. Notice how the values correspond to the numbers that
bits represent from left to right.
        cat=1; dog=2; snake=4; hampster=8; mouse=16;
       
        // The variable that is going to store the multiple selections (0 =
00000000 00000000 00000000 00000000, no flags are selected)
        Pets=0;
       
        // select snake, cat and hampster
        pets = setFlag(snake,pets,true);
        pets = setFlag(cat,pets);
       
        // deselect cat
        pets = setFlag(cat,pets,false);
       
        // toggle the status of hampster
        pets = toggleFlag(hampster,pets);      
       
        // check status of snake
        if(isFlagSet(snake,pets))
        {
            // do stuff
        }   
    </cfscript>

    Why bother?
    -----------
    Put simply, it saves space. Not so much in the live running of the
application; but when it comes to storing
    selections in a database it can save a lot of space (given a large
database).
    I.e.
   
    We have a customer record and we need to store their favourite pets
somewhere. There are 32 pets
    to choose from. We create a seperate table that contains our 32 pets and
each one has an ID. Now we have at
    least 3 options:
   
    1. Create a 3rd table that contains the fields customerID and petID. 
To get at the customer's
        pet selections we simply query that table to select all pets with
that customer ID. A good
        solution in that it enforces database integritry, but if we have
lots of these types of options
        we have a lot of tables.
    2. Add a string field to the customer table that will contain a list of
their selections.
        This is ok, but if we have lots of options like this with lots of
customers we are
        really wasting space because...
    3. You can store the customer's selection in a single integer field. 
If there are 8 or less options
        this can be a 'tinyint'(in sql), 16 or less it's a 'smallint', and
32 or less and it's an 'int'.
   
    Well, thats why I would bother.
   
    So that's it, a lot of babble for 3 functions that are just packaging a
couple of very simple bit operations.

--->
_______________________________________________

For details on ALL mailing lists and for joining or leaving lists, go to
http://list.cfdeveloper.co.uk/mailman/listinfo

--
CFDeveloper Sponsors:-
>- Hosting provided by www.cfmxhosting.co.uk -<
>- Forum provided by www.fusetalk.com -<
>- DHTML Menus provided by www.APYCOM.com -<
>- Lists hosted by www.Gradwell.com -<
>- CFdeveloper is run by Russ Michaels, feel free to volunteer your help 
>-<




_______________________________________________

For details on ALL mailing lists and for joining or leaving lists, go to 
http://list.cfdeveloper.co.uk/mailman/listinfo

--
CFDeveloper Sponsors:-
>- Hosting provided by www.cfmxhosting.co.uk -<
>- Forum provided by www.fusetalk.com -<
>- DHTML Menus provided by www.APYCOM.com -<
>- Lists hosted by www.Gradwell.com -<
>- CFdeveloper is run by Russ Michaels, feel free to volunteer your help -<

Reply via email to