4 ms·
>I should also note that C23 will also have Binary Integer Literals, so the same number can be written out in a more precise grouping of binary as well: cons
by MontyCarloHall 5y ago
>I should also note that C23 will also have Binary Integer Literals, so the same number can be written out in a more precise grouping of binary as well:
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;
- 12thwonder 5y agowhy not struct Foo{ bool some_flag : 1; bool other_flag : 1; .... } ?
- MontyCarloHall 5y agoBecause a struct of bools != a bitfield. C booleans as defined in stdbool.h are just convenience macros aliasing "true" to 8 bit integer 1 and "false" to 0. AFAIK, there aren't any compilers smart enough to implicitly pack a struct of bools into a bitfield, and then make member access operations implicitly mask the bitfield. Here's a quick example, using the latest GCC: https://godbolt.org/z/c7T54jzGT https://godbolt.org/z/c7T54jzGT
- coolreader18 5y agoNot sure if they edited the comment after you commented, but that is a bitfield now, at least
- MontyCarloHall 5y agoYou're right, I didn't see the :1's. Maybe it was edited, maybe I was just blind?
- deleted 5y ago[deleted]
- isidor3 5y agoI wrote a little solver for some programming puzzle. Thought I was being clever by using bitfields for an array of booleans to reduce memory usage and bandwidth, as they seemed a natural fit for the problem I was solving. 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.
- Ericson2314 5y agowhat does struct X { _BitInt(1) a; _BitInt(1) b; }; do now?
- WalterBright 5y agoI added 0b binary literals to the Datalight C compiler nearly 40 years ago, and put them in D, too. It sounds like a great idea, but it turns out to just not be useful in practice. One of the problems is the unwieldy length of them (and yes, you can group them in D with _ ). I predict people will use it for a while, then abandon it.
- dnautics 5y agoThey are fantastic for writing tests against any function that might need to bit twiddle.
- WalterBright 5y agoYes, that is the promise. Try it for a while, you'll see what I mean.
- dnautics 5y agoAlready have, there are many more like that on a branch I haven't committed yet. I'm very happy with it: https://github.com/ityonemo/Primes/blob/f157b63d91375c62f11051dbd484615dbd17045c/PrimeZig/solution_3/src/wheel.zig#L163 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/master/test/Posits5-test.jl https://github.com/interplanetary-robot/SigmoidNumbers/blob/... https://github.com/interplanetary-robot/SigmoidNumbers/blob/master/test/warlpiri-recasting.jl 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.
- WalterBright 5y ago