日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

补码学习

發(fā)布時間:2025/7/14 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 补码学习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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就表示為28次方即為-128,故而8為補碼所表示的范圍為-128----127(1111 1111)

故而two complement表示的范圍是:

sign bit
01111111=127
00000010=2
00000001=1
00000000=0
11111111=?1
11111110=?2
10000001=?127
10000000=?128
8-bit two's-complement integers

?而源碼,反碼的范圍都是-127-127.

Table G.3:?Three-bit two's-complement binaryfixed-pointnumbers.
BinaryDecimal
0000
0011
0102
0113
100-4
101-3
110-2
111-1


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(&amp;), 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é)

以上是生活随笔為你收集整理的补码学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。