Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Math and Science (http://www.chiefdelphi.com/forums/forumdisplay.php?f=70)
-   -   Negatives in Binary (http://www.chiefdelphi.com/forums/showthread.php?t=51888)

Michael Hill 14-01-2007 22:15

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?

Phalanx 14-01-2007 22:25

Re: Negatives in Binary
 
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.

Michael Hill 14-01-2007 22:29

Re: Negatives in Binary
 
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?

something 14-01-2007 22:30

Re: Negatives in Binary
 
Quote:

Originally Posted by Michael Hill (Post 556919)
But how would I know if 1011 is 11 or -5?

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.

Michael Hill 14-01-2007 22:33

Re: Negatives in Binary
 
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?

Phalanx 14-01-2007 22:48

Re: Negatives in Binary
 
Quote:

Originally Posted by Michael Hill (Post 556938)
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.

ChuckDickerson 14-01-2007 22:59

Re: Negatives in Binary
 
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.

Michael Hill 14-01-2007 23:03

Re: Negatives in Binary
 
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

Noah Kleinberg 14-01-2007 23:06

Re: Negatives in Binary
 
Quote:

Originally Posted by Phalanx (Post 556953)
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.

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...)

Phalanx 14-01-2007 23:12

Re: Negatives in Binary
 
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.

eugenebrooks 15-01-2007 00:24

Re: Negatives in Binary
 
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

sdexp 10-02-2007 15:18

Re: Negatives in Binary
 
1111 = our team number = 15

100000000 = 256
10000000 = 128

TimCraig 10-02-2007 21:28

Re: Negatives in Binary
 
Quote:

Originally Posted by something (Post 556932)
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.

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.


All times are GMT -5. The time now is 12:11.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi