[thelist] Unique combination numbering

David T-G davidtg-thelist at justpickone.org
Fri May 7 09:58:01 CDT 2004


João, et al --

...and then João Verde said...
% 
% The situation:
% =========
% 
% I have a form with a field for which there are 5 possible checkboxes.
% 
% [EXAMPLE]
% Water: [ ] River [ ] Lagoon [ ] Pool [ ] Sea [ ] Other
% [/EXAMPLE]
% 
% You can check them all, none, or any combination of the 5.

Since each of these places can hold one of two values, I would represent
it in binary; you can even use a smallint (two to the eighth power = 256
unique values) to do it.

  W R L P S    bin    dec

  . . . . .    00000  0 + 0 + 0 + 0 + 0 = 0
  . . . . x    00001  0 + 0 + 0 + 0 + 1 = 1
  . x . x .    01010  0 + 8 + 0 + 2 + 0 = 10
  x . . . .    10000  16 + 0 + 0 + 0 + 0 = 16
  x x x x x    11111  16 + 8 + 4 + 2 + 1 = 31

[Note that you show 'Other' as well; going from 2^5 to 2^6 is left as an
exercise for the student if necessary.]

All you have to do is keep dividing the number you've stored by two to
see what input you had.  There may even be some bit-shifting or dec<->bin
conversions in the language you're using to make encoding and decoding
easier.


HTH & HAND

:-D
-- 
David T-G
davidtg at justpickone.org
http://justpickone.org/davidtg/      Shpx gur Pbzzhavpngvbaf Qrprapl Npg!



More information about the thelist mailing list