Boost logo

Boost :

Subject: Re: [boost] Official boost macro to determine endianess?
From: Frédéric Bron (frederic.bron_at_[hidden])
Date: 2017-04-05 12:23:23


> I think the modern way to do this is to use Boost.Predef[1].

I used predef in a small program (see below) but I'm surprised by the output:
BOOST_ENDIAN_BIG_BYTE
value= 0102
memory=0201

predef says I am big endian but I see that most significant byte
(0x01) is stored at the end which seems to indicate little endian.
My processor is: Intel(R) Core(TM) i7-6700HQ

Frédéric

Program:
#include <cstdint>
#include <iomanip>
#include <iostream>

#include <boost/predef.h>

int main() {
#if defined(BOOST_ENDIAN_BIG_BYTE)
  std::cerr << "BOOST_ENDIAN_BIG_BYTE\n";
#elif defined(BOOST_ENDIAN_BIG_WORD)
  std::cerr << "BOOST_ENDIAN_BIG_WORD\n";
#elif defined(BOOST_ENDIAN_LITTLE_BYTE)
  std::cerr << "BOOST_ENDIAN_LITTLE_BYTE\n";
#elif defined(BOOST_ENDIAN_LITTLE_WORD)
  std::cerr << "BOOST_ENDIAN_LITTLE_WORD\n";
#endif

  std::cout << std::hex << std::setfill('0');

  std::uint16_t value = 0x0102;
  std::cout<<"value= "<<std::setw(4)<<value<<'\n';

  auto c = reinterpret_cast<const std::uint8_t *>(&value);
  std::cout<<"memory=";
  for (int i = 0; i < (int)sizeof(value); ++i) {
    std::cout << std::setw(2) << static_cast<int>(c[i]);
  }
  std::cout << '\n';

  return 0;
}


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