Scott Batchelor

Greenhorn

Posts: 11

posted 14 years ago

Can someone explain this to me in a bit

Thanks

SB

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.

Thanks

SB

Matt Fyffe

Greenhorn

Posts: 25

posted 14 years ago

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.

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

Posts: 3451

posted 14 years ago

Hi Scott,

Welcome to JavaRanch.

Nope 0 is 0

which is -128.

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

Greenhorn

Posts: 29

posted 14 years ago

I think there are a few ideas getting confused in your quote. So I'll take each at a time.

FIRST CONCEPT:

"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)

SECOND CONCEPT:

"(-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).

examples:

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"

CONCLUSION:

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.

BASE10

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

etc..

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

FIRST CONCEPT:

"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)

SECOND CONCEPT:

"(-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).

examples:

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"

CONCLUSION:

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.

BASE10

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

etc..

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