#include <iostream> using namespace std; int main() { int x=~1; cout<<x; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

#include <iostream> using namespace std; int main() { int x=~1; cout<<x; }

the answer is -2 ,someone who understand why -2 help me to understand

13th Jan 2017, 9:17 AM
Hisani msigwa
Hisani msigwa - avatar
7 Answers
+ 8
I'm not good at signed conversion of decimal value to binary value, but I can tell that the inversion of bits of binary value for 1 will result in -2.
13th Jan 2017, 9:55 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
The ~ operator is bitwise NOT, it inverts the bits in a binary number: NOT 011100 = 100011
13th Jan 2017, 9:46 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
That is not a destructor sign. ~ is the bitwise operator for NOT. 1 in binary is 00000000000000000000000000000001, ~1 will be 11111111111111111111111111111110 which is -2
13th Jan 2017, 11:10 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
7 in binary will be 00111. When NOT 7 it becomes 11000. If u know about the twos complement, 11000 is the negative of 01000 which is 8. Thus, ~7 will results a -8.
13th Jan 2017, 4:22 PM
Yi Jing Chai
Yi Jing Chai - avatar
+ 1
so why -2
13th Jan 2017, 9:52 AM
Hisani msigwa
Hisani msigwa - avatar
+ 1
I tested in a compiler x=~7 it give me -8 so increment 1 and assign -
13th Jan 2017, 10:05 AM
Hisani msigwa
Hisani msigwa - avatar
0
~(destructor ) what I know, so why increment 1 and assign -
13th Jan 2017, 10:10 AM
Hisani msigwa
Hisani msigwa - avatar