904 questions
Advice
1
vote
17
replies
6k
views
Surprising behaviour of using static_cast on struct with bit fields
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 ...
10
votes
1
answer
549
views
Is this a well-defined way to access bitfield bits by index in C11
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 ...
Best practices
0
votes
7
replies
193
views
How to efficiently initialize a volatile struct
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....
1
vote
4
answers
211
views
Does union "common initial sequence" include bitfields?
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 ...
3
votes
0
answers
202
views
What is causing multi single-Byte access to packed Bit-field on GCC as opposed to single double word access on Clang?
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 ...
2
votes
2
answers
222
views
Niche optimization: why is `size_of::<Result<bool, bool>>()` 2 instead of 1?
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 ...
3
votes
1
answer
145
views
Designated initialization with omitted bitfield
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 ...
1
vote
1
answer
109
views
Any way to create a bitfield reference wrapper in a generic way?
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 ...
3
votes
1
answer
173
views
Understanding sizeof for C Structures with bit-fields: alignment and packing
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 ...
1
vote
1
answer
127
views
When the bitfield is written in the for loop, the behavior of -fstrict-volatile-bitfields is incorrect and the strb instruction is generated
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-...
6
votes
0
answers
125
views
List initialization of bitfield enum class member fails under GCC but not Clang -- who's right?
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 ...
0
votes
1
answer
50
views
Use of gmock Field matcher with bitfields
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, ...
1
vote
0
answers
64
views
Is it possible to deduce the number of bits in a bitfield? [duplicate]
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 ...
2
votes
1
answer
133
views
Why do MCU libraries use logic operations instead of bitfield structs?
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 ...
3
votes
1
answer
203
views
Clarification - Struct Bitfield memory layout
Consider the below structure:
typedef struct football_game {
unsigned short num_players : 4;
unsigned int num_managers : 1;
unsigned short num_cameras : ...