This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why 2 to the 7th -1? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why 2 to the 7th -1?" Watch "why 2 to the 7th -1?" New topic
Author

why 2 to the 7th -1?

Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
This is what I found in the campfire story
http://www.javaranch.com/campfire/StoryCups.jsp
All of these integer types are SIGNED. The leftmost bit
represents the sign (positive or negative) and is NOT part of the
value. So with a byte, for instance, you don't get the whole 8 bits to represent your value. You get 7. This gives you a range, for bytes, of -2 to the 7th) through (2 to the 7th) -1. Why that little -1 on the
end? Because zero is in there, and zero counts as negative.

I don't understand the last sentence. for the larges byte 01111110, why should we have zero as the last digit? Why does zero count as negative?


------------------------<BR>Sun Certified Java Programmer<BR>----------------------------------
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3201
    
    2
We do get all the bits. This is actually simple. For eg. byte is 8 bits so it can represent 2^8 unique integers. It basically boils down to "what" unique integers you want it to represent.
If you just want to represent +ive no. you can have 0 to 2^8-1 but if you also want to store -ive integers you can have -2^7 to 2^7-1. Note that, in both the cases you are representing 2^8 unique integers.
Now, as byte is "defined" to be signed, it represents -2^7 to 2^7-1.
If you consider char and short, both are of 16 bits, but char can represent 0 to 2^16-1 (Because it is "defined" by the language to be unsigned) and short can represent -2^15 to 2^15-1. Again, both do store 2^16 unique integers.
To understand how the sign bit (left most bit) is used to represent -ive no. you may want to read about 2's complement method. Search on the web, you'll find a lot of material on this.

HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3201
    
    2
For that little -1:
how many numbers can you represnt using 1 bit? 2^1 = 2. Right?
What are those 2 numbers? 0 and 1
ie. 0 - (2^1 - 1 )
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why 2 to the 7th -1?
 
Similar Threads
Possible Correction in Cup Size?
Re: Change String value in method call
Hello all...new here and a question...
passing by refernce
Signed and unsigned confusion