473,385 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

play with integer numbers

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
Oct 17 '08 #1
5 1589
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" wrote:
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
Oct 17 '08 #2

"chrisben" wrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 17 '08 #3
On Fri, 17 Oct 2008 08:52:01 -0700, chrisben
<ch******@discussions.microsoft.comwrote:
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.
I agree with FTM. You should probably rethink whether you really should
be compressing your data into a single int. For the vast majority of
applications, this sort of thing really isn't necessary. Memory and disk
space is cheap, and even bandwidth is pretty high these days. At most,
just use the smallest word size needed for the value that's possible. If
all your values are less than 256 (and it appears they are), then you can
put them in bytes instead of ints.

In fact, doing it that way will use the exact same amount of space, but
with much simpler code. It's essentially the same way to store the data
as Morten has suggested, but with much more readable code.

Pete
Oct 17 '08 #4
"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.
Oct 17 '08 #5

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
Oct 17 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: dave | last post by:
I am reading input from a form. I want to validate the input by making sure that the string is actually an integer. How would I do this? Do i need to convert it to a character array and break down...
22
by: MLH | last post by:
I have some audio help files that play fine from within Access 97 and Access 2.0. Both are running on a Windows XP box. But I do not know what program plays the files. If I click Start, Run and...
10
by: CM | last post by:
Hi, I used to use the following code to play sound in VB6: Public Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, ByVal uFlags As...
38
by: Maarten | last post by:
hi all, how can i retrieve the type if a value inserted in an inputbox. what i want to do is make shure people only insert an integer type, if they don't they get an error message. ...
16
by: Stefan Wallentowitz | last post by:
Hello together! I'm searching for a statistic about the value ranges of integers in a set of "standard" c-programs. I'm specifically interested in the avarage ratio of assignments etc. of...
5
by: Nonoize | last post by:
Hi all, Really dumb question but I don't know how to resolve it. Looked in help and evry book I have. I have a table where the primary key was set as an Integer and its reached over 140K...
5
by: Bob Homes | last post by:
In VB6, foreground and background colors of controls had to be assigned a single number. If you knew the RGB values for the color, you still had to convert them into the single number accepatable...
3
by: Paul D Ainsworth | last post by:
Greetings everyone. I'm a relative newcomer to python and I have a technical problem. I want to split a 32 bit / 4 byte unsigned integer into 4 separate byte variables according to the following...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.