linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steve Graegert <graegerts@gmail.com>
To: "Robert P. J. Day" <rpjday@mindspring.com>
Cc: C programming list <linux-c-programming@vger.kernel.org>
Subject: Re: canonical byte swap macros?
Date: Mon, 11 Jul 2005 21:44:04 +0200	[thread overview]
Message-ID: <6a00c8d505071112442b9d83b7@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.63.0507102100500.9524@localhost.localdomain>

On 7/11/05, Robert P. J. Day <rpjday@mindspring.com> wrote:
> 
>   is there a standard set of byte swap macros/functions for doing
> big/little endian conversion?  i have no interest in re-inventing the
> wheel and i'm sure there's a universally-recognized set of macros for
> this, no?
> 

For quick and dirty solutions I sometimes use one of the following
(not favoured over any other solution, it just work for me):

#define swap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)

#define swap_32(x) \
     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))

uint64_t swap_64(uint64_t x) {
    union { 
        uint64_t ll;
        uint32_t l[2]; 
    } w, r;
    w.ll = x;
    r.l[0] = bswap_32 (w.l[1]);
    r.l[1] = bswap_32 (w.l[0]);
    return r.ll;
}


Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

      parent reply	other threads:[~2005-07-11 19:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-11  1:02 canonical byte swap macros? Robert P. J. Day
2005-07-11  2:48 ` Jeff Woods
2005-07-11 19:23 ` Glynn Clements
2005-07-11 19:44 ` Steve Graegert [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a00c8d505071112442b9d83b7@mail.gmail.com \
    --to=graegerts@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=rpjday@mindspring.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).