438 views
3 votes
3 votes

OUTPUT OF THE FOLLOWING PROGRAMME

#include<stdio.h>
int main()
{
    int n1=10;
    int n2=15;
    int n3;
    n3=~n1;
    printf("%d",n3);
    return 0;
}

Basically here i want to understand how do we store negation value and what are the changes done,for a signed and unsigned integers here considering integer is of 4 bytes here. 

1 Answer

Best answer
6 votes
6 votes

The complementation operation being used here is like NOT gate in digital logic . So the functionality is same as that of NOT gate.

Hence when we do the complementation of a number , the bits of the original number are toggled . 

Hence binary representation of 10 in 32 bits : 28 0's followed by "1010"..

So by doing complementation we get  28 1's followed by "0101"..

Now we know :

Numbers are stored in 2's complement representation in computer for sake of simplicity of its usage in arithmetic circuits and unique representation of '0' as well..So 

a) If MSB is 0 , the number itself gives true magnitude.

b) If MSB is 1 , we need to take 2's complement of that number to get true magnitude and then add '-' sign to it.

So in this case after complementation we have 28 1's followed by "0101" . So we need to take 2's complement to get true magnitude of the number..

Taking 2's complement we have : 28 0's followed by "1011"  ==>  -11

Hence we obtain -11 as the output of the program..

P.S. : There is difference between '!' and '∼' operator . The first one is "logical not operator also known as negation" whereas second one is bitwise NOT known as complementation operator. 

selected by

Related questions

5 votes
5 votes
3 answers
1
suvasish pal asked Aug 26, 2017
620 views
Will it result in to an error if a header file is included twice?[A].Yes[B].No[C].It is compiler dependent
1 votes
1 votes
1 answer
2
Vasu Srivastava asked Aug 18, 2017
1,047 views
#include <stdio.h int main(void){ int i=511; char *p = (char *)&i; printf("%d", *p); }OK so why take 2's complement and not simple binary number? Means, why is C giving -...
2 votes
2 votes
6 answers
3
Nitesh Choudhary asked Jun 21, 2017
1,126 views
what is the o/p #include<stdio.h void main() { int a=5; printf("%d %d %d",a++,++a, a); }also suggest me notes so i can clear my concept about printf function (For Gate)
2 votes
2 votes
1 answer
4
Sugumaran asked Feb 7, 2018
490 views
#include<stdio.h int main() { int x=191; char *p; p=(char*)&x; printf("%d",*p); }explain it