Why do comparison operators in C return 1 if the expression evaluates to true rather than -1?

Until a few days ago this question never crossed my mind. However, last week someone asked SO about a question about OpenCL C vector comparison operators. It was pretty easy to answer, and everything would stop there if someone had not commented on my answer, saying that:

It is a little annoying that scalars return 1 if the condition is true, and vectors return -1.

My first thought was "who cares as long as 0 is false and all other values ​​are true." Then I thought that it makes sense to return -1 when true, since all these bits are set: if false is 0000..000, then it is quite logical to have 1111 ... 111, which is true, and I, although it may even be useful for some bitwise operations.

Searching a little more about this topic, I found that someone suggested that in a certain state it is even faster to set all the bits instead of LSB (this was for a language other than C, and unfortunately I can not find this message).

But then, if what I just said is correct, why is the value 1 selected in C? I know that the standard says so ... but why?

Is it historical? Is it because some systems do not use two add-ons? And even then, would it be a problem for this system to return -1? In fact, is there any reason at all?

+4
source share

Source: https://habr.com/ru/post/1500693/


All Articles