Originally posted by JM:
I have read many places like
i = 7;
Print ~i .
I don't know how to find out this. On this forum , I have read few answers that explain with " ...... .. first convert a bit pattern of like 0000 1101 to 1's complement or to 2's " complement .
I don't know how do we get 1's or 2's complements .
Can somebody help me out how do we get complements.
In the discussion of >>> / << / >> operators for negative numbers also I find these technical words .
Please explain.
Thanks in advance.
Since nobody took a shot at this, I will try
Negative numbers are represented in computers using the 2's complement format. To get the 2's complement of a given number
1. Write the number in binary.
2. Take the 1's complement of it. 1's complement is nothing but flipping all 0s to 1s and all 1s to 0s.
3. Then add 1 to that. Ignore any overflow
For example: To write the number 11 in 2's complement, first write the number in binary
1. To convert to binary, continue to divide by 2, until you get a final remainder of 0 or 1. Take the remainders in the reverse order. 11 is written as 0000 1011.
2. Taking 1's complement would give 1111 0100.
3. Adding 1 would give 1111 0101
So, the 2's complement of 11 is 11110101.
Hope this helps.
Savithri
PS: Use all 32 bits if you are operating on an integer.
[This message has been edited by Savithri Devaraj (edited July 10, 2000).]