Odd boolean return - "50" and "227"

Started by
4 comments, last by loadsaEmone 12 years, 6 months ago
So I have a boolean that controls which direction an image is facing and a function which can flip it. I initialize the boolean fo false, but the images seem to randomly flip back and forth.

When I try to output the value of the boolean to console, it gives me a value of "227" for the first image and "50" for all subsequent images that I put on screen.

This confuses me because, as I understand it, a boolean can only hold a value of 0 or 1 and even if I did give it some junk value that wasn't zero, it would still resolve to 1.

I've tried searching on the internet for an answer, but to no avail.

I will post code if necessary.

Thanks.
Advertisement
It sounds like a buffer overrun or some other misaddressed memory issue, but it would be impossible to say for certain without seeing code. It also wouldn't hurt to mention things like what language and libraries you are using.

This confuses me because, as I understand it, a boolean can only hold a value of 0 or 1 and even if I did give it some junk value that wasn't zero, it would still resolve to 1.
[/quote]
In C++, bool is a small integral type, generally the same size as char. As such, it will typically have a range of 256 values. In a conditional context, such as an if, while or for statement, it behaves like any other integral value, 0 is "false" and anything else is "true".

In C++, bool is a small integral type, generally the same size as char. As such, it will typically have a range of 256 values.

No, a C++ bool only has two values: true and false. In the context of an integral conversion true converts to 1 and false converts to 0.
Sure.

I was talking about under the hood though (which I should have made clearer). Your answer explained how the errant value was likely set, my explanation was aimed at showing that bool != 1 bit, which the quoted text hinted at.

Apologies for the confusion.
Ok I got it. I wasn't initializing the variable properly.

This topic is closed to new replies.

Advertisement