Sign in to follow this  
loadsaEmone

Odd boolean return - "50" and "227"

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
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
[quote]
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++, [i]bool[/i] is a small integral type, generally the same size as [i]char[/i]. 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
[quote name='rip-off' timestamp='1317766854' post='4869116']
In C++, [i]bool[/i] is a small integral type, generally the same size as [i]char[/i]. As such, it will typically have a range of 256 values.
[/quote]
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this