+ 11

# This is a challenge question in sololearn.

X=1-(-1==~0). I think the answer should be 1 But it is given 0 ? Help

26th Aug 2019, 5:53 PM
Kesh∆v
+ 30
By saying the answer is one, you are saying that the complement of zero is not equal to -1. This is not the case however, especially since we're dealing with signed integers. A) ~0 means invert all digits so, 0 becomes a 1 with a set signed bit B) -1 is also 1 with a set signed bit
26th Aug 2019, 6:04 PM
Ace
+ 16
~ is not operator. ~0 is -1 (signed arithmetic) So 1-(-1 == ~0) = 1-(-1 == -1) = 1-1 = 0
26th Aug 2019, 6:02 PM
Théophile
+ 4
You have to consider how signed binary numbers work. Let's use a byte sized number for this: 00000000b now, ~00000000 simply means that C treats the decimal number as a string of binary digits, and inverts binary digits of that number. Keep in mind the Most Significant Bit (MSB, the left-most one) is the sign bit: sign magnitude 1 1111111 This is then converted to decimal: 11111111 = -127 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1 Now plug this into the expression: x = 1 -(-1 == (-1)) x = 1 - (1) : since -1 does equal -1, the result is true. In a program, this is automatically typecasted to be a signed integer. x = 0 And there you have it.
27th Aug 2019, 3:35 PM
BootInk
+ 4
Esto tiene que ver con el complemento a 1 y complemento a 2. ¿Qué son estas cosas? Hubo un tiempo, en electrónica, que necesitaron representar números negativos en el mismo espacio que los positivos: esto se puede hacer invirtiendo los bits, miramos si el MSB es 1 y si lo es el número será negativo, lo único que los 0 serán 1 y los 1 0. Ejemplo: 1: 0001 -1: 1110 Pero tuvieron un problema muy molesto: existían dos 0, el positivo y el negativo. 0: 0000 -0: 1111 Veamos: -1 + 1 = -0 1110 + 0001 = 1111 ¿Y -0 + 1 = 0? 1111 + 0001 = 0000 Eso no era bueno (hay electrónica que aún funciona así, por ejemplo los testers electrónicos). Así que alguien pensó en sumar 1 al complemento. Así se pudo hacer que -1 + 1 = 0 -1 = ~(-1)+1 = 1110 + 0001 = 1111 Entonces -1 + 1 = 0: 1111 + 0001 = 0000 A la forma más antigua lo llamaron complemento a 1 y a la más moderna complemento a 2. Por eso ~0 es el complemento a 1 de 0, es decir 1111 Y -1 es el complemento a 2 de 1, eso es 1111, por eso son iguales.
27th Aug 2019, 7:24 PM
Miquel Andreu Fuster Sancho
+ 3
As in bracket first solving properly the answer will come out to be 1 then subtracting It from 1 we get 0,here the bitwise and normal subtraction is taking place in this question
28th Aug 2019, 1:27 AM
Yatin Verma