比特移位可移植性

时间:2016-12-04 21:20:17

标签: c++ bit-manipulation portability

我的问题是直接的,这段代码是否可移植?

#include  <cstdint>

#ifndef  ECS_INT
  #define  ECS_INT  uint32_t
#endif

#ifndef  ECS_MAX_NB_COMPONENTS
  #define  ECS_MAX_NB_COMPONENTS  255
#endif

static constexpr uint8_t  FIND_MAX_NUMBER_OF_BITS(uint64_t base) {

  //! Round to upper pow2
  base--;
  base |= base >> 1;
  base |= base >> 2;
  base |= base >> 4;
  base |= base >> 8;
  base |= base >> 16;
  base |= base >> 32;
  base++;

  //! Check bits number
  uint8_t  counter = 0;
  while (!(base & (1 << counter)))
    ++counter;
  return counter;
}

static constexpr const ECS_INT  INVALID_INDEX           = ((ECS_INT) - 1);
static constexpr const uint8_t  ECS_INT_MAX_BITS        = FIND_MAX_NUMBER_OF_BITS(INVALID_INDEX) + 1;
static constexpr const uint8_t  ECS_COMPONENT_MAX_BITS  = FIND_MAX_NUMBER_OF_BITS(ECS_MAX_NB_COMPONENTS);

我不是有关比特的专家,但我认为这种语言可以让它变得便携。或者我应该使用类似std::bitset的东西?

0 个答案:

没有答案