September 13, 2022

On Tuesday, 13 September 2022 at 20:06:55 UTC, Steven Schveighoffer wrote:

>

On 9/13/22 3:47 PM, Don Allen wrote:

>

I would also add that talking about user-friendly/unfriendly doesn't make a lot of sense unless you state the purpose of the literal. If I wanted to initialize an int to the number of states in the US, no one sane would write

int n_us_states = 0b110010

If I were defining a mask to extract a field from a hardware register, I might use a binary literal, though I personally would use the shifting technique I described in an earlier post.

Agreed. The purpose is important.

If I wanted to specify an "every third bit set" mask, in hex it would be 0x924924924.... But in binary it is 0b100100100100.... The second version is immediately clear what it is, whereas the first is not.

While hex is usually clearer than decimal, it's not always as clear as binary.

BTW, you know how I figured out that 924 pattern? In the easiest way possible of course!

writefln("%x", 0b100100100100100100100100);

So you used 0b notation to come up with a justification for 0b notation :-)

I do this sort of thing with an HP calculator.

>

-Steve
September 13, 2022
On 9/11/22 16:00, Steven Schveighoffer wrote:

> I'm actually quite proud of the bit shifting code, I tried to find the
> most efficient/clever mechanism to test for a straight given a bitfield.
> Once I thought of it, it was simple to implement and understand:
>
> https://github.com/schveiguy/poker/blob/6f70cf7ca74e19e78b470f81640a3ce34a95d0d3/source/poker/hand.d#L245-L256

That's very clever! :)

Ali

September 13, 2022
On 9/13/2022 1:06 PM, Steven Schveighoffer wrote:
> If I wanted to specify an "every third bit set" mask, in hex it would be `0x924924924...`. But in binary it is `0b100100100100...`. The second version is immediately clear what it is, whereas the first is not.

Is it? How do you know it didn't overflow the int and create a long? How do you know you filled up the int?

It's pretty clear the hex one is a long.


September 13, 2022
On Tue, Sep 13, 2022 at 01:43:44PM -0700, Walter Bright via Digitalmars-d wrote:
> On 9/13/2022 1:06 PM, Steven Schveighoffer wrote:
> > If I wanted to specify an "every third bit set" mask, in hex it would be `0x924924924...`. But in binary it is `0b100100100100...`. The second version is immediately clear what it is, whereas the first is not.
> 
> Is it? How do you know it didn't overflow the int and create a long? How do you know you filled up the int?
[...]

Simple, use `_`:

	0b1001_0010_0100

This makes it immediately obvious exactly how many bits the literal occupies.


T

-- 
Javascript is what you use to allow third party programs you don't know anything about and doing you know not what to run on your computer. -- Charles Hixson
September 13, 2022

On 9/13/22 4:43 PM, Walter Bright wrote:

>

On 9/13/2022 1:06 PM, Steven Schveighoffer wrote:

>

If I wanted to specify an "every third bit set" mask, in hex it would be 0x924924924.... But in binary it is 0b100100100100.... The second version is immediately clear what it is, whereas the first is not.

Is it? How do you know it didn't overflow the int and create a long? How do you know you filled up the int?

How do you know the purpose is to fill up an int?

>

It's pretty clear the hex one is a long.

It's not a specific example with actual requirements for an existing problem. The point is that the sequence 924 isn't as clear that every 3rd bit is set vs. 100.

Have you never started with "I need a number that has these properties", and then proceeded to build that number?

Like if you started with "I need a number that's all 9 digits in decimal" you wouldn't try to figure it out in hex, right? You'd just write 99999.... And if you need it to fit in an int, you figure that out (probably with trial-and-error). You don't start with "well, I can't use decimal, because then I'll never know if it fits in an int!"

Same thing with binary. It allows me to express certain numbers without thinking or figuring too hard. Like building a number that has n consecutive bits set (i.e. the poker example). Or if you have a register that has sets of odd-length bit patterns.

The list of things that it helps with is not large. It's also not completely eclipsed by hex. And unlike the horrible C octal syntax, it's not error-prone.

IMO, enough to counter any justification for removal, or hoisting into an expensive library implementation. It's not even a different type, it costs nothing, because everything happens in the lexer. Removing this feature is so insignificant in terms of compiler/language "savings", and significant in breaking existing code. It just shouldn't even be considered for removal.

-Steve

September 13, 2022
On Tuesday, 13 September 2022 at 20:43:44 UTC, Walter Bright wrote:
> On 9/13/2022 1:06 PM, Steven Schveighoffer wrote:
>> If I wanted to specify an "every third bit set" mask, in hex it would be `0x924924924...`. But in binary it is `0b100100100100...`. The second version is immediately clear what it is, whereas the first is not.
>
> Is it? How do you know it didn't overflow the int and create a long? How do you know you filled up the int?
>
> It's pretty clear the hex one is a long.

It just as easy with binary literals as D supports the _ delimiter 0xb0100100_10010010_01001001_00100100. You can use the _ as you prefer. It is actually irrelevant what you think, if D would have been a commercial project you would have supported it because some customers would have demanded it. You would put a student on it to implement it during a weekend for a few dollars and movie ticket.

I can't believe this discussion. Initial motivation was to save a few lines of code, now the discussion more or less like what color of your underwear that is the best.
September 13, 2022

On 9/13/22 4:12 PM, Don Allen wrote:

>

On Tuesday, 13 September 2022 at 20:06:55 UTC, Steven Schveighoffer wrote:

>

On 9/13/22 3:47 PM, Don Allen wrote:

>

I would also add that talking about user-friendly/unfriendly doesn't make a lot of sense unless you state the purpose of the literal. If I wanted to initialize an int to the number of states in the US, no one sane would write

int n_us_states = 0b110010

If I were defining a mask to extract a field from a hardware register, I might use a binary literal, though I personally would use the shifting technique I described in an earlier post.

Agreed. The purpose is important.

If I wanted to specify an "every third bit set" mask, in hex it would be 0x924924924.... But in binary it is 0b100100100100.... The second version is immediately clear what it is, whereas the first is not.

While hex is usually clearer than decimal, it's not always as clear as binary.

BTW, you know how I figured out that 924 pattern? In the easiest way possible of course!

writefln("%x", 0b100100100100100100100100);

So you used 0b notation to come up with a justification for 0b notation :-)

I used it to figure out what the pattern would be in hex. The arguments here are that binary is not needed because hex has it covered.

Like if I get an error on windows of -1073741819, I have to put it into hex to see what the true error is (in hex, you can recognize the pattern of 0xc0000005)

-Steve

September 13, 2022
On 9/13/2022 2:04 PM, Steven Schveighoffer wrote:
>> Is it? How do you know it didn't overflow the int and create a long? How do you know you filled up the int?
> 
> How do you know the purpose is to fill up an int?

Ok, I'll rephrase that. How do you know when to stop?

There's a reason hex is so ubiquitous. It's compact. Binary literals beyond a few digits (8 max) are more or less unreadable. Yes, the _ can extend it to more digits before it becomes unreadable. (Even long hex numbers benefit from _, again, after 8 digits.)


September 13, 2022

On 9/13/22 8:35 PM, Walter Bright wrote:

>

On 9/13/2022 2:04 PM, Steven Schveighoffer wrote:

> >

Is it? How do you know it didn't overflow the int and create a long? How do you know you filled up the int?

How do you know the purpose is to fill up an int?

Ok, I'll rephrase that. How do you know when to stop?

Because I'm done making the mask. In this specific situation, I'm only testing 9 bits.

0b100100100 // obvious, clear, easy

0x? // have to calculate using 0b numbers hint: it's not 924

>

There's a reason hex is so ubiquitous. It's compact. Binary literals beyond a few digits (8 max) are more or less unreadable. Yes, the _ can extend it to more digits before it becomes unreadable. (Even long hex numbers benefit from _, again, after 8 digits.)

But it doesn't disprove the fact that sometimes, hex digits aren't as clear.

  1. binary numbers are sometimes clearer given the context
  2. binary numbers already are a thing in D
  3. there is no ambiguity with a binary number literal. The 0b prefix is obvious.

-Steve

September 13, 2022
On 9/13/2022 7:56 PM, Steven Schveighoffer wrote:
> But it doesn't disprove the fact that *sometimes*, hex digits aren't as clear.

Does sometimes justify a language feature, when there are other ways?

People often complain that D has too many features. What features would you say are not worth it?