# Signed and unsigned confusion

Vonique Leary

Ranch Hand

Posts: 107

posted 7 years ago

This is from Java Rance pass by value page, and I'm not sure what "Why that little -1 on the end? Because zero is in there, and zero counts as negative." means.

Why does there need to be a -1 that counts as zero?

"byte - 8 bits

short - 16 bits

int - 32 bits

long - 64 bits

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

Why does there need to be a -1 that counts as zero?

"byte - 8 bits

short - 16 bits

int - 32 bits

long - 64 bits

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

posted 7 years ago

I think the wording in the cup size campfire story is a little confusing, but it is basically stating that as far as Java is concerned, it stores zero as a positive number, therefore when counting the possible positive numbers, you have to subtract one for the place that zero is held in.

Consider a storage unit where I am storing my numbers in only 4 bits (a nibble - half an 8 bit byte), and using the uppermost bit for indicating whether the number is positive or negative:

Note: You do not need to know about nibbles for Java - it just helps to have a nice small unit that I can use to demonstrate bit storage.

So our nibble can potentially store the negative numbers from -1 through to -8. It can also store the positive numbers 1 through to (8 - 1) = 7. The - 1 is in there because zero is a positive number, which means that we have to reduce our potential positive numbers by one (or count the zero as -1 from the range of potential positives (I think that is what the campfire story is trying to say)).

Alternatively, our range is from (-2³) to (2³) -1. Again the -1 is in there because we need to account for zero.

Consider a storage unit where I am storing my numbers in only 4 bits (a nibble - half an 8 bit byte), and using the uppermost bit for indicating whether the number is positive or negative:

Note: You do not need to know about nibbles for Java - it just helps to have a nice small unit that I can use to demonstrate bit storage.

So our nibble can potentially store the negative numbers from -1 through to -8. It can also store the positive numbers 1 through to (8 - 1) = 7. The - 1 is in there because zero is a positive number, which means that we have to reduce our potential positive numbers by one (or count the zero as -1 from the range of potential positives (I think that is what the campfire story is trying to say)).

Alternatively, our range is from (-2³) to (2³) -1. Again the -1 is in there because we need to account for zero.

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog

Vonique Leary

Ranch Hand

Posts: 107

posted 7 years ago
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog

My personal opinion is that binary knowledge, like big-O notation, is something that you can get away with not knowing 99% of the time. Even for that remaining 1%, you can almost certainly get your code working, but it may not be as good as if you had the background knowledge.

Campbell Ritchie

Marshal

Posts: 52590

119

posted 7 years ago

One would expect a computer scientist to know binary arithmetic, big-O notation, and how to write a linked list, even though you don't use them directly in your programming. Something which sits in the back of your head like relative addressing for when you need to know about it.