R
Rod Pemberton
Keith Thompson said:Anything other than 0 is true(adjective), but true(noun) is 1.
You can only define a single value as TRUE (or true). The most
obvious value to use is 1.
Is it? Isn't _obvious_ it should -1? (-1 a.k.a. all bits set)
IMO, false is zero, true should generate -1, and non-zero values should be
accepted true. When so, then bitwise operations can be used to perform
logical operations. This doesn't affect affect C programmers directly.
But, it does affect the implementation of C, since few (or no) assembly
languages directly support logical operations or boolean operations. They
typically only support bitwise operations.
It's the value yielded by the "!",
relational, equality, "&&", and "||" operators, and it's the value
used in C99's <stdbool.h>.
That's true. But, using 1 as the only valid value "true" means you can only
use it within logical operations. What if you forget and use it with a
binary operation? Shouldn't a value of true be chosen to work with _both_
binary and logical operations? Wouldn't that be wise?
The only valid value for "false" is 0, and the only really sensible
value for "true" is 1.
I don't agree. I want my value for "true" to work correctly for both binary
and logical operations as a non-false value.
#include <stdio.h>
#define FALSE 0
/* enable TRUE you wish to test */
/* disable the others */
#if 0
#define TRUE 1
#endif
#if 0
#define TRUE (!FALSE)
#endif
#if 1
/* works for both */
#define TRUE -1
#endif
int main(void)
{
unsigned int i;
for(i=0;i<3;i++)
{
printf("\nFor %d:\n",i);
if(i&FALSE)
printf("FALSE fails as binary-false.\n");
else
printf("FALSE succeeds as binary-false.\n");
if(i&&FALSE)
printf("FALSE fails as logical-false.\n");
else
printf("FALSE succeeds as logical-false.\n");
if(i!=0)
{
if(i&TRUE)
printf("TRUE succeeds as binary-true.\n");
else
printf("TRUE fails as binary-true.\n");
if(i&&TRUE)
printf("TRUE succeeds as logical-true.\n");
else
printf("TRUE fails as logical-true.\n");
}
}
return(0);
}
HTH,
Rod Pemberton