This week's book giveaway is in the Servlets forum. We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line! See this thread for details.

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

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

posted

0

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

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

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!

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

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