On Sunday, 18 September 2022 at 12:15:17 UTC, Max Samukha wrote:>
On Sunday, 18 September 2022 at 11:00:12 UTC, Loara wrote:>
Because it seems that some people really needs binary literals and can't work without them although there are many higher level alternatives.
Yes, we need them because they have use cases where alternatives are not justifiable. For example, we have a simple communication protocol that defines just a handful of commands with additional information encoded in the bits (command length, flags, etc). It would be unreasonable to complicate the code with bit ops, parsers, etc., which would make it less readable. Having to replace the literals with
bin!... would be tolerable, though.
You can use hex literals behind an high level interface that hides them behind enums/aliases, in this way users won't need to know the position of each bit when they'll use your library.
Also if you need to change the order of two bits for some reason you'll just change 2/3 enums and your code will work anyway. In general it's better to hide constants behind an alias instead of copy them in several places.