Provide byte-order swapping functions.
More...
Namespaces |
namespace | bsls |
Detailed Description
- Outline
-
-
- Purpose:
- Provide byte-order swapping functions.
-
- Classes:
-
- See also:
- Component bsls_byteorder
-
- Description:
- This component provides a utility
class
, bsls::ByteOrderUtil
, that contains a suite of static functions for reversing the byte order of integral types. The functions swapByteOrder{16,32,64}
reverse the byte order of words having the indicated widths (in bits), while the overloaded function swapBytes
will swap the bytes of any integral type passed to it, returning the same type it is passed.
-
- Usage:
- In this example we demonstrate the use of different overloads of the
swapBytes
function.
- First we
typedef
a shorthand to the namespace class
: Then, we demonstrate reversing the bytes of an unsigned short
: unsigned short us = 0x1234;
assert(0x3412 == Util::swapBytes(us));
Next, we do a signed 'short: short ss = 0x4321;
assert(0x2143 == Util::swapBytes(ss));
Then, we reverse an unsigned int
: unsigned int ui = 0x01020304;
assert(0x04030201 == Util::swapBytes(ui));
Next, we reverse the bytes of a 32-bit signed integer: int si = 0x11223344;
assert(0x44332211 == Util::swapBytes(si));
Now, we perform the transform on a 64-bit unsigned: bsls::Types::Uint64 ui64 = 0x0102030405060708ULL;
assert(0x0807060504030201ULL == Util::swapBytes(ui64));
Finally, we do a 64-bit signed integer: bsls::Types::Int64 i64 = 0x0a0b0c0d0e0f0102LL;
assert(0x02010f0e0d0c0b0aLL == Util::swapBytes(i64));