Skip to main content
Advice
1 vote
17 replies
6k views

The following code compiles on std >= C++20 but fails to compile on std < C++20. This holds for msvc, gcc and clang. #include <cstdio> int main() { struct byte { unsigned ...
Puzomor Croatia's user avatar
10 votes
1 answer
549 views

The order of bits in C11 bitfields is implementation-defined, and so is any padding in between them. This makes it difficult to access the bitfields by index at runtime, even though most ...
jpa's user avatar
  • 12.6k
Best practices
0 votes
7 replies
193 views

I would like use bit-fields to access low level memory. I'm aware of the non-portability of bitfields but they do appear to be consistently implemented on my platform (cortex-m4) on both clang and gcc....
Lee's user avatar
  • 61
1 vote
4 answers
211 views

As far as I understand, you are allowed to access inactive members of a union, if they share a "common initial sequence" with an active one. This can be used to tag active member with a type ...
Dominik Kaszewski's user avatar
3 votes
0 answers
202 views

While investigating some spurious tests in a x86-64Bit Yocto-based Linux system, that there were multi Byte writes to a register of a PCIe card, instead of an expected single double word write. A ...
arminveres's user avatar
2 votes
2 answers
222 views

Niche optimization allows Rust to store data in the invalid bit patterns of a type. The only valid values of a bool are 0 and 1 (one bit). And Result only has two variants (one bit). So why does ...
BallpointBen's user avatar
  • 15.9k
3 votes
1 answer
145 views

Does designated initialization guarantee any omitted bitfield is initialized to zero? https://en.cppreference.com/w/c/language/struct_initialization.html says "All members that are not ...
Dominik Kaszewski's user avatar
1 vote
1 answer
109 views

I am creating wrappers for C structs containing register definitions with heavy use of bitfields. I would like to create reference getters for all of them, to provide consistent shorthand API (real ...
Dominik Kaszewski's user avatar
3 votes
1 answer
173 views

I am trying to understand how the sizeof operator works for C structures with bit-fields, particularly regarding packing and alignment. Below is a C program I wrote: #include <stdio.h> typedef ...
mauli02k's user avatar
1 vote
1 answer
127 views

Compiler: arm-gnu-toolchain-13.2.rel1-mingw-w64-i686-arm-none-eabi I use bitfields to write peripheral registers, and can only write 32-bit, so cannot use strb. I used the gcc -fstrict-volatile-...
yecheng's user avatar
  • 11
6 votes
0 answers
125 views

enum class E : int { eA }; struct S { E i : 2; }; S f() { return {.i = E::eA}; } Clang happily accepts this code, while GCC fails with: error: could not convert '{E::eA}' from '<brace-enclosed ...
Vittorio Romeo's user avatar
0 votes
1 answer
50 views

I am trying to create a gmock matcher for a structure which contains bitfields: struct s { uint32_t x; uint32_t y : 12; uint32_t z : 20; }; I can create a testing::Field(&s::x, ...
Dominik Kaszewski's user avatar
1 vote
0 answers
64 views

Given a struct with some bitfields: struct S { uint64_t bitmap1 : 4; uint64_t bitmap2 : 8; }; I would like to avoid assigning values that exceed the maximal value that can be represented by a ...
Elad Maimoni's user avatar
  • 4,811
2 votes
1 answer
133 views

In different hardware libraries for MCU I've seen (STM32 HAL, GD32 SPL, MDR32 SPL, LibOpenCM3), for operating with registers bits, logic operations (shifts, ands, ors, nots, etc) are used. But why not ...
lazba's user avatar
  • 181
3 votes
1 answer
203 views

Consider the below structure: typedef struct football_game { unsigned short num_players : 4; unsigned int num_managers : 1; unsigned short num_cameras : ...
Anton's user avatar
  • 125

15 30 50 per page
1
2 3 4 5
61