"chrisben" <ch******@discussions.microsoft.comwrote in message
news:44**********************************@microsof t.com...
I try to use an integer to pass 4 kinds of information. Each kind may have
5
types. I plan to use an integer like 2034, while 2 0 3 4 are corresponding
to
the type of each kind. I am wondering, what is the most efficient way to
1. update any of field, for example, changing from 2034 to 2134
2. grab the number (0 to 4) from any one of those field. for example,
immediately figure out my third kind is type 3 by reading 2034.
There are many ways of doing it. I am wondering whether there is a way (as
bitwise operations etc.) that I can use to do it most efficiently.
You are a human being and this method of encoding works great for a human
being, but it's terrible for a computer. Computers do not see decimal
digits, they see binary digits. Since you have five potential values, you'll
need 3 bits to represent the each range (and you'll have 3 "wasted" values,
since 3 bits can hold 8 numbers: 0 - 7). In a 32-bit integer you'd have room
for 10 of these values (with 2 bits left over). You could apply masking and
bit shifting to get your values, but no matter what these numbers won't LOOK
like what you have above.
If you absolutely MUST use the format you indicated, you might as well just
start doing integer division by powers of 10 and then modding ( % ) to
extract each digit.
Or you could use the classes Morten mentioned.