Why is the answer of the following line is 1? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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