aspose file tools*
The moose likes Beginning Java and the fly likes Signed and unsigned confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Signed and unsigned confusion" Watch "Signed and unsigned confusion" New topic
Author

Signed and unsigned confusion

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
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."
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

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.


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

Joined: Mar 24, 2008
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

Joined: Mar 28, 2003
Posts: 11509
    
  95

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
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Signed and unsigned confusion