471,610 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Help combinging 3 numbers into 1 with bitwise operatioons

I have 3 5-digit numbers that i'd like to combine into one 15 digit number,
and later rip back into the original 3 5-digit numbers.

I know i can do this easily with some string manipulations, but i'm pretty
sure i can do the same thing with some bitwise operation like bit shifting or
something.

basically, if i have the following 3 numbesr

12321
34543
67876

i'd like to end up with

123213454367876

any ideas?
Feb 4 '06 #1
2 1238
Hi John,
bit shifting is modifying the bits in binary so you are talking about
base2, if you want to preserve the numbers after you combine them ie 12345 is
in base10 then you want to multiply to the power of 10 to shift the whole
number left. You also need to make sure that you have a datatype that is
capable of representing this large number. What you want to do is:
12321
34543
67876

i'd like to end up with

123213454367876
then it is:

123210000000000 + 3454300000 + 67876 = 123213454367876

which you could do by:

int i1 = 12321;
int i2 = 34543;
int i3 = 67876;

double dall = i1 * 1E10 + i2 * 1E5 + i3;

Hope that helps
Mark Dawson
http://www.markdawson.org


"john conwell" wrote:
I have 3 5-digit numbers that i'd like to combine into one 15 digit number,
and later rip back into the original 3 5-digit numbers.

I know i can do this easily with some string manipulations, but i'm pretty
sure i can do the same thing with some bitwise operation like bit shifting or
something.

basically, if i have the following 3 numbesr

12321
34543
67876

i'd like to end up with

123213454367876

any ideas?

Feb 4 '06 #2
john conwell wrote:
I have 3 5-digit numbers that i'd like to combine into one 15 digit number,
and later rip back into the original 3 5-digit numbers.

I know i can do this easily with some string manipulations, but i'm pretty
sure i can do the same thing with some bitwise operation like bit shifting or
something.

basically, if i have the following 3 numbesr

12321
34543
67876

i'd like to end up with

123213454367876

any ideas?


how about:?

long a = 12321;
long b = 34543;
long c = 67876;

long d = a*10000000000 + b*100000 + c;

hth,
Max
Feb 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

45 posts views Thread by bobalong | last post: by
5 posts views Thread by noridotjabi | last post: by
10 posts views Thread by Rob Wilkerson | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

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.