Binary Literals

I would love to see binary literals in Elm. We have decimal literals (1337) and hexadecimal literals (0xFF) for now. But in some cases, binary literals would help to produce clearer code. Since we now got elm/bytes, we have more use-cases for low-level bit manipulation.

One example where binary literals are helpful to produce clearer code is bitmasking for multiple bits at once. To check if bits 2, 3 and 4 are set, we currently have to write Bitwise.and 0x1C value == 0x1C. In this case it’s not obvious which bits are being checked for. If we had binary literals, it could look like Bitwise.and 0b11100 value == 0b11100 where the checked bits are written down explicitly.

For now, I always write masks like this in hexadecimal and add a comment with the actual bit-pattern for better readability.

I am dabbling with implementing DEFLATE which can then be used to implement elm-zip or elm-gzip. With that project, binary literals are even more helpful, as you have to work with groups of bits smaller than 8 a lot.

I thought I bring this issue up and see what the community thinks about this. Maybe this can lead to binary literals being implemented or at least a start a discussion why they shouldn’t.

10 Likes

I think it’s a good idea. And also to support grouping for number literals, like 0b1010_0000 or 5_000_000.

One question is what elm-format would do. The Elixir equivalent formatter inserts those separators for numbers longer than 5 digits automatically. For binary it would make sense to group by four.

5 Likes

And also to support grouping for number literals, like 0b1010_0000

Rust supports this exact syntax, and I’ve found it extremely helpful when working at the bit level.

In Rust I have the choice of using this or hexadecimal literals, and I’ve found I always reach for 0b over 0x. It’s longer to read, but I don’t have to do the mental translation of “hang on, which bits are in which places for 0xC again?”

1 Like

I’ve never used the grouping before, but it seems like a good addition. For what it’s worth, besides Rust, C# and Python also have the same exact syntax for grouping unsing the underscore.

One question is what elm-format would do.

What you proposed seems fine to me. But in the end, I really don’t care as long as elm-format makes it consistent. I never did this before, but I really like that elm-format expands hexadecimal literals to full bytes (0xF becomes 0x0F) and I got quickly used to it. I expect it would be the same for grouping. :slight_smile:

I also support this idea … when writing code using Bytes it would be helpful when writing tests and when experimenting.

@Malax, I wish you luck with doing elm-zip if you do that. Were you the one who suggested I try doing elm-tar instead of elm-zip? Indeed much simpler and it fits my needs. (I have no plans to do elm-zip).

Hexadecimal syntax is great when you’re building something that either is hexadecimal in nature itself, or the specification you are encoding in your programming language specifies its values in hexadecimal.

And the same is true for working with binary syntax.

I am definitely in favour for adding binary (and potentially hexadecimal) literals to the language., because it is a way to make certain algorithms and design decisions more explicit and readable.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.