News for hackers. Stuff that matters.
Re:Progress on C23 by MontyCarloHall on Sunday September 05, 2021 @10:35AM (#28424636)
>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;
Re:Progress on C23 by WalterBright on Sunday September 05, 2021 @05:10PM (#28427833)
I 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.

Re:Progress on C23 by dnautics on Sunday September 05, 2021 @09:16PM (#28429320)
They are fantastic for writing tests against any function that might need to bit twiddle.
Re:Progress on C23 by WalterBright on Sunday September 05, 2021 @10:25PM (#28429645)
Yes, that is the promise. Try it for a while, you'll see what I mean.
Re:Progress on C23 by dnautics on Monday September 06, 2021 @01:35AM (#28430662)
Already 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/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.

Re:Progress on C23 by WalterBright on Monday September 06, 2021 @04:24AM (#28431608)
> I'm one datapoint that refutes your assertion.

There's always one!

BTW, D does support 0b:

https://dlang.org/spec/lex.html#BinPrefix

Re:Progress on C23 by dnautics on Monday September 06, 2021 @10:39AM (#28434098)
It's highly unlikely I will ever use a language whose creator very wrongly assumes I don't know what I'm talking about when I talk about programming ergonomics.
Re:Progress on C23 by setpatchaddress on Monday September 06, 2021 @02:08AM (#28430893)
I’ve used them for almost 20 years in Ruby, several years in Swift. I do find them useful in those contexts.

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.

Re:Progress on C23 by WalterBright on Monday September 06, 2021 @04:26AM (#28431618)
> useful for is self-documentation of enums representing bitmask values.

I used to do that, and wound up back using hex. Hex is easier to read for me, as I don't have to count the digits.

Re:Progress on C23 by 12thwonder on Sunday September 05, 2021 @12:05PM (#28425338)
why not

    struct Foo{
        bool some_flag : 1;
        bool other_flag : 1;
        ....
    }

?
Re:Progress on C23 by MontyCarloHall on Sunday September 05, 2021 @12:49PM (#28425667)
Because 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
Re:Progress on C23 by isidor3 on Sunday September 05, 2021 @02:27PM (#28426495)
I 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.

Re:Progress on C23 by coolreader18 on Sunday September 05, 2021 @01:27PM (#28425990)
Not sure if they edited the comment after you commented, but that is a bitfield now, at least
Re:Progress on C23 by MontyCarloHall on Sunday September 05, 2021 @01:34PM (#28426057)
You're right, I didn't see the :1's. Maybe it was edited, maybe I was just blind?
Re:Progress on C23 by Ericson2314 on Sunday September 05, 2021 @01:35PM (#28426060)
what does

  struct X {
    _BitInt(1) a;
    _BitInt(1) b;
  };
do now?