+ 1
Why is the answer of the following line is 1?
int x = 1 - ( - 1 == ~0);
10 Respuestas
+ 11
Edited answer, Thanks to my friend CarrieForle for correcting me 😊
Manthan Gohel results will *not* be 1, it'll be 0.
~ complements bits of number. 0's become 1 and 1's become 0. well 0 and 1 represent bits(states) not decimal integer numbers.
in general a signed integer is represented as sequence of 32 bits (4bytes).
when you use bitwise operators you are actually working on its bits.
just assume that int is being represented with 8 bits then 0 will be represented as 00000000
in binary. on applying ~ bitwise not you get 11111111 that represents -1.
-1 == -1 is true when you typecast boolean `true` to integer it becomes 1.
(*Just for your info that `false` becomes 0.)
So expression becomes
int x=1-1;
So answer is 0.
●Try to search on net about :
*Bitwise operators
*Implicit and explicit typecasting/ type conversion
+ 4
CarrieForle 🤯
Thanks friend 😊🙏
I was wrong , smart of you 🤓.
Well integers are represented with multiple bits in memory.
If you represent int with suppose 8 bits(generally 32 bit but just assume ) 0 will be as 00000000 in memory and on complementing it'll be 11111111 representing -1.
and yes finally x will be 0.
Thanks a lot. 😁😁😁😁😁
+ 3
Take a look: https://www.sololearn.com/learn/4076/?ref=app
+ 3
🇮🇳Omkar🕉 //Busy , Exams. I did a test but it shows that the output is 0.
https://code.sololearn.com/c1EP34NidM0Y/?ref=app
+ 3
Manthan Gohel yeah correct friend. When MSB (most significant bit) is 1 it's a negative number.
Just to get a little clear idea assuming signed int is being represented with 4 bits only. 4 bits can represent upto 2^4 = 16 decimal numbers.
Then half (8) combinations of 4 bits will represent positive and half(8) will represent negative numbers.
Positive no : 0000, 0001, 0010, 0011, 0100 , 0101, 0110, 0111
Negative no :1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111
observe how numbers with msb 1 are negative.
+ 3
i think that code help for you:
here the difference between these operations (-, !, ~, !!, ~~) and the representation of signed numbers in the "int" type are presented.
https://code.sololearn.com/ceccnWD8vvLQ/?ref=app
+ 1
But,
How can 11111111 become 0?
Because it is signed integer?
I think first 1 represents - sign in binary signed digit, correct me if I am wrong please.
+ 1
🇮🇳Omkar🕉 //Busy , Exams. Yeah, I learned it in my diploma college 1month ago, i am just getting started, I am doing diploma in CE.
0
Thanks Bro, I was a bit confused
0
Michail Getmanskiy Thanks man, appreciate that😊