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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. swap bytes in a ushort/ulong

swap bytes in a ushort/ulong

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
7 Posts 3 Posters 3 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
    Jurgen Eidt 0
    wrote on last edited by
    #1

    What is the fasted way to swap bytes in a ushort/ulong? For example: 0x1234 -> 0x3412 0x12345678 -> 0x78563412

    unsigned short short_exch(const unsigned short a)
    {
        // 0x1234 -> 0x3412
        return (a << 8) + (a >> 8);
    };
    
    
    unsigned long long_exch(unsigned long a)
    {
        // 0x12345678 -> 0x78563412
    
        unsigned long t = a&0xff;
        a >>= 8;
        t <<= 8;
        t += a&0xff;
    
        a >>= 8;
        t <<= 8;
        t += a&0xff;
    
        a >>= 8;
        t <<= 8;
    
        return t + a;
    };
    

    Jürgen Eidt http://cpicture.de/en

    R G 2 Replies Last reply
    0
    • J Jurgen Eidt 0

      What is the fasted way to swap bytes in a ushort/ulong? For example: 0x1234 -> 0x3412 0x12345678 -> 0x78563412

      unsigned short short_exch(const unsigned short a)
      {
          // 0x1234 -> 0x3412
          return (a << 8) + (a >> 8);
      };
      
      
      unsigned long long_exch(unsigned long a)
      {
          // 0x12345678 -> 0x78563412
      
          unsigned long t = a&0xff;
          a >>= 8;
          t <<= 8;
          t += a&0xff;
      
          a >>= 8;
          t <<= 8;
          t += a&0xff;
      
          a >>= 8;
          t <<= 8;
      
          return t + a;
      };
      

      Jürgen Eidt http://cpicture.de/en

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      Try a union : typedef union { ULONG itemlong; UCHAR itemchar[4]; } UnionLong; then you can swap the bytes through a temporary variable. a two cent stamp short of going postal.

      J 1 Reply Last reply
      0
      • R Rick York

        Try a union : typedef union { ULONG itemlong; UCHAR itemchar[4]; } UnionLong; then you can swap the bytes through a temporary variable. a two cent stamp short of going postal.

        J Offline
        J Offline
        Jurgen Eidt 0
        wrote on last edited by
        #3

        Didn't thought of a union :) But even swapping with the xor trick I'm not sure how the compiler optimization would be. I guess looking at the asm code would make the final judgement. Thanks. Jürgen Eidt http://cpicture.de/en

        1 Reply Last reply
        0
        • J Jurgen Eidt 0

          What is the fasted way to swap bytes in a ushort/ulong? For example: 0x1234 -> 0x3412 0x12345678 -> 0x78563412

          unsigned short short_exch(const unsigned short a)
          {
              // 0x1234 -> 0x3412
              return (a << 8) + (a >> 8);
          };
          
          
          unsigned long long_exch(unsigned long a)
          {
              // 0x12345678 -> 0x78563412
          
              unsigned long t = a&0xff;
              a >>= 8;
              t <<= 8;
              t += a&0xff;
          
              a >>= 8;
              t <<= 8;
              t += a&0xff;
          
              a >>= 8;
              t <<= 8;
          
              return t + a;
          };
          

          Jürgen Eidt http://cpicture.de/en

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          unsigned long Swap(unsigned long value)
          {
          return ((value & 0xFF000000) >> 24) |
          (value & 0x00FF0000) >> 8) |
          (value & 0x0000FF00) << 8) |
          (value & 0x000000FF) << 24);
          }


          Software Zen: delete this;

          J 1 Reply Last reply
          0
          • G Gary R Wheeler

            unsigned long Swap(unsigned long value)
            {
            return ((value & 0xFF000000) >> 24) |
            (value & 0x00FF0000) >> 8) |
            (value & 0x0000FF00) << 8) |
            (value & 0x000000FF) << 24);
            }


            Software Zen: delete this;

            J Offline
            J Offline
            Jurgen Eidt 0
            wrote on last edited by
            #5

            Thanks Gary, that a good way to do it! Jürgen Eidt http://cpicture.de/en

            G 1 Reply Last reply
            0
            • J Jurgen Eidt 0

              Thanks Gary, that a good way to do it! Jürgen Eidt http://cpicture.de/en

              G Offline
              G Offline
              Gary R Wheeler
              wrote on last edited by
              #6

              You're welcome. If you make the function inline, the compiler will compile it in place without the function call overhead (I forgot to do that).


              Software Zen: delete this;

              J 1 Reply Last reply
              0
              • G Gary R Wheeler

                You're welcome. If you make the function inline, the compiler will compile it in place without the function call overhead (I forgot to do that).


                Software Zen: delete this;

                J Offline
                J Offline
                Jurgen Eidt 0
                wrote on last edited by
                #7

                Of course inline! Actually you can remove the masking for the 24 bit shifts. For the inner two bytes I don't see an improvement. Thanks Gary for the follow up :) Jürgen Eidt http://cpicture.de/en

                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