int x = ~10;
What will be the value of x?
how to solve this question
Can u pl explain me
Hi @spagire4 , let me explain you how a machine generates value for this expression , i expect you to be well versed with the concept of 2’s compliment.
let’s begin:-
- machine recieves the value ~10 (~ will invert all the bits). so the binary form of 10 is 00001010 (considering in as a 8-bit number) so we will get 11110101
- now here the machine will check the MSB (most significant bit) ,which is 1,so it will percieve it as a negative number.
- machine will convert the number in decimal form which is by taking its 2’s compliment and adding a - sign to it
- so 2’s compliment is done by ~(11110101) +1 which is (00001011) which is 11 and adding a -ve sign makes it -11, which is our answer
In case of any doubt feel free to ask
mark your doubt as resolved if you got the answer