"not" operator on an int

Hello,

I have a simple question but can’t find the answer to in the documentation.

Let’s say we receive an int with socket_read_binary_integer() and that int represents a “keep alive” signal.

So value would only be 0 or 1.

Can I use the “not” operator to set a variable to the opposite value of “keepalive” ?

Thank you !

Usually a “NOT” operation would not be allowed for Integers, especially since the behaviour that you are describing is the behaviour of a “NOT” operation only fo booleans.

Here is a specific example:
Considering a boolean that is represented by 1 bit: using a NOT operation when 0 would return 1 and viceversa
However, an integer has 32 bits: using a NOT operation when 0 would return 2,147,483,647 and viceversa

Now about possible solutions:

  • Either use a cast, to reduce the datatype of the int to a bool (which i don’t even know if it is supported honestly)

  • Or, only if you are sure that the value of that variable is always either 0 or 1, you can use some very simple conversion function.

For example:
var = -var + 1

which converts 0 to 1 (-0 + 1) and 1 to 0 (-1 + 1).

Hope it helps

Thanks, I went with a manual conversion

What about a if statement?

if (var == 0){
var = 1
}else if (var > 0){
var = 0
}

Yes, that’s pretty much what I’ve done