Converting an unsigned int to to unsigned long long (64 bit)

This is a discussion on Converting an unsigned int to to unsigned long long (64 bit) within the C Programming forums, part of the General Programming Boards category; Originally Posted by swoopy >With gcc on my Windows XP box this works: Thanks for the info Dave. Maybe I've ...

  1. #16
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by swoopy
    >With gcc on my Windows XP box this works:
    Thanks for the info Dave. Maybe I've got an old version of gcc. I'll try your exact code tomorrow with Dev-C++ and see what happens.

    >(If you run gcc -pedantic, it mentions that ISO C90 does not support the 'll' printf modifier.)
    I'll give that a try too, and see if that warning shows.
    Well, I don't use dev-cpp for windows console apps and I hadn't tried it. (I use command line cygwin gcc for most things.)

    Here's what I found about printing 64-bit ints with dev-cpp:

    No need to "update". I doubt that it would change things. Format type specifiers can be found (the "right way") in <inttypes.h> (which includes <stdint.h>)

    Here's something that runs on dev-cpp and other gnu gcc on my Windows XP box:

    Code:
    #include <stdio.h>
    #include <inttypes.h>
    
    uint64_t make_longlong(uint32_t, uint32_t);
    
    int main ()
    {
    
      uint64_t start, end, diff;
      uint32_t hi, lo;
    
    
      hi = 0x12345678;
      lo = 0x9abcdef0;
      start = make_longlong(hi, lo);
    
      hi = 0x55555555;
      lo = 0x11111111;
      end = ((uint64_t)hi << 32) | lo;
    
      diff = end - start;
    
      printf("end   = %"PRIu64" (%"PRIx64" hex)\n",end, end);
      printf("start = %"PRIu64" (%"PRIx64" hex)\n",start, start);
      printf("diff  = %"PRIu64" (%"PRIx64" hex)\n",diff, diff);
    
      getchar();
    
      return 0;
    }
    
    /* if you want to make a function to hide the bit shifting 
     * here's a possibility
     */
    uint64_t make_longlong(uint32_t hi, uint32_t lo)
    {
      return ((uint64_t)hi << 32) | lo;
    }
    output is

    Code:
    end   = 6148914690091192593 (5555555511111111 hex)
    start = 1311768467463790320 (123456789abcdef0 hex)
    diff  = 4837146222627402273 (4320fedc76543221 hex)
    Regards,

    Dave
    Last edited by Dave Evans; 02-11-2005 at 09:18 AM.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Here's something that runs on dev-cpp and other gnu gcc on my Windows XP box:
    Bingo, that works Dave. So the trick is to use the format specifier %"PRIu64".

    Here's my question. Why does %llu work for you? You're running the same OS (Windows XP), and the same compiler as Dev-C++ uses. Is it something to do with cygwin?

    By the way, I did go ahead and upgrade to Dev-C++, which looks like was worth it.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well, maybe not the same compiler. cygwin gcc is probably more like the unix version. Would that be correct?

  4. #19
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by swoopy
    Well, maybe not the same compiler. cygwin gcc is probably more like the unix version. Would that be correct?
    I don't have any insight into the differences. With the dev-cpp compiler, gcc --version gives this:
    gcc (GCC) 3.3.1 (mingw special 20030804-1)
    With cygwin's gcc I get this:

    gcc (GCC) 3.3.3 (cygwin special)
    I was thinking maybe different library versions.

    In the <inttypes.h> for dev-cpp it says, for example:
    Code:
    #define PRIu64 "I64u"
    In <inttypes.h> for cygwin, it says

    Code:
    #define PRIu64 "llu"
    Anyhow, using the data types from <stdint.h> and format specifiers from <inttypes.h> we at least have portability among these two C compilers.

    [edit]
    Forgot to mention: My examples also work on my Linux Fedora 3 Box

    gcc --version gives this:

    gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)
    And <inttypes.h> has stuff like this:

    Code:
    # if __WORDSIZE == 64
    #  define __PRI64_PREFIX    "l"
    #  define __PRIPTR_PREFIX    "l"
    # else
    #  define __PRI64_PREFIX    "ll"
    #  define __PRIPTR_PREFIX
    # endif
    
    
    # define PRIu64        __PRI64_PREFIX "u"
    # define PRIx64        __PRI64_PREFIX "x"
    (The Linux version also works with %ll, obviously.)
    [/edit]

    Regards,

    Dave
    Last edited by Dave Evans; 02-11-2005 at 11:23 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21