Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Frankie68

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

This topic is 5922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
Advertisement
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...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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! ]

Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!