• That was a good article on packing bits into integer fields, I just wanted to add a little something. Before I begin, though, I want to note that this is written with T-SQL for SQL Server in mind, I'm not 100% certain if Access would work the same way, or could be made to work using different operators that mean the same thing.

    When you're pulling the values back out of the packed integer field, you can use the bit-wise operator, "&", to determine which bits are set. So, if you have eight bits packed into an integer, as in your article, and you want to find out if the fifth bit is set, you can use the following code:

    -- assume @packedbits is an integer defined above and set to

    -- the integer you're storing in the database for the check boxes

    declare @box_5 bit

    select @box_5 = (@packedbits & 16)

    You can also use 2^x to select the bit fields, so (@packedbits & 16) is the same as (@packedbits & (2^4)), much like how you generated the listing of values for each bit position in the integer. Note that you must enclose (2^4) in parentheses since the bit-wise operator has a higher precedence in the order of operations.

    The above code does rely on an implicit conversion from int to bit; the actual value returned from (@packedbits & 16) when the fifth bit is set will be 16, but SQL will implicitly convert any non-zero numeric value to 1 before setting your bit variable.

    I think this makes the code a bit more compact and easier to read, particularly if you're setting all 32 bits available in your integer. 🙂

    Matthew Galbraith