+ 1

Why is the answer of the following line is 1?

int x = 1 - ( - 1 == ~0);

10th Nov 2019, 2:20 PM
Manthan Gohel
Manthan Gohel - avatar
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
10th Nov 2019, 2:42 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 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. 😁😁😁😁😁
10th Nov 2019, 3:12 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
10th Nov 2019, 2:39 PM
你知道規則,我也是
你知道規則,我也是 - avatar
10th Nov 2019, 3:01 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 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.
10th Nov 2019, 3:58 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 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
11th Nov 2019, 11:48 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 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.
10th Nov 2019, 3:32 PM
Manthan Gohel
Manthan Gohel - avatar
+ 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.
10th Nov 2019, 4:09 PM
Manthan Gohel
Manthan Gohel - avatar
0
Thanks Bro, I was a bit confused
10th Nov 2019, 2:58 PM
Manthan Gohel
Manthan Gohel - avatar
0
Michail Getmanskiy Thanks man, appreciate that😊
12th Nov 2019, 3:34 PM
Manthan Gohel
Manthan Gohel - avatar