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

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

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

    This isn't really a C question, but I'm hoping some people here can help anyway. I have 2 32 bit unsigned ints representing a 64 bit unsigned int. I want to save the value to a string so I can print it out. My first thought was

    Code:
    sprintf(val, "%u", ((top << 32) | bottom));
    But that doesn't work because I shift the top half right out of the memory space for the int. Anyone have any thoughts on how this can be accomplished, or links to point me to that discuss dealing with 64 bit values on a 32 bit system?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A long int on a 32-bit system is 8 bytes.
    Code:
    	printf("%d", sizeof(long));
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by homer_3 View Post
    Anyone have any thoughts on how this can be accomplished, or links to point me to that discuss dealing with 64 bit values on a 32 bit system?
    A 32 bit system doesn't limit the size of available datatypes to 4 bytes. As MK suggested you have 64 bit integers readily available, you can even go crazy with it and use a long double for 128 bits (no int tho).

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by homer_3 View Post
    This isn't really a C question, but I'm hoping some people here can help anyway. I have 2 32 bit unsigned ints representing a 64 bit unsigned int. I want to save the value to a string so I can print it out. My first thought was

    Code:
    sprintf(val, "%u", ((top << 32) | bottom));
    But that doesn't work because I shift the top half right out of the memory space for the int. Anyone have any thoughts on how this can be accomplished, or links to point me to that discuss dealing with 64 bit values on a 32 bit system?
    Save the two 32-bit ints into a 64-bit variable (type long long), then stringify and save to a char array using sprintf().

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MK27 View Post
    A long int on a 32-bit system is 8 bytes.
    Code:
    	printf("%d", sizeof(long));
    depends on compiler... on most 64 bit long type is

    long long
    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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    A long int on a 32-bit system is 8 bytes.
    Code:
    	printf("%d", sizeof(long));
    What system would that be?
    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.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Elysia View Post
    What system would that be?
    I think it's FictitiOS


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by quzah View Post
    I think it's FictitiOS


    Quzah.
    I have it here actually, mac os X 10.6.3, gcc 4.2.1

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's not a 32 bit OS. Actually ... it's a confused OS. It doesn't look like it knows for sure what it is. It looks like it either may or may not be booted with 64 bit extensions. If you've got them running -- and more than likely you do if size of long is 64 bits -- then you're in 64 bit mode.

    Quzah.
    Last edited by quzah; 04-05-2010 at 05:30 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    No, I'm booting the 32 bit kernel. The thing is, from what I understand the reason for this is that 3rd party peripherals will be allowed some time to port their drivers.

    Any way, the size of long (or rather if it's name is long, or long long) should have nothing to do with pointer sizes, 64 bits then for the addressing.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Are you talking about Mac OS X? You mean it checks drivers to see if all are 64 bit or not and if not it loads 32bit kernel?

    BTW, are you all agreed that long long is always 64bits?
    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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by siavoshkc View Post
    BTW, are you all agreed that long long is always 64bits?
    Yes. It's standardized in C99 and C++0x.
    In previous standards, all compilers that support long long makes them 64-bit AFAIK.
    Last edited by Elysia; 04-06-2010 at 03:21 PM.
    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.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by siavoshkc View Post
    Are you talking about Mac OS X? You mean it checks drivers to see if all are 64 bit or not and if not it loads 32bit kernel?
    Yes mac os x, as far as I'm aware it boots with a 32 bit kernel by default on all macs but xserve at this point. My guess is that it will stay that way until a majority of 3d party device drivers have been ported to 64 bit. You can boot it into a 64bit kernel by pressing 6 4 keys on a restart though, or change the boot args.
    Last edited by Subsonics; 04-06-2010 at 09:11 AM.

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by Elysia
    Yes. It's standardized in C99 and C++0x.
    On previous standards, all compilers that support long long makes them 64-bit AFAIK.
    Is a 128 bit integer defined in the standard?
    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

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by siavoshkc
    Is a 128 bit integer defined in the standard?
    No, though there is nothing in the C and C++ standards that prevent an implementation from providing a 128 bit integer type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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