This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell 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
Author

complement

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.
 
GeeCON Prague 2014
 
subject: complement