McAfee Interview Question
SDE1sCountry: India
Interview Type: In-Person
the operator is nothing but eg:-) if input is 8 then (8+1)*(-1) would get the ~ result.. correct me if its wrong:)
It is not that way, actually (~) means compimenting or inverting the bits
eg:
6 ---> 0110
~6 --->1001
but system while accessing this from memory looks at the MSB('1'001) and thinks this as negative number, since system stores the negative number in 2's compliment it decodes it to get the number as (-7)
since -7 will be stored as 1001 in 2's compliment form
7 ---> 0111
-7 :
compliment of 7+1==> 1000+1==> 1001(ACTUAL BINARY STORAGE OF -7)
this is true concept :)
first of all, its output will be -9. (not 9)
if we take int is of 1 byte, then 8= 0000 1000
its ~, ~8=1111 0111
now compiler sees, oh, its MSB is 1, so it must be negative number. So in order to print it, I will take 2's compliment of this binary combination, find its decimal equivalent, put negative sign before it, and print it, That's it !
decimal equivalent of 1111 0111= -(2's comp of 1111 0111)
= -(0000 1001)
= -(9)
8= 00000000 00000000 00000000 00001000
- Bajaj July 30, 2013~8 = 11111111 11111111 11111111 11110111
we are assigning it in integer hence most significant bit (MSB)is the sign bit
bcz MSB is 1 hence it is treated as -ve no.
when u try to print it then before printing compiler will take 2's complement hence it becomes :
2's complement of (~8)=9
2's complement of 11111111 11111111 11111111 11110111 is 00000000 00000000 00000000 00001000 +1 = 1001 = 9