TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 227 subscribers
Post #23972 21
Declaring bit fields with position as well as number of bits

I would love it if I could specify the bit position as well as the number of bits in a bit field, something like:

struct S
{
uint32_t x : 0, 5; // Starts at position 0, size is 5 so goes up to position 4
uint32_t z : 18, 3; // Starts at position 18, size is 3 so goes up to position 20
uint32_t y : 5, 11; // Starts at position 5, size is 11 so goes up to position 15
}

Does anyone know if there are any proposals in the works to add something like this?

Of course there are many pitfalls (e.g. error/warn/allow overlapping fields?) but this would be useful to me.

I considered building some template monstrosity to accomplish something similar but each time I just fool around with padding fields.

https://redd.it/1nxnjqr
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Oct 17, 2026The Double Headed Stack Allocator – The blog at the bottom of the sea https://blog.demofox…
  2. Oct 17, 2026Barry's C++ Blog - Generating Operator Functions https://brevzin.github.io/c++/2026/10/14/…
  3. Oct 17, 2026MSVC Build Tools Version 14.52 Release Candidate Now Available https://devblogs.microsoft.…
  4. Oct 17, 2026C++ futures part 4: What if I want an immediate on-thread std::async? https://devblogs.mic…
  5. Oct 14, 2026I tested whether a coding agent would use C++23 ranges without being told Blog post: https…
  6. Oct 14, 2026C++ futures part 1: The mental model https://devblogs.microsoft.com/oldnewthing/20261012-0…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →