补码学习
one's complemen:即將每位二進制求反即反碼。
000...00001?=?+1? 000...00000?=?+0? 111...11111?=?-0?
問題出現(xiàn)在(+0)和(-0)上,在人們的計算概念中零是沒有正負之分的.這種簡單的機制會產(chǎn)生-1 + 2 =?+0,為避免這種情況,引入twos-complement.?
binary decimal 11111110 -1 + 00000010 +2 ............ ... 1 00000000 0 <-- not the correct answer 1 +1 <-- add carry ............ ... 00000001 1 <-- correct answer
two complement:?是將二進制求反加一,忽略溢出。
這就避免了對0的兩種表示。
000...00001?=?+1?? ?000...00000?=?0??? 111...11111?=?-1?
注意:111...11111,111...11111在不同規(guī)則下表示的是不同的值。Sign-and-magnitude表示原碼。
在two complement 下1000 0000b表示是多少?
分析:首先它是負數(shù),所以取反加一得到他相對應的正數(shù)的補碼,而正數(shù)的補碼為其本身,所以1000 0000b就表示為2的8次方即為-128,故而8為補碼所表示的范圍為-128----127(1111 1111)。
故而two complement表示的范圍是:
| sign bit | |||||||||
| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | = | 127 |
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | = | 2 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | = | 1 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | = | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | = | ?1 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | = | ?2 |
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | = | ?127 |
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | = | ?128 |
?而源碼,反碼的范圍都是-127-127.
|
Note that according to our negation rule,?. Logically, what has happened is that the result has ``overflowed'' and ``wrapped around'' back to itself. Note that??also. In other words, if you compute 4 somehow, since there is no bit-pattern assigned to 4, you get -4, because -4 is assigned the bit pattern that would be assigned to 4 if??were larger. Note that numerical overflows naturally result in ``wrap around'' from positive to negative numbers (or from negative numbers to positive numbers). Computers normally ``trap'' overflows as an ``exception.'' The exceptions are usually handled by a software ``interrupt handler,'' and this can greatly slow down the processing by the computer (one numerical calculation is being replaced by a rather sizable program).
Note that?temporary overflows?are ok in two's complement; that is, if you add??to??to get?, adding??to??will give??again. This is why two's complement is a nice choice: it can be thought of as placing all the numbers on a ``ring,'' allowing temporary overflows of intermediate results in a long string of additions and/or subtractions. All that matters is that the final sum lie within the supported?dynamic range。
?mov ? al,27h ??
mov ? bl,73h ??
add ? al,bl ??
?? ?
?
27h ? ? ? 00100111 ??
73h ? ? ? 01110011 ??
?? ?
如果是無符號數(shù)al的結(jié)果為10011010(9A),如果是有符號數(shù)結(jié)果是什么? ??
?? ?
?? ?如果是有符號數(shù),結(jié)果依然是10011010(9A)。注意,此時溢出標志置位。表明這個加法如果是作為有符號數(shù)的加法時,結(jié)果是不能用的。當然,如果你把這個加法作為無符號數(shù)的加法來看,結(jié)果則是可用的(雖然此時仍有溢出,但它是無意義的。溢出只針對有符號數(shù)而言的)。 ??
?? ?這是個特例。因為無符號數(shù)的加減法與有符號數(shù)的補碼的加減法是恰巧是一致的。因此有符號數(shù)與無符號數(shù)可以使用同一指令,得到同一結(jié)果。而這一結(jié)果是作為有符號數(shù)還是無符號數(shù),是由程序員來定義的:如果程序員認為是有符號數(shù)的加減法,則他將關心運算結(jié)果的溢出標志,以判別結(jié)果是否可用;如果程序員認為是無符號數(shù)的加減法,則他將關心運算結(jié)果的進位標志,看運算結(jié)果是否有進(借)位。 ??
?? ?正是基于這樣的原因,計算機的有符號數(shù)才采用補碼表示。 ?
?例如: ??
? al=1001 ? 1100(9C),bl=1110 ? 0111(E7),則 ??
? add ? al,bl ??
? 運算結(jié)果是: ??
? al=1000 ? 0011(83) ? 且進位標志CY=1,溢出標志OV=0. ??
? ??
? 對于這個結(jié)果,你可以把它作為無符號數(shù)運算,即有: ??
? al=9Ch=156d,bl=E7h=231d,運算結(jié)果al=83h=131d,考慮到進位,運算結(jié)果應為131+256=387,而al+bl=156+231=387.正確。 ??
? ??
? 同樣對于這個結(jié)果,你也可以把它作為有符號數(shù)的運算,即有: ??
? al=9Ch=-100d,bl=E7h=-25d,(注意有符號數(shù)均看成補碼,9Ch是-100的補碼,下同)。 ??
? 而運算結(jié)果al=83h= ? -125d,因為沒有溢出(有符號數(shù)是不考慮進位的),運算結(jié)果可用。 ??
? 而al+bl=9Ch+E7h=(-100)+(-25)=-125.可見結(jié)果亦完全正確。 ??
? ??
? 由上例可見,無符號數(shù)的加法與有符號數(shù)的補碼加法是完全一致的。CPU沒有必要去區(qū)分。關鍵在于程序員如何去看待。CPU的運算結(jié)果將同時影響進位標志與溢出標志。如果程序員認為這是無符號數(shù)運算,則只考慮進位標志而忽略溢出標志。反之,如果程序員認為這是有符號數(shù)運算,則只考慮溢出標志而忽略進位標志。 ??
?
Both Csharp and Visual Basic uses twos complement representations of numbers. Understanding twos complement isn't strictly necessary for most applications but it sometimes comes in handy. With a little thought, you can discover the value of every bit in a number whether it is positive or negative.
Bitwise operators in c# OR(|), XOR(^), AND(&), NOT(~)
http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx?
參考:http://en.wikipedia.org/wiki/Two's_complement?
?? ?? ??http://ccrma.stanford.edu/~jos/mdft/Two_s_Complement_Fixed_Point_Format.html
?? ?? ?http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
http://topic.csdn.net/t/20030913/20/2256856.html#
?
轉(zhuǎn)載于:https://www.cnblogs.com/luyong/archive/2008/09/05/1284905.html
總結(jié)
- 上一篇: RMQ(Range Minimum/Ma
- 下一篇: VS2008 Tips #004 – 您