#Question about booleans being assigned ints

1 messages · Page 1 of 1 (latest)

kind remnant
#

When dealing with a bool and assigning an int to it, can I assume that the int is always converted to 1 or 0?

I'm asking this in the context of a case like this where I want to substract bools without doing (bool)?1:0 and use bool directly

int main() {
  bool x = 0;
  bool y = 2;
  
  int z = y - x;
  if(z==1) {
    printf("Okay");
  } else {
    printf("weird");
  }
  
  return 0;
}
versed frostBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

tropic onyx
#

!cref _Bool

versed frostBOT
tropic onyx
#

Screenshot cause I can't be bothered restoring all that formatting:

#

;compile

#include <stdbool.h>
#include <stdio.h>

bool x = 4;
printf("%d\n", x);
dull anchorBOT
#
Program Output
1
tropic onyx
#

@kind remnant As you can see it's always either 1 or 0.
Also if you're dealing with plain integers that you want to reduce to 1 or 0 if truthy or falsy, then there's 2 simpler ways:

// Say you have an integer x of which you want it's boolean value
// Then you can either do:
(bool) x;  
// or:
(_Bool) x;
// to cast the variable to the bool equivalent
// or you can double negate:
!!x;
kind remnant
#

Hmmmm okay I see

#

And do you think (bool)?1:0 makes stuff slower or nah? Ik that if it does it would be such a tiny thing, but my code is already slow because I run so many simulations, and the end goal is for it to be as fast as possible. The reason I might want to keep (bool)?1:0 is just for readability when doing int operations on bools

#

And like the part of the code what would run the most times would be the part containing this ternary operator

severe pivot
#

it's probably more proper to say it's 0 and non-zero.

tropic onyx
torn token
#

Conditional statements such as if take 0 as false and anything else as true

kind remnant
tropic onyx
kind remnant
#

I wanted to convert the bool to an int for arithmetic int operations

kind remnant
#

Yeah right

#

!solved

versed frostBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

tropic onyx
kind remnant
#

Yeah

#

One more question actually

#

When I add an int and a bool without casting the bool or anything, does the bool get implicitly cast to int or does smth else happen?

tropic onyx
#

;asm -Og

int f(_Bool a, _Bool b) {
    return a + b;
}
dull anchorBOT
#
Assembly Output
f:
  movzx edi, dil
  movzx esi, sil
  lea eax, [rdi+rsi]
  ret

kind remnant
#

I'm not sure what lea does

#

But I love this

tropic onyx
#

lea := load effective address

#

movzx := move + zero extend

#

dil := lowest 8 bits of the 64-bit rdi register (edi for the lowest 32 bits)

#

sil := lowest 8 bits of the rsi register (esi for the lowest 32-bits)

kind remnant
#

Hmmm you probably know more abt assembly than me but it seems like casting would do the same right? Bc at the end of the day it's dealing with bytes and not types right?

tropic onyx
#

I'm not actually entirely sure what happens here, but it does look like some 8-bit addition, which means I'm very curious in the result myself, too

#

;compile

int f(_Bool a, _Bool b) {
    return a + b;
}

int main() {
    __builtin_printf("f(true, true) = %d\n", f(1, 1));
}
dull anchorBOT
#
Program Output
f(true, true) = 2
kind remnant
#

Hmmmm shouldn't f(true, true) work?

tropic onyx
#

Okay yeah, so it actually does evaluate to 2, but I wouldn't rely on these implicit conversions

tropic onyx
kind remnant
#

Just to make things clear too'

#

Thank you so much

tropic onyx
#

Let's see what it does if compiled with -O0:

#

;asm -O0

int f(_Bool a, _Bool b) {
    return a + b;
}
dull anchorBOT
#
Assembly Output
f:
  push rbp
  mov rbp, rsp
  mov edx, edi
  mov eax, esi
  mov BYTE PTR [rbp-4], dl
  mov BYTE PTR [rbp-8], al
  movzx edx, BYTE PTR [rbp-4]
  movzx eax, BYTE PTR [rbp-8]
  add eax, edx
  pop rbp
  ret

tropic onyx
#

Yeah, here we get a better picture of how a stupid computer thinks

kind remnant
#

What's rbp? And like does push like push on the stack?

tropic onyx
#

And yeah, push pushes an element on the stack and automatically decreases rsp

kind remnant
#

And btw doesn't like the output assembly code depend on the compiler? And don't cpus have different assembly language sets? I mean this is what I was taught in school. You know, mips and stuff.

tropic onyx
kind remnant
#

Ooh okay

tropic onyx
#

And then there are also two different notations.
Once there is the Intel syntax, i.e. the one the compiler here uses by default aswell as any sane person (just kidding on the latter part, but it's the only one I learned).
And on the other side the AT&T syntax.

#

But yeah, most of the time you will work on CISCs (complex instruction set computers) rather than RICSs (reduced instruction set computers) and most of the time it will likely just be x86, although x86-64 is more likely.

kind remnant
#

Nice

#

It's interesting

#

Who cares abt web dev stuff lol

tropic onyx
#

The only real counterpart to x86(-64) is ARM, but I haven't ever used one, so can't say anything about it