Saturday 21 October 2017

2s complement for negative value.


void main(){
   int a=-12;
   a=a>>3;
   printf("%d",a); 
}

(a) -4
(b) -3
(c) -2
(d) -96
(e) Compiler error

Answer :( c)
Explanation:
Binary value of 12 is: 00000000 00001100
Binary value of -12 wills 2’s complement of 12 i.e.




So binary value of -12 is: 11111111 11110100




Right shifting rule:
Rule 1: If number is positive the fill vacant spaces in the left side by 0.
Rule 2: If number is negative the fill vacant spaces in the left side by 1.
In this case number is negative. So right shift all the binary digits by three space and fill vacant space by 1 as shown following figure:




Since it is negative number so output will also a negative number but its 2’s complement.




Hence final out put will be:




And its decimal value is: 2
Hence output will be:-2

No comments:

Post a Comment