Boost logo

Boost :

Subject: [boost] [endian] Alternative implementation of endian_flip
From: Mike Patterson (mike_at_[hidden])
Date: 2011-03-14 15:38:12


I wrote the attached code years ago. When I noticed the proposed Boost
Endian library, I figured now was as good a time as any to share it. My code
provides an alternative solution to Beman's boost::integer::endian_flip
function that may or may not be a useful improvement. My implementation
exposes a single function template, boost::endian::swap, that accepts any
type, but only swaps the endianness of 16-bit, 32-bit, and 64-bit types.

Features:

1. Slightly more compact.

2. Accepts any type, not just u?int(16|32|64)_t.
+ This might be useful for unions/structs, e.g., MSVC's LARGE_INTEGER, and
when writing function templates.
- Decreases the chance that the compiler can identify improper use (e.g.,
"swap(my_64_bit_object);").

3. Uses bit shifting/masking instead of a temporary variable.
~ I'm not sure if this is any better or worse from the compiler's
perspective.
- My 64-bit implementation may not work on older compilers.

-Mike




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk