Hacker News new | past | comments | ask | show | jobs | submit login

This would utterly break the expectations of anyone coming from another platform, and a substantial amount of existing C code. (Whether that C code was allowed to make that assumption or not, it did.)

Leaving aside whether the behavior you propose qualifies as "right": no, I don't think there is any chance to change the encoding of "bool", any more than you can redefine the size of "char" or the value of CHAR_BITS. The world uses 8-bit bytes, the world uses twos-complement (C recently wrote that into the standard), and the world uses 0 for false and 1 for true.




> This would utterly break the expectations of anyone coming from another platform,

Which platform?

I come from x86, ppc, mips, and arm, and in all these platforms "bool's true" are sometimes ~0 and sometimes 1. For example, all SIMD operations on all these ISAs use ~0 for true, and I've fixed hundreds of bugs where people expected true to be represented by 1.

Even RISC-V Vector ISA uses `~0` for true. So if anything, what would be completely retarded is for a new ISA to sometimes use 1 for true, and sometimes use ~0. That's super confusing, and people get hit by it all the time.

Beyond the confusion of using different values for different operations, even for simple scalar code, it means that you can't just "logical and" with true to mask all bits, which is a super useful thing to do (so useful, that this is why all SIMD ISAs use ~0, and why 1 for true wouldn't be an option for SIMD).

> (Whether that C code was allowed to make that assumption or not, it did.)

Citation needed. In C, using an integer in a logical operation returns false if the integer is 0, and true otherwise. Avoiding this is _super hard_, so I doubt your claim that C code is relying on this. Also using 0 or 1 when converting to bool is handled by the compiler, and required to convert to the false and true representations of bool, where multiple true representations are allowed.

If you are serializing or deserializing raw bools, most code doing this serializes 1 bit per bool. Code serializing 4 bytes, is mostly using integer types, and when reading those testing against 0 is the most common thing to do. And well code doing this needs to deal with endianness and what not anyways.


There is a concrete advantage to using a scheme where 0 is true and all other values are false: error codes. True means the operation succeeded correctly, or false because of insufficient permissions, false because there file doesn't exist, false because of io error, false because of timeout etc. bash uses this scheme.

However, this ship has sailed for general purpose programming languages. 0 is false, all other values are interpreted as true, operations that create a bool create 1. As you say, that's just how the world works.

False = ~0 is just zany though.


> There is a concrete advantage to using a scheme where 0 is true and all other values are false: error codes. True means the operation succeeded correctly, or false because of insufficient permissions, false because there file doesn't exist, false because of io error, false because of timeout etc. bash uses this scheme.

That works perfectly fine if you write the type as an integer type and define 0 as "no error". You can't call that a C "bool" though.


> You can't call that a C "bool" though.

In hardware, C _Bool is just an scalar integer type (1 byte wide almost everywhere these days).

If you define 0 for false, and true otherwise, you can emit much better machine code for all scalar comparisons. For example, when doing scalar == 0 or scalar != 0 (e.g. in null pointer checks) the result is always the scalar itself. The test is a nop, and with "branch on non-zero" instruction, you can just directly branch.

If you define true to some value, you need to actually test whether the scalar is zero, and give it some other value otherwise. That goes from zero instructions to often two instructions (e.g. if the hardware comparison returns 0xffff and you need to convert that to 1).


> You can't call that a C "bool" though.

Correct. It was not my intent to imply otherwise. Like the person I replied to and I reiterated, that ship has sailed.


Just change your interpretation of 0 for a return value. It does not mean success, it mean there was no error. Let's not confuse error codes (enums) for booleans because they arent.


Perhaps the type you want would be called “status” instead of bool. Zero meaning success, a few bits encoding severity, and the rest available to identify specifically the error. You would then have a macro or inline called “success” to convert it to a Boolean for flow control.

Welcome to VMS!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: