• Advertisement
Sign in to follow this  

Odd boolean return - "50" and "227"

This topic is 2328 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

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.

Share this post


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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement