Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. This should be an easy question... 128-bit blues.

This should be an easy question... 128-bit blues.

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
9 Posts 4 Posters 121 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeremy Falcon
    wrote on last edited by
    #1

    Last time I did hardcore C was a while back. Before the 128-bit days. Ok, cool. But, I got a silly question when it comes to printing a 128-bit integer. You see online examples saying just do a `long long` cast and call it a day bro, but then they use small numbers. Which obviously works for them because it's a small number. But, I figure hey, I'll try it for poops and giggles. As you'd might expect the number is never correct.

    #include

    int main()
    {
    __uint128_t u128 = 34028236692093846346337460743176821145LL;
    printf("%llu\n", (unsigned long long)u128);

    return 0;
    

    }

    The above don't do it. Now, I could bit shift to get around any limits, but I ultimately need the output formatted with comma separators, so doing bit logic would cause issues when putting humpty dumpty back together again. So, um... anyone know how to print a 128-bit number in C? Preferably portable C.

    Jeremy Falcon

    K Mircea NeacsuM 2 Replies Last reply
    0
    • J Jeremy Falcon

      Last time I did hardcore C was a while back. Before the 128-bit days. Ok, cool. But, I got a silly question when it comes to printing a 128-bit integer. You see online examples saying just do a `long long` cast and call it a day bro, but then they use small numbers. Which obviously works for them because it's a small number. But, I figure hey, I'll try it for poops and giggles. As you'd might expect the number is never correct.

      #include

      int main()
      {
      __uint128_t u128 = 34028236692093846346337460743176821145LL;
      printf("%llu\n", (unsigned long long)u128);

      return 0;
      

      }

      The above don't do it. Now, I could bit shift to get around any limits, but I ultimately need the output formatted with comma separators, so doing bit logic would cause issues when putting humpty dumpty back together again. So, um... anyone know how to print a 128-bit number in C? Preferably portable C.

      Jeremy Falcon

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      As far as I know, the C standard does not specify anything about 128 bit integers. _uint128_t is a GCC extension, also supported by clang. Maybe MSVC too? As far as I know, there's no printf format code for 128 bit integers either. See here for a possible solution: [https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc\](https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc)

      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

      J 1 Reply Last reply
      0
      • K k5054

        As far as I know, the C standard does not specify anything about 128 bit integers. _uint128_t is a GCC extension, also supported by clang. Maybe MSVC too? As far as I know, there's no printf format code for 128 bit integers either. See here for a possible solution: [https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc\](https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc)

        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

        J Offline
        J Offline
        Jeremy Falcon
        wrote on last edited by
        #3

        Yeah that's what I was afraid of. If it wasn't for the formatting, I'd just slice it up as two 64s to be portable for printf. I may just have to roll my own formatting for something I'm working on. I mean, I could just forget about 128-bit support, but no cool points for that. :laugh: Thanks btw.

        Jeremy Falcon

        1 Reply Last reply
        0
        • J Jeremy Falcon

          Last time I did hardcore C was a while back. Before the 128-bit days. Ok, cool. But, I got a silly question when it comes to printing a 128-bit integer. You see online examples saying just do a `long long` cast and call it a day bro, but then they use small numbers. Which obviously works for them because it's a small number. But, I figure hey, I'll try it for poops and giggles. As you'd might expect the number is never correct.

          #include

          int main()
          {
          __uint128_t u128 = 34028236692093846346337460743176821145LL;
          printf("%llu\n", (unsigned long long)u128);

          return 0;
          

          }

          The above don't do it. Now, I could bit shift to get around any limits, but I ultimately need the output formatted with comma separators, so doing bit logic would cause issues when putting humpty dumpty back together again. So, um... anyone know how to print a 128-bit number in C? Preferably portable C.

          Jeremy Falcon

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          Have you looked at GMP? It's not for the faint of heart but it should do what you want - Formatted Output Strings (GNU MP 6.3.0)[^]

          Mircea

          J 1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            Have you looked at GMP? It's not for the faint of heart but it should do what you want - Formatted Output Strings (GNU MP 6.3.0)[^]

            Mircea

            J Offline
            J Offline
            Jeremy Falcon
            wrote on last edited by
            #5

            Never even heard of it. :omg: Does it handle localization? If so, you're totally my hero.

            Jeremy Falcon

            Mircea NeacsuM 1 Reply Last reply
            0
            • J Jeremy Falcon

              Never even heard of it. :omg: Does it handle localization? If so, you're totally my hero.

              Jeremy Falcon

              Mircea NeacsuM Offline
              Mircea NeacsuM Offline
              Mircea Neacsu
              wrote on last edited by
              #6

              It should. Per above reference:

              Quote:

              The decimal point character (or string) is taken from the current locale settings on systems which provide localeconv (see Locales and Internationalization in The GNU C Library Reference Manual). The C library will normally do the same for standard float output.

              Mircea

              J 1 Reply Last reply
              0
              • Mircea NeacsuM Mircea Neacsu

                It should. Per above reference:

                Quote:

                The decimal point character (or string) is taken from the current locale settings on systems which provide localeconv (see Locales and Internationalization in The GNU C Library Reference Manual). The C library will normally do the same for standard float output.

                Mircea

                J Offline
                J Offline
                Jeremy Falcon
                wrote on last edited by
                #7

                Schweet. I'll have to check it out. Thanks man.

                Jeremy Falcon

                Mircea NeacsuM 1 Reply Last reply
                0
                • J Jeremy Falcon

                  Schweet. I'll have to check it out. Thanks man.

                  Jeremy Falcon

                  Mircea NeacsuM Offline
                  Mircea NeacsuM Offline
                  Mircea Neacsu
                  wrote on last edited by
                  #8

                  One is glad to be of service.

                  Mircea

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    CharHen
                    wrote last edited by
                    #9

                    Using 128 bit ints

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups