Thread: easiet way to put digits at the left side of number

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    easiet way to put digits at the left side of number

    lets say i have a long type variable
    for example
    Code:
    long x =43343539839853583;
    im looking for the best algorithm to put 111 for example at the left of the number(
    Code:
    final result 11143343539839853583)
    Last edited by cable; 11-04-2011 at 03:10 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    So I guess we're assuming that a long is 64 bits? I would just do this: x += 11100000000000000000;
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If the number of digits will vary, then you need to look into using log10 to figure out the number of digits (or a division loop), and pow to get something to multiply it by to get the right number of zeros after the 111. Then you can add.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by itsme86 View Post
    So I guess we're assuming that a long is 64 bits? I would just do this: x += 11100000000000000000;
    Even if we were assuming that, then the constant is still missing the L suffix and is therefore of type int. Unless the compiler he is using treats both int and long as 64 bit (which I don't believe can be the case) then the first line of code he posted is a fail.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Convert to string, stick "111" at the front, convert back to long?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Like this...

    Code:
    char str[32] = "111";
    
    sprintf(&str[3],"%d",number);
    
    number = stoll(str);
    Brewbuck had the right idea...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shift all '1' bits in a number to the left end
    By beatbox32 in forum C Programming
    Replies: 10
    Last Post: 07-06-2011, 01:38 PM
  2. Replies: 7
    Last Post: 11-24-2010, 04:21 AM
  3. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 12:12 PM
  4. Replies: 19
    Last Post: 09-17-2005, 09:49 AM
  5. Number of digits?
    By falador in forum C Programming
    Replies: 2
    Last Post: 05-13-2004, 03:52 AM