gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

OffTopic > Fun behavior from bools

#173931 - zelbo - Sat May 08, 2010 11:19 pm

figured i'd post it here since i don't expect anyone to figure it out for me, this is one i'm going to need to sort out myself, but i thought it was interesting.
i'm using something like this:
Code:
iprintf("%i", is_true());


to display some true/false info in the form of 1/0 (this should work, right?). It was working pretty good till i messed something up and now using %i to print something that should be a bool (the function is declared as a bool and returns a bool) is now displaying stuff like "48" and "19".

I don't know what i screwed up to make that happen, but i'm sure it's going to take some digging to figure this one out.

I guess i'm getting pretty sloppy now, since i'm trying to make some progress before i'm without computer access for a couple of months (probably the rest of the year). i like living in the woods, but i'm sure going to miss computers. especially cracked.com and programming.

#173936 - headspin - Sun May 09, 2010 10:57 am

If your variable returns a bool it should only have a value of 0 or 1. You could always try:

Code:
iprintf("%i", is_true() ? 1 : 0);

_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#173942 - Pete_Lockwood - Sun May 09, 2010 12:21 pm

48 sounds like ascii '0' but no clue how a function declared as returning a 'typed' bool could be returning '0'.
_________________
It's not an illusion, it just looks like one.

#173947 - zeruda - Sun May 09, 2010 5:15 pm

Sometimes when I get random behaviour that makes no sense, after banging my head on the wall for an hour I then realise I should do a complete rebuild and that fixes the issue.

#173963 - elwing - Mon May 10, 2010 8:01 am

hum, by the way a bool is:
    0 => false
    any other value => true
it is not:
    0 => false
    1 => true
    anyother value => undefined...


edit: hum, and by the way you should only use boolean as (expression) or as "true" or "false" and not their integer value... (like the "istrue()?1:0" mantionned...)

#173965 - headspin - Mon May 10, 2010 8:29 am

elwing wrote:
hum, by the way a bool is:
    0 => false
    any other value => true
it is not:
    0 => false
    1 => true
    anyother value => undefined...



Running the following in GCC 4.2

Code:
bool value = 32;

fprintf(stderr, "%d\n", value);


Outputs "1" for me. I always thought it worked as you suggested but it must depend on the compiler.
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#173966 - elwing - Mon May 10, 2010 8:47 am

headspin wrote:

Outputs "1" for me. I always thought it worked as you suggested but it must depend on the compiler.


hum, you're right it's prolly compiler dependent... not sure if ANSI says something specific about that... anyway you should never use the integer value directly...

another reason to not use the integer value is that bool basic type is not always the same (not sure if that's an error or if that's not something defined by ANSI...) ex: bool is a 32bit int under wondows CE 4.2 but a 8bit integer under windows CE 5.0

#173985 - ninjalj - Mon May 10, 2010 10:33 pm

elwing wrote:
headspin wrote:

Outputs "1" for me. I always thought it worked as you suggested but it must depend on the compiler.


hum, you're right it's prolly compiler dependent... not sure if ANSI says something specific about that...


ISO C 99 says (6.3.1.2.1):
Quote:
When any scalar value is converted to _Bool, the result is 0 if the value compares equal
to 0; otherwise, the result is 1.


IDK about C++, but it's probably the same.

#173987 - sgeos - Mon May 10, 2010 11:58 pm

I'd read up on GDB and debug it on the PC. Debuggers are awesome, and GDB isn't hard to learn.

#173989 - Drovor - Tue May 11, 2010 3:32 am

You can use "!!is_true()" to convert any true output into 1. I've never found a need for it, but thought it was a neat trick when I heard of it.

#173993 - elwing - Tue May 11, 2010 7:12 am

Drovor wrote:
You can use "!!is_true()" to convert any true output into 1. I've never found a need for it, but thought it was a neat trick when I heard of it.


lol, neat trick...
I wonder what is the best trough between this and a jump using x?1:0...
I guess its the double inversion that is faster, am I right?

that said it really does not matter as the only place where you want to get specifically a 0 or a 1 is only in traces, where performance are already completly out of frame...

#174543 - Stocky - Wed Jun 23, 2010 11:07 am

well yes its a neat trick but now new to me..i found it a year ago or so..anyways..thanks

#174544 - vuurrobin - Wed Jun 23, 2010 12:19 pm

you can also use (is_true() != false). not sure what would be the smallest or fastest.


edit: also watch out which language you use. in c++, bool is a real type which can only be true or false. but in c it is useally an enum that may have other values than specified in the enum definition.
_________________
my blog:
http://vuurrobin.100webcustomers.com/

#174547 - sajiimori - Wed Jun 23, 2010 10:59 pm

In C++, bool is an integer type. You can do arbitrary integer operations on them, such as +=. Don't assume they're either 0 or 1.

#174631 - kusma - Fri Jul 02, 2010 6:48 pm

sajiimori wrote:
In C++, bool is an integer type. You can do arbitrary integer operations on them, such as +=. Don't assume they're either 0 or 1.

C99 introduced a C++ style bool-type, so this goes for C99 and above as well. For older C-languages, there is no bool type.

#174633 - ninjalj - Sat Jul 03, 2010 2:31 pm

kusma wrote:
sajiimori wrote:
In C++, bool is an integer type. You can do arbitrary integer operations on them, such as +=. Don't assume they're either 0 or 1.

C99 introduced a C++ style bool-type, so this goes for C99 and above as well. For older C-languages, there is no bool type.


I don't know about C++, but in C99 you can assume any _Bool var (or bool, which is a macro defined in <stdbool.h>) is either 0 or 1. See my post above.

#174634 - vuurrobin - Sat Jul 03, 2010 3:45 pm

sajiimori wrote:
In C++, bool is an integer type. You can do arbitrary integer operations on them, such as +=. Don't assume they're either 0 or 1.


I just tried it in c++ and c (by including stdbool.h) using mingw, and although I could use += etc, the value of the bool stayed 1 or 0.
_________________
my blog:
http://vuurrobin.100webcustomers.com/

#174636 - sajiimori - Sat Jul 03, 2010 11:47 pm

Indeed, I just tried various ways to trick MinGW into producing a bool with a value other than 0 or 1 (besides reinterpretation which is totally cheating), and I couldn't do it.

So, at least GCC and MSVC try to guarantee that bools are 0 or 1. That's good news. Now I can't remember if I ever actually had a problem with C++ bool, or if I've just been paranoid for no reason. :)

Interestingly, MinGW doesn't trust its own booleans to be 0 or 1. Indexing into an array with "b != false" generates extra opcodes as opposed to just indexing with b. Oh well, miscellaneous trivia...

#174647 - ninjalj - Sun Jul 04, 2010 9:15 pm

sajiimori wrote:
Interestingly, MinGW doesn't trust its own booleans to be 0 or 1. Indexing into an array with "b != false" generates extra opcodes as opposed to just indexing with b. Oh well, miscellaneous trivia...


That's because the array index is an integer (technically, when subscripting an array, one of the expressions shall have type "pointer to object type", and the other shall have integer type), so the equality expression gets an integer promotion.