Two Laptop Bag
The moose likes Beginning Java and the fly likes Hello here and a question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hello here and a question..." Watch "Hello here and a question..." New topic

Hello here and a question...

Scott Batchelor

Joined: Jul 07, 2003
Posts: 11
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. Works the same way with the others.
float - 32 bits
double - 64 bits

Can someone explain this to me in a bit<---Get it!? more detail? I am having a little trouble grasping this.
Matt Fyffe

Joined: Jun 19, 2003
Posts: 25
Ack, binary... I hate that stuff. Well assuming you know the way binary works with powers of 2, let me see if I can word it better. When you have a signed int, it includes both positive and negative values. When written in binary if you have 8 bits (0000 0000) the bit on the far left would be the one that shows whether or not this number is positive or negative. If the number is positive, then the leftmost bit would be 0. If negative, 1.
Because this bit is used to show whether or not the value is positive, you are left with only 7 bits to show the actual value.
I hope that sums everything up, and if not, I'm sure someone else can reprimand me :-D.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Scott,
Welcome to JavaRanch.
Because zero is in there, and zero counts as negative. Works the same way with the others.
Nope 0 is 0 that's why you have one fewer positive numbers for integeral types, the minimum byte is -2^7 or 128 which is represented in binary by 10000000. It's due to the fact that negatives are stored in 2's complement form. In 2's complement you flip all 0s to 1s and all 1s to 0s and add 1. To get the absolute value you reverse the process by subtracting 1 and flipping the bits:

which is -128.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Eric Hoskland

Joined: Oct 12, 2000
Posts: 29
I think there are a few ideas getting confused in your quote. So I'll take each at a time.
"All of these integer types are SIGNED. The leftmost bit represents the sign (positive or negative) and is NOT part of the value"
This is a format called SIGN MAGNITUDE which I don't think JAVA uses. This, as you said, means that the value is represented by the the lower 7 bits and the 8th bit is purely a sign bit. So
00000011 = 3
10000011 = -3
This format creates a symmetry between negative and positive respresentations giving you a range of
-((2^7)-1) to 2^7 - 1
-127 to 127
(same number of positive and negative values are represented)
"(-2 to the 7th) through (2 to the 7th) -1"
This range occurs when the values are being represented by two's complement, which is what I'm pretty sure JAVA uses. The equation for calculating a two's complement number is as follows (for a byte)
msb = most significant bit.
(2^7)*(-1)*(msb) + (the value represented by the remaining bits).
00000011 = 3
(2^7)*(-1)*0 + 3 = 3
11111101 = -3
(2^7)*(-1)*1 + 125 = -3
This format results in the range you said:
"(-2 to the 7th) through (2 to the 7th) -1"
Two's complement (nothings wasted):
10000000 = -128
00000000 = 0
Sign magnitude (two things represent 0):
10000000 = -0
00000000 = +0

These representation issues come from the fact that in binary you have an even set of values to pick from to represent an odd set.
from -10->10 there are 11 values (odd)
BASE2 will always result in an even number of different combinations.
0 = 2
00 = 4
000 = 8
So somewhere there is going to be a comprimise. In SIGN magnitude, because there are two zeros, you will lose one value of representation but then you have the same number of positive and negative numbers. With 2's complement you have one more value represented on the negative side.

Hope this answers your question
I agree. Here's the link:
subject: Hello here and a question...
It's not a secret anymore!