merge two long numbers into a single long number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ankitabhatia
    New Member
    • Feb 2010
    • 8

    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:)
  • anurag275125
    New Member
    • Aug 2009
    • 79

    #2
    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:

    Code:
    public class ankitabhatia
    {
    	public static void main(String args[])
    	{
    		long l1,l2;
    		l1=12345678;
    		l2=87654321;
    		String s1=Long.toString(l1);	// converting long to String
    		String s2=Long.toString(l2);
    		String s3=s1+s2;
    		long l3=Long.valueOf(s3).longValue();	// converting String to long
    		System.out.println(l3);
    	}
    }
    Hope this will help you!!

    Comment

    • ankitabhatia
      New Member
      • Feb 2010
      • 8

      #3
      thanks a lot :):)...

      Comment

      • deathhack
        New Member
        • Jul 2013
        • 1

        #4
        Originally posted 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:

        Code:
        public class ankitabhatia
        {
        	public static void main(String args[])
        	{
        		long l1,l2;
        		l1=12345678;
        		l2=87654321;
        		String s1=Long.toString(l1);	// converting long to String
        		String s2=Long.toString(l2);
        		String s3=s1+s2;
        		long l3=Long.valueOf(s3).longValue();	// converting String to long
        		System.out.println(l3);
        	}
        }
        Hope this will help you!!
        thank's nice solution...

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          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.log1 0(i1)+1) or other ways. See http://stackoverflow.com/questions/1...gits-in-an-int

          Comment

          Working...