473,395 Members | 1,978 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,395 software developers and data experts.

merge two long numbers into a single long number

i have 2 long numbers
eg xl=12345678
xr=87654321

i want to merge these 2 nos. into a single long number
i have to do this in java..
any help would be great..thanks a lot:)
Mar 6 '10 #1

✓ answered by anurag275125

For merging two long values into a single long value, you have to first convert long data type to String and then concat these two string into a single string, then convert this string into long data type.

The code for conversion is:

Expand|Select|Wrap|Line Numbers
  1. public class ankitabhatia
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         long l1,l2;
  6.         l1=12345678;
  7.         l2=87654321;
  8.         String s1=Long.toString(l1);    // converting long to String
  9.         String s2=Long.toString(l2);
  10.         String s3=s1+s2;
  11.         long l3=Long.valueOf(s3).longValue();    // converting String to long
  12.         System.out.println(l3);
  13.     }
  14. }
Hope this will help you!!

4 17265
For merging two long values into a single long value, you have to first convert long data type to String and then concat these two string into a single string, then convert this string into long data type.

The code for conversion is:

Expand|Select|Wrap|Line Numbers
  1. public class ankitabhatia
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         long l1,l2;
  6.         l1=12345678;
  7.         l2=87654321;
  8.         String s1=Long.toString(l1);    // converting long to String
  9.         String s2=Long.toString(l2);
  10.         String s3=s1+s2;
  11.         long l3=Long.valueOf(s3).longValue();    // converting String to long
  12.         System.out.println(l3);
  13.     }
  14. }
Hope this will help you!!
Mar 6 '10 #2
thanks a lot :):)...
Mar 6 '10 #3
@anurag275125
thank's nice solution...
Jul 24 '13 #4
chaarmann
785 Expert 512MB
Why convert to String and then back?
You can multiply the first number with a power of ten and add the second to it. That's a high magnitude quicker!

Example:
given
l1=12345678;
l2=87654321;
results in
l3=l1*100000000 + l2;

P.S: Just look at the source code of Long.toString() and you see that's a loop which divides by ten and prints the remainder into a string buffer. But you do the division only once and you don't need to allocate memory for your string object.

P.P.S:
if the length of the first number is unknown, then
you can get it with divide-and-conquer alogorithm. It's much faster than String.valueOf(i1).length() or (int)(Math.log10(i1)+1) or other ways. See http://stackoverflow.com/questions/1...gits-in-an-int
Jul 24 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: el_roachmeister | last post by:
I have a 4000 line php cgi script that is not memory or cpu intensive. It runs in 1 second. Now I am getting a lot of customer requests to add in new features. My question is if there is a point...
6
by: Skybuck Flying | last post by:
unsigned long long int __a; /* Factor in congruential formula. */ C:\C TESTEN\TestNRand48\Main.cpp(29) : error C2632: 'long' followed by 'long' is illegal ???
5
by: Daniel Rudy | last post by:
How does one covert a interger number in a unsigned long long int (64-bit) to long double (80-bit) storage? I looked at math.h and I found function that convert double to long long, but didn't...
4
by: Hipo | last post by:
Hi. My problem seems to be simple, but I can not figure out a solution. Problem: I have a number that consists of 128 bit. I want to compute its modulo (modulo is at the range < 2^16). So that...
3
by: Caffiend | last post by:
This question is probably to complex to be answered quickly, but if you could even point me in the right direction that would be great! I am interested in knowing how to work with extremely large...
2
by: ruthboaz | last post by:
Hi, I need to merge about 50 database into a single database for reporting purpose. All have the same structure. I do not know at any point of time the list of database in the server. It...
1
by: tbrogdon | last post by:
I am new to this group and new to DBs. I am building a small DB for my work. We create sheetmetal parts. Each part has a part number (e.g., 1054471 or 50TG508506 - both formats are typical for a...
105
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
3
by: jatinch | last post by:
i am implementing blowfish algorithm.. i have a long number or a 64 bit no. say X i have to divide this 64 bit block into two 32 bit blocks say xl and xr. after doing some calculations i have to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.