Negatives in Binary

I’m reading a book on Fortran 90/95 right now, and I was wondering, in general math, how would you go about making a binary number negative without a given bit length? For example, how would I know 1110111101 was -67 rather than 957? I remember something about the leftmost bit being 1, it’s negative, and if it’s 0, it’s positive. But how would I know if 1011 is 11 or -5?

Yes, when the “high order” bit is on it is a negative number, but it doesn’t have to be. It depends on your code and what you wish to do with the value or how you wish to treat it.

As for a given bit length it is always in terms of the field type. a byte = 8 bits with high bit as sign bit, a word = 16 bits again high bit is the sign bit, and so on.

I hope this helps.

So the biggest and smallest numbers allowed in a byte are 255 and -256? what would I do if I wanted to convert the number 3485 to binary?

A signed 4-bit value would only be able to represent values from 4 to -4. Check out two’s complement on wikipedia for more info on binary representations of negative numbers.

OOH! so a number 8 bits long will indicate if it is negative or positive by the left-most bit? so by default a number is 8 bits?

In essence yes.
For a one byte field the value can be 0-255 if unsigned, or between -127 to +127 if signed.

255 = 1111 1111 (unsigned)
127 = 0111 1111 (unsigned)

-127 = 1111 1111 (signed)
+127 = 0111 1111 (signed)

Again it is about how your code wishes to deal with the field as a signed or unsigned value.

An UNSIGNED byte can represent a value between 0 and 255.
A SIGNED byte can represent -128 to 127.

You must know what type of byte you are working with. This is the whole purpose of declaring variable types.

wow, things are becoming clear to me now in declaring variable types. before, I’d never really cared about it (mostly because I was doing stuff where declaring variable types weren’t required such as programming a calculator and MatLAB). Thanks

I think that in most cases (maybe it’s different in fortran?), the value -127 would be represented by “1000 0001”, using two’s complement, which is the most common way to represent negative numbers. It’s calculated by changing each 1 to a 0 and each 0 to a 1, and then adding 1 to the resulting number (read the wikipedia article linked above for more information…)

No, it doesn’t matter what the language is, the representation is identical.

Fortran, Cobol, C, PL/1, Pascal, APL, ect… it’s all the same internally.

The posts in this thread have so many errors that I am not
going to try and correct them all. I would suggest that you
download and read Chapter 6 of
http://srvhsrobotics.org/eugenebrooks/IntroCProg.pdf

There are 10 kinds of people in the world, those who
understand binary, and those who don’t…

Have fun,
Eugene

1111 = our team number = 15

100000000 = 256
10000000 = 128

Actually, 4 bits provides 16 possible combinations of 0 and 1 so it can represent 0 to 15 as an unsigned integer or -8 to +7 as a signed integer.
(unsigned integer in this case being a mathematical term rather than the C type).

Yes, typically computers represent signed integers as two’s complement so negative numbers have the high bit set.