File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please Help with 1's / 2's complements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please Help with 1 Watch "Please Help with 1 New topic
Author

Please Help with 1's / 2's complements

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Savithri Devaraj
Ranch Hand

Joined: Jun 26, 2000
Posts: 103
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
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.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

JM/Savithri:
FYI.....another related discussion
Regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Aparanji Raju
Ranch Hand

Joined: Apr 02, 2000
Posts: 65

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
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help with 1's / 2's complements
 
Similar Threads
Using ~ (Unary bitwise complement) for Zero
Why negative integers are so nasty in binary?
Binary numbers
shift operators
Doubt in negative numbers in binary format