Jan-19-2017, 01:42 PM
Hi there,
it is explained very well here:
http://superuser.com/questions/975684/co...-to-binary
There is a small difference between the 1's and 2's complement.
The 1's complement simply switches 0->1 and 1->0, like 1010 becomes 0101.
With the 2's complement you proceed the same way, but add 1 in the end: 1010->0101 (1's complement) +0001 -> 0110 (2's complement).
Now you can represent positive numbers. What about negatives? We can assign the first bit to the sign. That means 0 for + and 1 for -:
1111 1111 to 0111 1111 (8 bit := 1 Byte); that is what you can read detailed above.
To answer your question:
60 (dec) -> 0011 1100 (bin)
1's complement: 1100 0011, that means the first 1 -> - and 100 0011 is 67. You work with one Byte (=8bit), 1 is already assigned, so 7 left. 2**7 = 128. You now substract (because this is a negative value and this is how negative values are represented in binary) this value of 67; 67-128 = -61.
With Python 3.5:
I have no detailed knowledge how python stores bits, but from its behavior I suggest this result.
Hope my explanation was understandable.
it is explained very well here:
http://superuser.com/questions/975684/co...-to-binary
There is a small difference between the 1's and 2's complement.
The 1's complement simply switches 0->1 and 1->0, like 1010 becomes 0101.
With the 2's complement you proceed the same way, but add 1 in the end: 1010->0101 (1's complement) +0001 -> 0110 (2's complement).
Now you can represent positive numbers. What about negatives? We can assign the first bit to the sign. That means 0 for + and 1 for -:
1111 1111 to 0111 1111 (8 bit := 1 Byte); that is what you can read detailed above.
To answer your question:
60 (dec) -> 0011 1100 (bin)
1's complement: 1100 0011, that means the first 1 -> - and 100 0011 is 67. You work with one Byte (=8bit), 1 is already assigned, so 7 left. 2**7 = 128. You now substract (because this is a negative value and this is how negative values are represented in binary) this value of 67; 67-128 = -61.
With Python 3.5:
>>> bin(~60) '-0b111101' >>> format(~60, 'b') '-111101'The sign is already stored separately, so you just get the binary form of 60 and the "2's complement" by adding 1: 61.
I have no detailed knowledge how python stores bits, but from its behavior I suggest this result.
Hope my explanation was understandable.