4

Until few days ago this question never crossed my mind. However, last week someone asked on SO a question about comparison operators on vectors in the OpenCL C language. It was quite easy to answer and everything would have stopped there if someone wouldn't have commented my answer saying that:

It's a bit 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 the other values are true". Then I thought that it makes more sense to return -1 when true since like that all bits are set: If false is 0000..000 then it's quite logic to have 1111...111 being true, and I though it might even be useful for some bitwise operations.

Searching a bit more about this topic, I found someone suggesting that it is even faster in certain condition to set all bits instead of the LSB (It was for another language than C and unfortunately I can't find back this post).

But then, if what I've just stated is correct why in C the value 1 was chosen? I know that the standard states so... but why?

Is it historical? Is it because some systems do not use two's complement representation? And even so, would it be a problem for these system to return -1? Actually, is there any reason at all?

Community
  • 1
  • 1
CaptainObvious
  • 2,525
  • 20
  • 26
  • 2
    I don't see any logic in that `true` should be all bits set. Zero and non-zero seems more logical to me. – Some programmer dude Sep 05 '13 at 09:57
  • It doesn't make "more sense", really, since a check for equality to True is simply "*not equal to zero*", in any half-sane processor architecture. – Michael Foukarakis Sep 05 '13 at 09:57
  • Similar question here: http://stackoverflow.com/questions/6627178/c99-why-are-false-and-true-defined-as-0-and-1-and-not-as-bool0-and-bool. Looks like it's to do with compatibility to boolean types? – SpaceDog Sep 05 '13 at 10:02
  • 4
    I think arithmetically 1 makes more sense than -1. – Medinoc Sep 05 '13 at 10:02
  • 1
    something that must be accepted. like 3 comes after 2 :) – sr01853 Sep 05 '13 at 10:04
  • 1
    1 bit can represent exactly 2 distinct values, (e.g. true/false), so that would likely be the first choice if you were to store a boolean value. As to *exactly* why Dennis Ritchie chose that is probably lost to history, but it was likely one of the many things inherited from the B language which did the same. – nos Sep 05 '13 at 10:10
  • -1 is the _bitwise logical_ option, since expression `if (a) b; else c;` can be evaluated as `a & b | ~a & c`; where all a,b,c have the same word size. – Aki Suihkonen Sep 05 '13 at 10:36
  • I think @nos has it spot on. Just using one bit for true/false is much simpler than using the entire N bits for two's compliment. It may also be slightly faster to use one bitwise operation to check if the first bit is one rather than using some other method to check if the number is -1. – charmlessCoin Sep 05 '13 at 10:52
  • `-1` is not all bits one on all machines. it depends on the sign representation. Only if converted to `unsigned` the new value is `UINT_MAX` with all bits set to one. So this is certainly not a natural choice, or at least wasn't when architecture with different sign represetation were more common. – Jens Gustedt Sep 05 '13 at 12:34
  • If true had to be `1111 1111` and false be `0000 0000`, then a bit-error would easily invalidate your `true/false` bipolarity and introduces 254 potential unknown states... – Shark Sep 05 '13 at 13:12
  • @Joachim, Michael: I agree with you, zero and non-zero is quite logical and I didn't want to call into question this concept. Also "more sense" was most probably not the best expression. My thought was that based on the fact that the systems that I know (of course there is a lot of system that I don't know) use two's complement and that you have to choose a value to return for true among all the possible values why not choose -1 (~0). So, I was expecting an explanation somewhere in the line: "it would screw up thing for some sys using the x representation" or "back then the problem was..." – CaptainObvious Sep 05 '13 at 13:17
  • @SpaceDog: I saw this question before posting this one. I doesn't answer my question at all since it concerns the use of the type bool to define true and false. Besides if I well recall, the bool type was introduce with C99 way after the choice of returning 1 for true was made. – CaptainObvious Sep 05 '13 at 13:22
  • @Medinoc: I agree with you. If it's the reason behind choosing the 1 as returned value for true, then my question would be why -1 for OpenCL vectors comparison. Or in other word why choose what makes sense at the bit level over what makes sense arithmetically speaking for OCL. I can only guess that some performance consideration would be involved...maybe – CaptainObvious Sep 05 '13 at 13:29
  • @sr01853: If you'd posted that as answer, most probably I would have accepted it :D. If you want to make you comment an answer you better to hurry though: one more vote and this question is closed ;) – CaptainObvious Sep 05 '13 at 13:34
  • @nos: Do you mean that the choice was made to take into account that at that time some systems were storing the value only in one bit and not in bytes? – CaptainObvious Sep 05 '13 at 13:38
  • @AkiSuihkonen: I'm aware of this. That why I asked this question: from the bitwise operations POV it'd make sense, but as Medinoc pointed out it makes more sense from the arithmetic POV. – CaptainObvious Sep 05 '13 at 13:42
  • @CaptainObvious I would recon no - that the value was rather chosen for the logical reason that you only need 2 values to represent true/false, given the normal convention in [digital systems](http://en.wikipedia.org/wiki/Digital_electronics)(relates to bits) and [boolean algebra](http://en.wikipedia.org/wiki/Boolean_algebra) of using 0 as false and 1 as true, and the lack of any superior alternative, it makes a natural choice, But we will never know – nos Sep 05 '13 at 14:11

0 Answers0