permaculture playing cards*
The moose likes Beginning Java and the fly likes Binary conversion help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Binary conversion help" Watch "Binary conversion help" New topic
Author

Binary conversion help

John Richardson
Greenhorn

Joined: Feb 20, 2004
Posts: 13
This should be an easy answer, but I surely can't get my head around it. To keep it simple I'll use a byte. How would I represent +127 (prob. 0111 1111) and -128. I'm mostly concerned with -128 FOR THIS REASON:
If the last bit is the sign bit, what does this mean (I know if it's a "1" then it's a negative number) and do you still count the bit in the calculation? Is 1000 0000 equal to -128, or is it 1111 1111?
Thanks for anyone that can clear this up!
John
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
1000 0000 is -128
1111 1111 is -1
John Richardson
Greenhorn

Joined: Feb 20, 2004
Posts: 13
Thanks for answering Billy Bob. Why is it that 1111 1111 is -1? I thought that to convert to decimal 1111 1111 would be like 255? Is it only when you have a negative integral type that you flip all the bits and add 1, meaning that all the highest-order bit "1" signifies is that you need to flip the bits and add 1 BEFORE you try to convert to decimal?
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
1111 1111 would be 255, if you were talking about an unsigned byte. But you were talking about signed bytes, right? So the first bit (1) is just the sign bit. It's called 2's complement. Look it up on google.
For example:
This is the first hit
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
think about the byte, if we have 1111 1111 then think about the bits or numbers the bits represent i hope this works;

think about it like, -128 + (64 + 32 + 16 + 8 + 4 + 2 + 1) brackets for tidying up reasons this is -128 + 127 = -1
But think about the bit at 1000 0000 -128, -128 is the sign bit, so you have a negative number, but it is the only bit that is a 1 so therefore it is -128.
so 0111 1111 is 127 because the sign bit is a 0.
Hope this helps
Davy


How simple does it have to be???
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
14TheLamb,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
John Richardson
Greenhorn

Joined: Feb 20, 2004
Posts: 13
Thanks for all the help. I've got it crystal clear now. Just one of the many things I don't use day in and day out that I needed to have solid before the exam!
John
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
good luck mate.
davy
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you are interested in some more details, you can google for "twos compliment". This is the name of the format used to store integers. There are plenty of places on the web that explain how to convert from a decimal number to a "twos compliment" binary number and vice versa.
Layne


Java API Documentation
The Java Tutorial
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Layne Lund:
If you are interested in some more details, you can google for "twos compliment".

I guess my earlier post on this thread is invisible.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Yup, I guess so :roll:
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Maybe it's only invisible to people named "Layne Lund".
 
GeeCON Prague 2014
 
subject: Binary conversion help