This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes complement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "complement" Watch "complement" New topic


Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100

why does it give -5 as the output?
Sanyev Babu
Ranch Hand

Joined: Dec 18, 2003
Posts: 132
The binary of 4 "00000100"
If you do a complement if becomes "11111011" which is actually -5.
Just remember this when you do a complement on a positive number the resulting negative number will be one greater than the original positive number with a negative sign.

~4 = -(4+1)

When we are doing a complement we are actually doing a two's complement.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Negative Numbers in Java - Two's Complement

SCJP Tipline, etc.
Have you checked out Aspose?
subject: complement
It's not a secret anymore!