Why is true == 0x1 and false == 0x0?

Started by
31 comments, last by Frankie68 21 years, 11 months ago
Just a question: true is equal to 000000000000000000000000000001 and false is equal to 0000000000000000000000000000000. Why isn''t true equal to 111111111111111111111111111111? THat uses the same amount of memory, but you are sure that the value stays in the memory. You just count the number of one''s and if it is more than 16, you are sure that it is a true and with false vice versa. It will be just as fast and it will be better, wouldn''t it be? OK, I don''t knor it for sure, but maybe someonewill know...
Advertisement
I''m assuming you''re talking about C / C++. Anything non-zero is true.
OK,

I mean C++, but I mean if you are using bool''s:


bool yesorno = true;
int test = int(yesorno) // is 1, ok...
int test2 = yesorno // is also 1, could be much better if it was 0xffffffff...
in general... false is 0 and true is -1, because -1 is 1111111111111111. de msb (most significant bit) is -32767 and all the other 1''s make it -1. to check it, the processor has to do a different function which is faster that just getting a bit, checking which value and jumping to the according address.

but then again... this question now exists for more than 20 years and i am sure they thought about it very very well... so why bother?

REALITY.SYS corrupted, reboot UNIVERSE? [y/n]
REALITY.SYS corrupted, reboot UNIVERSE? [y/n]
OK, maybe you are right, I was just wondering...
A bool is (technically) a binary value, a single bit. You cannot store 0xFFFFFFFF in a single bit, it just gets truncated to 0x1.

Although practically speaking, compilers usually treat bools as just ints or chars (compiler dependent) it is still technically a single bit value, and this is sometimes enforced. (for example, bool vectors/bitfields etc) If we defined true as you suggest, then you would get an inconsistency between cases where a single bit is used and where a larger data type is used.

Also, there is no bool type in C. It is often typedefed as an int.

[edited by - Sandman on May 1, 2002 10:04:16 AM]
What on earth are you talking about? In C++, true is equal to true and false is equal to false. A bool type can undergo an integral promotion to int, where true becomes 1 and false becomes 0. There is only 1 bit of significance in a bool, although a compiler will have to use at least the smallest addressable unit of machine storage. I''m not really sure what you''re on about with the value "staying in memory".

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
I think the general definitions of bool are an expression of the expression evaluation from C.

zero is false, anything non-zero is true

You shouldn''t worry about what value true is - if you are doing so you might want to reconsider.

Also, I think C99 or the upcoming new C standard has bool as a built in type - although I doubt many straight C compilers will end up supporting it, but who knows?


quote:Original post by SabreMan
I''m not really sure what you''re on about with the value "staying in memory".


Sometimes, a very small 1 can look almost identical to a large 0. If you only have a single 1 to look at, and it just happens to be a small 1, then the computer might mistake it for a 0 and get confused.
quote:Original post by SteveC
Also, I think C99 or the upcoming new C standard has bool as a built in type - although I doubt many straight C compilers will end up supporting it, but who knows?

It''s a macro.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

This topic is closed to new replies.

Advertisement