C/C++: Force Bit Field Order and Alignment

No, it will not be fully-portable. Packing options for structs are extensions, and are themselves not fully portable. In addition to that, C99 ยง6.7.2.1, paragraph 10 says: “The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined.” Even a single compiler might lay the bit field out … Read more

What is CHAR_BIT?

CHAR_BIT is the number of bits in char. These days, almost all architectures use 8 bits per byte but it is not the case always. Some older machines used to have 7-bit byte. It can be found in <limits.h>.

How do I write a maintainable, fast, compile-time bit-mask in C++?

Best version is c++17: template< unsigned char… indexes > constexpr unsigned long long mask(){ return ((1ull<<indexes)|…|0ull); } Then void apply_known_mask(std::bitset<64> &bits) { constexpr auto m = mask<B,D,E,H,K,M,L,O>(); bits &= m; } back in c++14, we can do this strange trick: template< unsigned char… indexes > constexpr unsigned long long mask(){ auto r = 0ull; using … Read more

Best practices for circular shift (rotate) operations in C++

See also an earlier version of this answer on another rotate question with some more details about what asm gcc/clang produce for x86. The most compiler-friendly way to express a rotate in C and C++ that avoids any Undefined Behaviour seems to be John Regehr’s implementation. I’ve adapted it to rotate by the width of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)