#### Archived

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

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

## 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 on other sites
I''m assuming you''re talking about C / C++. Anything non-zero is true.

##### Share on other sites
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 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 on other sites
OK, maybe you are right, I was just wondering...

##### 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 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 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 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 on other sites
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! ]

##### Share on other sites
quote:
Original post by Sandman
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.

Say what?

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

##### Share on other sites
I am sorry.

What I ment with "stay in memory" was the following: due to the cosmic radiation there is a change that a 1 can become a 0 and a 0 can become a 1. If you have 8, 16 or 32 one''s you are for sure that the change that the helve of them will change to a 0 is close to that zero.

That is what I ment and I am sure that this problem is common. Maybe it is better for some apps to sacrefice some space so you know for sure that a value stays in the memory...

##### Share on other sites
quote:
Original post by SabreMan
It''s a macro.

Actually, in C99 it''s a type _Bool large enough to store the values 0 and 1. The spec states that it''s a standard unsigned integer type of the lowest rank of all integer types. In stdbool.h the macro bool expands to _Bool, true expands to 1, false expands to 0 and __bool_true_false_are_defined expands to 1. See section 7.16 of the C99 spec.

##### Share on other sites
quote:
Original post by Frankie68
What I ment with "stay in memory" was the following: due to the cosmic radiation there is a change that a 1 can become a 0 and a 0 can become a 1. If you have 8, 16 or 32 one''s you are for sure that the change that the helve of them will change to a 0 is close to that zero.

There is also a chance that due to cosmic radiation, you will transform into a mutant ninja turtle, but the probability is sufficiently small to not worry too much about.

If the probability of memory getting corrupted is that highh then the odd bool coming up true instead of false is the least of your worries - there is the program code itself, pointer addresses etc. which will have a much more serious impact on the programs operation than a few boolean variables.

If you were writing mission critical software which would be expected to operate in an area with a great deal of radiation, you would expect your hardware to be heavily shielded.

SabreMan - it was supposed to be a silly joke, but it ended up surprisingly close to what Frankie really meant...

##### Share on other sites
Just a fact I read last weekend in a popular sceince magazine:

Every twelve hours there crashes a computer because of that radiation. So, the problem is more non-trivial than that it may seem at first.

But Sandman is right: the chance that a bool is being hit is much smaller than the change a pointer or the code is being hit. But still: why take a risk that could be made much smaller?

[edited by - Frankie68 on May 1, 2002 11:57:44 AM]

##### Share on other sites
quote:
Original post by Frankie68
Every twelve hours there crashes a computer because of that radiation. So, the problem is more non-trivial than that it may seem at first.

Given the number of computers in the world, I would say that every twelve hours is a pretty low incidence rate.

##### Share on other sites
quote:
Actually, in C99 it''s a type _Bool large enough to store the values 0 and 1.

Yes, _Bool is a type. However "bool" is a macro which expands to _Bool.

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

##### Share on other sites
also considering the fact that many cpus have at least a parity bit to detect corruption of memory (some go as far to be able to correct it) all without your knowledge or care.

just to let you know, when you get a parity error, your bios/motherboard/os tend to lock up completely with the error on display. i have had this happen once on my 12yr 386 when i lowered the refresh rate for the ram through some software. parity errors are ussauly caused by something quite wrong with the pc somewhere. like overheating for one. many servers run 24/7 with only 15mins downtime the WHOLE year. if a pc can run staright for almost an entire year without crashing, then it safe to assume that its a rare occurence that you dont need to worry about.

if your worried about a single bit getting flipped by radiation, then i suggest you turn your pc off and go off to do something else. being paranoid about things you cant control is not going to get you anywhere fast,

##### Share on other sites
quote:
Original post by Frankie68
I am sorry.

What I ment with "stay in memory" was the following: due to the cosmic radiation there is a change that a 1 can become a 0 and a 0 can become a 1. If you have 8, 16 or 32 one''s you are for sure that the change that the helve of them will change to a 0 is close to that zero.

That is what I ment and I am sure that this problem is common. Maybe it is better for some apps to sacrefice some space so you know for sure that a value stays in the memory...

It isn''t common. Don''t concern yourself with it.

##### Share on other sites
I always keep two sets of all my data and whenever they don''t agree I print out an "radiation error"

##### Share on other sites
Thanks guys for providing me with some entertainment.

##### Share on other sites
HAHAHAHAHAHAHAHAHA

OMG, pass the crackpipe dude and lol @ everyone who tried to honestly answer original question when it was obvious the guy was on crack.

##### Share on other sites
quote:
Original post by Maxxes
I always keep two sets of all my data and whenever they don't agree I print out an "radiation error"

But wait -- if what Frankie68 says is true, there's also a chance due to cosmic radiation that BOTH values will change in the same way so that the "radiation errors" could go undetected. I think your best chance would be to keep an infinite number of copies of all program data... that's the only way you'll be able to reduce the chance of radiation affecting your memory to zero.

LOL

EDIT: You know, it occurred to me that even if you were to concede that your 'TRUE' values were more safe by having more 1's, your false could still 'easily' (if you really think that there's a chance of this.. heh) change to a 'true', as you would only need one bit to change false->true . This is seriously a rather ridiculous thread. But it made me laugh

-pirate_dau

[edited by - pirate_dau on May 1, 2002 7:42:34 PM]

##### Share on other sites
quote:
Original post by Frankie68
Just a fact I read last weekend in a popular sceince magazine:

Every twelve hours there crashes a computer because of that radiation. So, the problem is more non-trivial than that it may seem at first.

But Sandman is right: the chance that a bool is being hit is much smaller than the change a pointer or the code is being hit. But still: why take a risk that could be made much smaller?

Hmmm....one out of how many computers??? Are you including pocket calculators, too? What about my beloved consoles (and handhelds)?

Perhaps you should inform Stephen Hawking of this grave danger to the computers of the world. Quick--to the Bat Cave!!!

:D

-----------------------------------
"Is the size of project directly proportional to the amount of stupidity to be demonstrated?"
-SabreMan

##### Share on other sites
The explanation for true=1 and false=0 has everything to do with the circuitry. When there''s an electric charge, it is noted as ON, represented by 1. When there''s no charge, it is noted as OFF, represented by 0. This is also why power switches are marked with | and O. | means it''s ON, O means it''s OFF. It''s just a convention used during the birth of circuits.

• ### Forum Statistics

• Total Topics
628305
• Total Posts
2981962

• 9
• 12
• 11
• 12
• 11