• <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>

            chenglong7997

            Signed to unsigned conversion in C - is it always safe? (from stackoverflow)

            Suppose I have the following C code.

            unsigned int u = 1234;
            int i = -5678;

            unsigned int result = u + i;

            What implicit conversions are going on here, and is this code safe for all values of u and i? (Safe, in the sense that even though result in this example will overflow to some huge positive number, I could cast it back to an int and get the real result.)

            Answer:

            Short Answer

            Your i will be converted to an unsigned integer by adding UINT_MAX + 1, then the addition will be carried out with the unsigned values, resulting in a large result (depending on the values of u andi).

            Long Answer

            According to the C99 Standard:

            6.3.1.8 Usual arithmetic conversions

            1. If both operands have the same type, then no further conversion is needed.
            2. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
            3. Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
            4. Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
            5. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

            In your case, we have one unsigned int (u) and signed int (i). Referring to (3) above, since both operands have the same rank, your i will need to be converted to an unsigned integer.

            6.3.1.3 Signed and unsigned integers

            1. When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
            2. Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
            3. Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

            Now we need to refer to (2) above. Your i will be converted to an unsigned value by adding UINT_MAX + 1. So the result will depend on how UINT_MAX is defined on your implementation. It will be large, but it will not overflow, because:

            6.2.5 (9)

            A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

            Bonus: Arithmetic Conversion Semi-WTF

            #include <stdio.h>

            int main(void)
            {
             
            unsigned int plus_one = 1;
             
            int minus_one = -1;

             
            if(plus_one < minus_one)
                printf
            ("1 < -1");
             
            else
                printf
            ("boring");

             
            return 0;
            }

            You can use this link to try this online: http://codepad.org/yPhYCMFO

            Bonus: Arithmetic Conversion Side Effect

            Arithmetic conversion rules can be used to get the value of UINT_MAX by initializing an unsigned value to -1, ie:

            unsigned int umax = -1; // umax set to UINT_MAX

            This is guaranteed to be portable regardless of the signed number representation of the system because of the conversion rules described above. See this SO question for more information: Is it safe to use -1 to set all bits to true?

            posted on 2012-08-17 02:32 Snape 閱讀(774) 評論(0)  編輯 收藏 引用 所屬分類: Traps

            導航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            my

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产激情久久久久影院小草| 国产精品久久久久免费a∨| 亚洲欧美伊人久久综合一区二区| 一级a性色生活片久久无| 99精品久久精品一区二区| 热re99久久6国产精品免费| 狠狠干狠狠久久| 伊人久久五月天| 日韩精品国产自在久久现线拍| 久久精品亚洲欧美日韩久久| 久久久婷婷五月亚洲97号色| 国产高潮国产高潮久久久91 | 久久狠狠高潮亚洲精品| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 人妻中文久久久久| 精品久久久无码人妻中文字幕豆芽 | 国产精品免费福利久久| 人人狠狠综合久久亚洲高清| 久久久久四虎国产精品| 亚洲精品乱码久久久久久按摩 | 亚洲AV无码久久精品色欲| 久久久久久一区国产精品| 久久国产色AV免费看| 亚洲精品无码久久久久AV麻豆| 久久最近最新中文字幕大全 | 亚洲综合熟女久久久30p| 久久乐国产精品亚洲综合| 久久美女人爽女人爽| 99久久无码一区人妻a黑| 日本欧美久久久久免费播放网 | 久久久SS麻豆欧美国产日韩| 国产精品VIDEOSSEX久久发布| 久久精品国产亚洲AV电影| 久久久久久久久久久久久久 | 国产aⅴ激情无码久久| 亚洲精品国产自在久久| 色偷偷88欧美精品久久久| 亚洲国产日韩欧美久久| 亚洲午夜精品久久久久久浪潮| 亚洲国产成人乱码精品女人久久久不卡| 久久国产热这里只有精品|