Thread: How to save a 64 bit number with 2 32 bit ints?

  1. #16
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    sprintf(val, "%llu", (long long)top << 32 | bottom);

  2. #17
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by nonoob
    Code:
    sprintf(val, "%llu", (long long)top << 32 | bottom);
    A type cast does not allocate memory for any type of variable.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What are you getting at, exactly? The example should work according to what the OP wants.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by siavoshkc View Post
    A type cast does not allocate memory for any type of variable.
    What type of variable, do you think, will be put on the stack?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    What type of variable, do you think, will be put on the stack?
    I didn't mean that (dynamic allocation). Stack memory is also allocated.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by siavoshkc View Post
    I didn't mean that (dynamic allocation). Stack memory is also allocated.
    it's MY point...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by siavoshkc View Post
    A type cast does not allocate memory for any type of variable.
    I don't understand your objection to my solution. I rarely post code snippets without actually testing it in a compiled and run program for correctness. The above cast is fine and does exactly what is needed.

  8. #23
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by nonoob
    I don't understand your objection to my solution. I rarely post code snippets without actually testing it in a compiled and run program for correctness. The above cast is fine and does exactly what is needed.
    Sorry, I think I was wrong.
    Are you sure it will work with any compiler?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think it is safe to say that it is quite certain.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by siavoshkc View Post
    Are you sure it will work with any compiler?
    Yep! because behind-the-scenes, it uses a 64-bit scratch register/variable to save (long long) top before it's stringified and saved to char val[].
    Last edited by itCbitC; 04-07-2010 at 11:22 AM.

  11. #26
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Still not sure about what the concern is. If the compiler supports 64-bit integers, then it is allowable to push such a data type on the stack. sprintf's "%lld" will retrieve that sized value properly.

    I expected more of an objection about neglecting to cast bottom as well. I figured the compiler promotes its type automatically to match the largest type so far - the one to the left of the binary 'or' operator. Else I would have seen a warning about incompatible types.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nonoob View Post
    I expected more of an objection about neglecting to cast bottom as well. I figured the compiler promotes its type automatically to match the largest type so far - the one to the left of the binary 'or' operator. Else I would have seen a warning about incompatible types.
    It will promote bottom.
    If there are multiple sizes in an expression, the compiler will implicitly convert all of them to the biggest size in the expression.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-13-2010, 04:58 PM
  2. creating an array of rects
    By a1dutch in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2006, 06:15 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM