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. Big Endian and small endian

Big Endian and small endian

Scheduled Pinned Locked Moved C / C++ / MFC
question
2 Posts 2 Posters 0 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.
  • N Offline
    N Offline
    nachilau
    wrote on last edited by
    #1

    Hello All, Can someone suggest a fast ways to convert big endian to small endina? Thanks! Nachi

    P 1 Reply Last reply
    0
    • N nachilau

      Hello All, Can someone suggest a fast ways to convert big endian to small endina? Thanks! Nachi

      P Offline
      P Offline
      peterchen
      wrote on last edited by
      #2

      there is a bswap ASM instruction, however, this might not be the fastest way on all architectures.

      DWORD SwapEnidan(DWORD x)
      {
      __asm mov eax, x
      __asm bswap eax
      __asm mov x, eax
      return x;
      }

      Another option (that could be better if you have to convert many values and unroll the loop by two) would be splitting into bytes and shifting around:

      DWORD x = ...;
      DWORD swapped = ((x & 0x000000FF) << 24) |
      ((x & 0x0000FF00) << 8) |
      ((x & 0x00FF0000) >> 8) |
      ((x & 0xFF000000) >> 24));

      I don't think that XCHG or "manually" swapping bytes is faster.


      we are here to help each other get through this thing, whatever it is Vonnegut jr.
      sighist || Agile Programming | doxygen

      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