by MontyCarloHall · 5y ago · 0 comments
const unsigned magical_number = 0b0110'0001'0110'0011'0110'0001'0110'0010;
Finally. It’s mystifying to me why this took so long to add; not everyone is fluent in translating hex digits to binary nybbles on the fly. This will greatly help with clearly defining and manipulating bitfields. The fact that digit separators can be placed arbitrarily will further help delimit fields of variable width, e.g. # A B C D
uint8_t flags = 0b00’1’0’11’01;
A and B are binary fields; C and D are quaternary fields. The first two bits are padding. This is a lot more readable than uint8_t flags = 0x2D;I predict people will use it for a while, then abandon it.
https://github.com/ityonemo/Primes/blob/f157b63d91375c62f110...
I've also verified that binary representations of unusual datatypes are correct and have the expected properties (~>5 yr ago, paid gig, actually my first paid gig):
https://github.com/interplanetary-robot/SigmoidNumbers/blob/...
https://github.com/interplanetary-robot/SigmoidNumbers/blob/...
Extremely happy with it in all cases. You can't convince me otherwise. Zig and Julia would be poorer languages if not for binary literals. My life would have been more annoying without it. It's a tool I have reached for in the past and will continue reaching for when appropriate and tasteful, I have not "abandoned it". At least, I'm one datapoint that refutes your assertion.
I haven’t used the C2x version, but one thing I expect them to be useful for is self-documentation of enums representing bitmask values.
struct Foo{
bool some_flag : 1;
bool other_flag : 1;
....
}
?Turned out that it was actually significantly faster to use one byte per boolean and forgo the masking operations. I assume the processor was just good enough at keeping its cache filled in that particular workload, so the additional masking operations just slowed things down. So I understand why you might not want a compiler to automatically do this.
struct X {
_BitInt(1) a;
_BitInt(1) b;
};
do now?