One Database Field to Hold Survey Answer

Posted by yar on Stack Overflow See other posts from Stack Overflow or by yar
Published on 2010-05-24T12:28:45Z Indexed on 2010/05/24 12:31 UTC
Read the original article Hit count: 170

Filed under:

Skipping the question of whether this is bad design (and the question of why I would want/need to do this), I'm just wondering if my 'math' is right...

I have n things that need to be put in order (n is always less than 5):

thing1
thing2
thing3
...

and I'd like to store these results in a single integer for thing. My initial thought is that (obviously the code will not look like this):

thing = thing1 * 1 + thing2 * 2 + thing3 * 4 + thing5 * 8

will always give me a unique value, and that any value for thing will always translate back to its values for thing1...thingn. Is this correct?

© Stack Overflow or respective owner

Related posts about algorithm