programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Knute Snortum
• Junilu Lacar
• Devaka Cooray
Saloon Keepers:
• Ganesh Patekar
• Tim Moores
• Carey Brown
• Stephan van Hulst
• salvin francis
Bartenders:
• Ron McLeod
• Frits Walraven
• Pete Letkeman

# Signed and unsigned confusion

Ranch Hand
Posts: 107
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."

author and jackaroo
Marshal Commander
Posts: 12218
279
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.

Vonique Leary
Ranch Hand
Posts: 107
Okay, I think I'm starting to get it. I wonder, how important is it to be a Java programmer and know all about these little facts? Can I get by and not get too in-depth with binary knowledge?

Thanks so much for the help.
Von

Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12218
279
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.

Marshal
Posts: 60795
190
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.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?