Why does std::bitset expose bits in little-endian fashion?

后端 未结 2 551
一向
一向 2020-12-20 15:28

When I use std::bitset::bitset( unsigned long long ) this constructs a bitset and when I access it via the operator[], the bits seems to b

相关标签:
2条回答
  • 2020-12-20 16:00

    This is consistent with the way bits are usually numbered - bit 0 represents 20, bit 1 represents 21, etc. It has nothing to do with the endianness of the architecture, which concerns byte ordering not bit ordering.

    0 讨论(0)
  • 2020-12-20 16:01

    There is no notion of endian-ness as far as the standard is concerned. When it comes to std::bitset, [template.bitset]/3 defines bit position:

    When converting between an object of class bitset<N> and a value of some integral type, bit position pos corresponds to the bit value 1<<pos. The integral value corresponding to two or more bits is the sum of their bit values.

    Using this definition of bit position in your standard quote

    initializing the first M bit positions to the corresponding bit values in val

    a val with binary representation 11 leads to a bitset<N> b with b[0] = 1, b[1] = 1 and remaining bits set to 0.

    0 讨论(0)
提交回复
热议问题