|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
explanation of negative numbers in binary
I've noticed that when a negative number is represented in binary, all bits to the left that would have been zero should be one. I see that... But what's the difference between
how -1 and 255 are represented? "1111 1111" The windows calculator will covert -1 to that, but when I go back to decimal, it makes it 255.. |
|
#2
|
||||
|
||||
|
It's because the -ve number (-1) is being represented in binary using two's complement. It's the most common way that computers use to represent -ve numbers these days. See http://www.duke.edu/~twf/cps104/twoscomp.html for more information as to the advantages of using this technique.
You can elect to treat a number as signed or unsigned in some languages (such as C, C++, Pascal, Delphi etc.). Basically what this means is that you tell the compiler to either assume that all 8 bits are being used for a positive number (i.e. unsigned), or 7 bits are used for the number and 1 bit for the sign (i.e. signed). If the number is treated as unsigned, then the range is 0 to 255 (for one byte). If you treat it as signed, then the range is -128 to 127. Either way, you have a range of 256 distinct values. It's just a matter of which range of values you choose to use. |
|
#3
|
||||
|
||||
|
I think this is a very common question because a lot of different books and teachers don't explain the difference between unsigned and signed integers in a program. Instead, all they do is say "OK, flip everything to make it two's complement". This is true, but what they aren't telling you is that the one bit your flipping doesn't get counted in the value as anything more than (+|-). I posed such a question to a goofball C (actually, Hypecard) programming teacher in High School and he couldn't tell me the difference
. I also have a Sams C in 24 hours book that doesn't explain that one bit gets reserved for unsigned / signed integers. What a pain. |
|
#4
|
||||
|
||||
|
Actually, if you only flip the bits of a binary number in two's complement ns you will not get the correct negative or positive number back. You must flip the bits then add 1 to the number applying the carry rule through the most sig bit, or wherever the carry stops. Just flipping the bits changes ones complement sign value. Of course this is all really easy to figure out once you know whats going on. Floating point numbers however are a different story. Alot of folks with degrees in comp-sci can't correctly explain how floating point numbers work in today's popular hardware. It's freakin magic
![]() |
![]() |
| Viewing: Dev Shed Forums > Other > Dev Shed Lounge > explanation of negative numbers in binary |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|