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.

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).]

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

posted

0

Thanks a lot . It was getting on my nerves. I was not able to get the questions on shift operators right. Hope this will help. Once again thanks.

Hi all, I will be giving the exam this week.The >> and >>> operations are still confusing me particularly with respect to the sign.Could somebody give me an explaination or suggest a site i could refer to? Thanks, Regards Aparanji

The operator >> means "shift right with sign extension". The operator >>> means "shift right with zero fill." For example, public static void main(String args[]) { int number = -128; int anotherNumber = -128; System.out.println(number >> 1); // prints -64 System.out.println(anotherNumber >>> 1); // prints 2147483584 } As you can see, using >> fills the leftmost bits with the same sign as the original number, whereas using >>> fills the leftmost bits with 0's. Hope this helps.