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?

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.

Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3556

9

posted

0

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