|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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?
|
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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?
|
|
#4
|
|||
|
|||
|
Re: Negatives in 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.
|
|
#5
|
||||
|
||||
|
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?
|
|
#6
|
||||
|
||||
|
Re: Negatives in Binary
Quote:
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. |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
||||
|
||||
|
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
|
|
#9
|
||||
|
||||
|
Re: Negatives in Binary
Quote:
|
|
#10
|
||||
|
||||
|
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. |
|
#11
|
|||
|
|||
|
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 |
|
#12
|
||||
|
||||
|
Re: Negatives in Binary
1111 = our team number = 15
100000000 = 256 10000000 = 128 Last edited by sdexp : 10-02-2007 at 15:20. |
|
#13
|
|||
|
|||
|
Re: Negatives in Binary
Quote:
(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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Refrain from the negatives | Arefin Bari | General Forum | 19 | 10-05-2006 22:43 |
| Reading binary data from a file and writing it in VB | complete | Programming | 1 | 02-01-2006 21:21 |
| binary in C | sciguy125 | Programming | 9 | 13-11-2005 22:12 |
| Binary in Rose is Rose | MisterX | Chit-Chat | 8 | 11-02-2004 18:26 |
| delphi: binary sort | archiver | 2000 | 0 | 24-06-2002 00:30 |