play with integer numbers

  • Thread starter Thread starter chrisben
  • Start date Start date
C

chrisben

Hi,

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.

Thanks

Chris
 
I cannot imagine the process of encoding and decoding the four values to a
four digit integer will better than simply passing the four values.

You won't be able to use bitwise operations, because it appears you are
using base 10 logic to build your integer. You would need to do integer
divisions and mods to split the int apart, or else split the string itself to
four single chararcter string to get an int.
 
chrisben said:
Hi,

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.

Thanks

Chris

Hi Chris,

You can store many values inside an integer data type. Using bit
manipulation you can split the number into fields and work with the fields
and combine them to a number afterwards

int n = 123456789;

int field1 = n & 0xFF;
int field2 = (n >>= 8) & 0xFF;
int field3 = (n >>= 8) & 0xFF;
int field4 = n >>= 8;

int o = (field4 <<= 24) | (field3 <<= 16) | (field2 <<= 8) | field1;

The above code splits the integer into four equal 8 bit parts but you can
just as well use variable parts. Change the mask and bit shift positions as
needed.

You can also use the BitArray and BitVector32 classes meant for manipulating
values stored as bits inside a bigger data type. BitVector32 specifically
uses int32 as storage type.
 
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.
 
Thank you guys. I was looking for the answer as Morten suggested. Since I
pass a lot of data through the network by the middleware, I tried to compress
the msg as small as possible. Maybe a little bit overdone. need to think
about it.
Thanks
 
Back
Top