News for hackers. Stuff that matters.
Re:Type-safe Bitmasks in C++ by Animats on Wednesday June 13, 2018 @02:35PM (#17305840)
That's doing it the hard way. This is the language-standard way:

    struct gamecontroller { 
        bool up: 1;
        bool down: 1;
        bool left: 1;
        bool right: 1;
        bool a: 1;
        bool b: 1;
        bool x: 1;
        bool y: 1;
        };
That's a structure with a size of one byte. Bit fields work just fine in C and C++, yet many programmers don't know this.
Re:Type-safe Bitmasks in C++ by dokem on Wednesday June 13, 2018 @02:48PM (#17305928)
Unfortunately this cannot be used to pack an integer that is sent over the wire or to produce a register for a hardware peripheral. Basically this is not a valid substitution for most use cases of bit packing and can only be used to save a bytes of memory without reverting to undefined behavior. ie, casting this memory to a byte/int is undefined. The language does not define that the first field will be the lsb, msb or some arbitrary bit. The reason "many programmers don't know this" is because its a mostly useless language feature.
Re:Type-safe Bitmasks in C++ by Animats on Wednesday June 13, 2018 @03:09PM (#17306079)
Portability is a problem. There is this:

Microsoft Specific

The ordering of data declared as bit fields is from low to high bit.

END Microsoft Specific[1]

In practice, everybody does it that way.

For fields only one byte wide, this isn't an issue for major compilers. NTP has been using this for decades without problems. But for structs more than one byte wide, there are little-endian/big endian problems. Just like with the numeric types.

This is only an issue if you intend to write the bitfield to a network, file, or device. Locally, you usually don't care about the representation.

[1] https://docs.microsoft.com/en-gb/cpp/cpp/cpp-bit-fields

Re:Type-safe Bitmasks in C++ by kolpa on Wednesday June 13, 2018 @03:40PM (#17306289)
The OP also is not a valid substitution for "most" (by what metric, anyway?) uses of bit packing.

The network wire doesn't need a C++ ABI-compatible object, and clearly isn't portable when communicating between systems running different software.

For transmitting data over network, convert from local format to network format in transit.

> can only be used to save a bytes of memory

Yes, that's what "packing" is for.

Re:Type-safe Bitmasks in C++ by dokem on Wednesday June 13, 2018 @04:24PM (#17306621)
I think you are missing my points. This language feature cannot be used to eg, create a TCP header. It has nothing to do with the C++ ABI. And no, bit packing is not for memory saving, eg MISRA disallows bitpacking for the purpose of space saving.
Re:Type-safe Bitmasks in C++ by nice_byte on Wednesday June 13, 2018 @05:14PM (#17307001)
That had been brought up elsewhere. Let me just copy-paste my response:

"I think enums allow a bit more flexibility. For example, if you need to store a piece of data that indicates which option to set, it's easier to do with an enum (can't store the "name" of the bitfield, and you're not permitted to take its address or offset either IIRC). I also like the 'Option1 | Option2 | ... ' syntax for creating bitmasks on the fly (not sure how you'd achieve that with bitfields)"

Re:Type-safe Bitmasks in C++ by kolpa on Wednesday June 13, 2018 @02:45PM (#17305918)
Does the OP offer benefits over the :1 way?