A signed 1-bit type?

ryuukk_ ryuukk.dev at gmail.com
Fri Sep 22 16:19:09 UTC 2023


On Tuesday, 19 September 2023 at 12:37:59 UTC, Quirin Schroll 
wrote:
> I don’t know if what I’m going to say is trivial or 
> interesting, but I had this thought only recently, thinking 
> about the design of an integer type system.
>
> In Visual Basic (and probably more Basic dialects), booleans 
> convert to integers as `False` → 0 and `True` → −1. You read 
> that correctly, it’s *minus* one. A weird choice, isn’t it? But 
> could it come from a basic principle?
>
> Yes. If we think of booleans as 1-bit numeric types, it boils 
> down to the question of mere signedness. If we assume 2’s 
> complement, a signed 1-bit type has the values −1 and 0; that’s 
> how 2’s complement works. Of course an unsigned 1-bit type has 
> the values 0 and 1.
>
> I cannot answer you why you’d want a signed 1-bit type, though.
>
> D’s booleans, however, are unsigned integer types.

On Tuesday, 19 September 2023 at 12:37:59 UTC, Quirin Schroll 
wrote:
> I don’t know if what I’m going to say is trivial or 
> interesting, but I had this thought only recently, thinking 
> about the design of an integer type system.
>
> In Visual Basic (and probably more Basic dialects), booleans 
> convert to integers as `False` → 0 and `True` → −1. You read 
> that correctly, it’s *minus* one. A weird choice, isn’t it? But 
> could it come from a basic principle?
>
> Yes. If we think of booleans as 1-bit numeric types, it boils 
> down to the question of mere signedness. If we assume 2’s 
> complement, a signed 1-bit type has the values −1 and 0; that’s 
> how 2’s complement works. Of course an unsigned 1-bit type has 
> the values 0 and 1.
>
> I cannot answer you why you’d want a signed 1-bit type, though.
>
> D’s booleans, however, are unsigned integer types.

Perhaps arbitrary bit-width integers could be a solution

```
u1
u2
u5
u18

i1
i2
i5
i18

etc..
```




More information about the Digitalmars-d mailing list