John Richardson

Greenhorn

Posts: 13

posted 13 years ago

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

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

John Richardson

Greenhorn

Posts: 13

posted 13 years ago

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

Posts: 202

posted 13 years ago

1111 1111 would be 255, if you were talking about an

For example:

This is the first hit

**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

Posts: 384

posted 13 years ago

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

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

Posts: 7023

posted 13 years ago

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!

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!

John Richardson

Greenhorn

Posts: 13

Davy Kelly

Ranch Hand

Posts: 384

Layne Lund

Ranch Hand

Posts: 3061

posted 13 years ago

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

Layne

Billybob Marshall

Ranch Hand

Posts: 202

posted 13 years ago

I guess my earlier post on this thread is invisible.

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

Posts: 3061