• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            好吧,我被征服了—— 關(guān)于 c++ unsigned char

            1 unsigned char i;
            2 i=-20;
            3 
            上面一段很簡單的代碼,如果輸出會是什么 呢?
            今天群里的新學(xué)c++同學(xué)問我,unsighed char 和char 有啥區(qū)別,上面的輸出會有什么 不同
            在這里,我先鄙視一下自己,我直觀的理解為通常的 首位符號位,然后丟下的就是輸入無符號的。好吧,估計各位看官說我太菜,但確實是輸出的結(jié)果和我想的不大一樣,如果各位沒明白我說的是什么問題,可以試一下。然后回來看下面的內(nèi)容。













            In an unsigned type, all the bits represent the value. If a type is defined for a particular machine to use 8 bits, then the unsigned version of this type could hold the values 0 through 255.

            無符號型中,所有的位都表示數(shù)值。如果在某種機器中,定義一種類型使用 8 位表示,那么這種類型的 unsigned 型可以取值 0 到 255。

            The C++ standard does not define how signed types are represented at the bit level. Instead, each compiler is free to decide how it will represent signed types. These representations can affect the range of values that a signed type can hold. We are guaranteed that an 8-bit signed type will hold at least the values from 127 through 127; many implementations allow values from 128 through 127.

            C++ 標準并未定義 signed 類型如何用位來表示,而是由每個編譯器自由決定如何表示 signed 類型。這些表示方式會影響 signed 類型的取值范圍。8 位 signed 類型的取值肯定至少是從 -127 到 127,但也有許多實現(xiàn)允許取值從 -128 到 127。

            Under the most common strategy for representing signed integral types, we can view one of the bits as a sign bit. Whenever the sign bit is 1, the value is negative; when it is 0, the value is either 0 or a positive number. An 8-bit integral signed type represented using a sign-bit can hold values from 128 through 127.

            表示 signed 整型類型最常見的策略是用其中一個位作為符號位。符號位為 1,值就為負數(shù);符號位為 0,值就為 0 或正數(shù)。一個 signed 整型取值是從 -128 到 127。

            Assignment to Integral Types
            整型的賦值

            The type of an object determines the values that the object can hold. This fact raises the question of what happens when one tries to assign a value outside the allowable range to an object of a given type. The answer depends on whether the type is signed or unsigned.

            對象的類型決定對象的取值。這會引起一個疑問:當我們試著把一個超出其取值范圍的值賦給一個指定類型的對象時,結(jié)果會怎樣呢?答案取決于這種類型是 signed 還是 unsigned 的。

            For unsigned types, the compiler must adjust the out-of-range value so that it will fit. The compiler does so by taking the remainder of the value modulo the number of distinct values the unsigned target type can hold. An object that is an 8-bit unsigned char, for example, can hold values from 0 through 255 inclusive. If we assign a value outside this range, the compiler actually assigns the remainder of the value modulo 256. For example, we might attempt to assign the value 336 to an 8-bit signed char. If we try to store 336 in our 8-bit unsigned char, the actual value assigned will be 80, because 80 is equal to 336 modulo 256.

            對于 unsigned 類型來說,編譯器必須調(diào)整越界值使其滿足要求。編譯器會將該值對 unsigned 類型的可能取值數(shù)目求模,然后取所得值。比如 8 位的 unsigned char,其取值范圍從 0 到 255(包括 255)。如果賦給超出這個范圍的值,那么編譯器將會取該值對 256 求模后的值。例如,如果試圖將 336 存儲到 8 位的 unsigned char 中,則實際賦值為 80,因為 80 是 336 對 256 求模后的值。

            For the unsigned types, a negative value is always out of range. An object of unsigned type may never hold a negative value. Some languages make it illegal to assign a negative value to an unsigned type, but C++ does not.

            對于 unsigned 類型來說,負數(shù)總是超出其取值范圍。unsigned 類型的對象可能永遠不會保存負數(shù)。有些語言中將負數(shù)賦給 unsigned 類型是非法的,但在 C++ 中這是合法的。


            In C++ it is perfectly legal to assign a negative number to an object with unsigned type. The result is the negative value modulo the size of the type. So, if we assign 1 to an 8-bit unsigned char, the resulting value will be 255, which is 1 modulo 256.

            C++ 中,把負值賦給 unsigned 對象是完全合法的,其結(jié)果是該負數(shù)對該類型的取值個數(shù)求模后的值。所以,如果把 -1 賦給8位的 unsigned char,那么結(jié)果是 255,因為 255 是 -1 對 256 求模后的值。



            When assigning an out-of-range value to a signed type, it is up to the compiler to decide what value to assign. In practice, many compilers treat signed types similarly to how they are required to treat unsigned types. That is, they do the assignment as the remainder modulo the size of the type. However, we are not guaranteed that the compiler will do so for the signed types.

            當將超過取值范圍的值賦給 signed 類型時,由編譯器決定實際賦的值。在實際操作中,很多的編譯器處理 signed 類型的方式和 unsigned 類型類似。也就是說,賦值時是取該值對該類型取值數(shù)目求模后的值。然而我們不能保證編譯器都會這樣處理 signed 類型。



            以上摘自 c++ primer,慚愧,還是再細細的從頭品一次這書吧。









            posted on 2011-02-13 16:12 Brandon 閱讀(4949) 評論(0)  編輯 收藏 引用 所屬分類: C++

            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            收藏夾

            IT WEB

            常用鏈接

            牛人BLOG

            學(xué)習(xí)網(wǎng)站

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            国产成年无码久久久久毛片| 精品久久久久久久国产潘金莲| 99国产精品久久久久久久成人热| 日本久久久久亚洲中字幕| 无码精品久久久天天影视| 青青久久精品国产免费看| 久久人人爽人人爽人人片av麻烦| 亚洲国产精品无码成人片久久| 亚洲欧美日韩精品久久| 久久亚洲中文字幕精品一区| 久久综合给合久久狠狠狠97色69| 久久国产精品99精品国产| 人人狠狠综合88综合久久| 国产精品久久久久9999| 亚洲伊人久久成综合人影院 | 日本国产精品久久| 亚洲精品无码久久久久去q | 久久久久久噜噜精品免费直播| 亚洲综合精品香蕉久久网| 久久久WWW免费人成精品| 97久久久久人妻精品专区| 久久无码AV中文出轨人妻| 久久97久久97精品免视看秋霞| 久久精品无码午夜福利理论片 | 老司机午夜网站国内精品久久久久久久久 | 久久久久国产精品三级网| 久久精品草草草| 国产精品欧美久久久天天影视 | 国产精品青草久久久久婷婷 | 久久久久久无码国产精品中文字幕 | 精品国产VA久久久久久久冰| 伊人久久大香线蕉综合网站| 久久亚洲国产成人精品无码区| 91亚洲国产成人久久精品网址| 精品久久久久久| 97久久香蕉国产线看观看| 国产一久久香蕉国产线看观看| 亚洲国产美女精品久久久久∴| 影音先锋女人AV鲁色资源网久久| 精品久久久久久无码不卡| 一本色道久久综合狠狠躁|