青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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 閱讀(818) 評論(0)  編輯 收藏 引用 所屬分類: Traps

導航

<2012年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

統計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章分類

文章檔案

my

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产亚洲激情| 久久久久久亚洲精品不卡4k岛国| 最新高清无码专区| 99综合电影在线视频| 欧美一级午夜免费电影| 欧美国产亚洲精品久久久8v| 亚洲一品av免费观看| 欧美成人中文| 影音先锋久久久| 欧美一级二级三级蜜桃| 性久久久久久久| 国产精品久久国产精品99gif | 国产精品视频久久| 亚洲三级视频| 欧美成人一二三| 欧美伊人久久大香线蕉综合69| 欧美视频在线视频| 夜夜狂射影院欧美极品| 国产精品99久久久久久宅男| 欧美精品二区| 一区二区精品在线| 亚洲精品视频一区二区三区| 欧美jjzz| 99在线精品免费视频九九视| 一区二区三区欧美成人| 欧美精品少妇一区二区三区| 欧美一级一区| 欧美国产日韩a欧美在线观看| 极品少妇一区二区| 欧美14一18处毛片| 蜜乳av另类精品一区二区| 激情校园亚洲| 正在播放日韩| 亚洲日本一区二区| 欧美中文字幕视频| 亚洲资源av| 午夜欧美理论片| 亚洲九九九在线观看| 亚洲精品中文字幕在线| 国产字幕视频一区二区| 欧美成人中文字幕在线| 国产精品久久久久国产a级| 欧美成在线视频| 国内精品视频在线观看| 在线视频精品一区| 亚洲精选国产| 欧美成人精品福利| 欧美99久久| 精品成人免费| 欧美一级免费视频| 亚洲国产精品成人va在线观看| 亚洲国产导航| 欧美特黄一区| 亚洲国产精品久久久久婷婷884| 精品999在线播放| 午夜久久久久| 欧美在线视频播放| 久久人人爽人人爽| 亚洲视频一二| 久久久青草婷婷精品综合日韩| 欧美一区二区观看视频| 久久精品盗摄| 在线视频你懂得一区二区三区| 久久免费精品日本久久中文字幕| 久久噜噜噜精品国产亚洲综合 | 亚洲激精日韩激精欧美精品| 亚洲国产成人高清精品| 久久人体大胆视频| 欧美国产精品劲爆| 最新中文字幕一区二区三区| 亚洲欧美日韩久久精品 | 国产精品入口麻豆原神| 亚洲在线电影| 中日韩美女免费视频网址在线观看 | 性感少妇一区| 欧美va亚洲va日韩∨a综合色| 亚洲电影一级黄| 亚洲一区二区三区在线播放| 欧美一级视频免费在线观看| 国产亚洲一区在线| 久久一区二区三区国产精品| 亚洲国产1区| 亚洲永久免费| 黄色精品一区二区| 亚洲欧美成人在线| 一区二区三区精品视频| 国产精品激情| 久久免费99精品久久久久久| 欧美激情精品久久久久久蜜臀| 99re热精品| 欧美国产日韩二区| 在线一区二区日韩| 毛片基地黄久久久久久天堂| 国产视频一区在线观看| 亚洲最新合集| 一区二区三区视频免费在线观看| 国产精品久线观看视频| 一本高清dvd不卡在线观看| 国产精品免费网站| 欧美亚洲日本国产| 在线成人欧美| 欧美日韩直播| 久久九九99| 欧美一区二区三区另类| 亚洲国产成人在线播放| 欧美午夜电影在线| 日韩视频一区二区三区| 99国产欧美久久久精品| 国产欧美欧美| 欧美中文字幕在线| 99精品视频免费观看| 久久综合网hezyo| 午夜一区不卡| 99精品国产99久久久久久福利| 国产日韩欧美综合| 欧美色综合天天久久综合精品| 久久天堂av综合合色| 亚洲香蕉网站| 亚洲精品一区二| 欧美激情视频一区二区三区不卡| 欧美一区激情| 亚洲欧美日韩国产中文| 国产伦精品一区二区三| 久久福利电影| 亚洲综合精品四区| 亚洲视频1区2区| 亚洲精品偷拍| 亚洲日本中文字幕免费在线不卡| 一本色道久久综合精品竹菊 | 亚洲韩国日本中文字幕| 欧美日本二区| 先锋a资源在线看亚洲| 亚洲伦理自拍| 亚洲精品中文字幕在线观看| 欧美激情精品久久久久久| 猛干欧美女孩| 亚洲一区二区欧美| 精品电影在线观看| 黑人巨大精品欧美一区二区| 国产亚洲精品资源在线26u| 国产精品综合av一区二区国产馆| 国产精品高潮呻吟久久av无限 | 狠狠久久五月精品中文字幕| 国产一区二区精品久久| 国模叶桐国产精品一区| 国产亚洲精品美女| 在线电影一区| 亚洲娇小video精品| 亚洲精品视频一区二区三区| 亚洲精品乱码久久久久久按摩观 | 亚洲无线一线二线三线区别av| 亚洲日韩视频| 在线视频亚洲| 午夜伦欧美伦电影理论片| 亚洲精品女av网站| 亚洲免费观看在线视频| 亚洲桃色在线一区| 欧美一区三区二区在线观看| 久久久一本精品99久久精品66| 可以看av的网站久久看| 欧美黄色网络| 一区二区三区久久| 午夜激情一区| 欧美mv日韩mv国产网站app| 欧美日韩亚洲视频| 猛干欧美女孩| 欧美日韩一区二区三区在线| 国产精品久久久久一区| 国内精品国产成人| 亚洲精品在线三区| 性感少妇一区| 欧美成人免费视频| 一区二区三区四区国产| 久久久精品五月天| 欧美日韩不卡一区| 欧美激情精品久久久久久| 欧美午夜大胆人体| 激情欧美一区二区三区| 一区二区久久久久| 久久久欧美精品| 91久久在线视频| 欧美一区网站| 欧美日韩国产综合网| 国产字幕视频一区二区| 在线视频中文亚洲| 蜜臀av性久久久久蜜臀aⅴ| 国产精品99久久久久久久久| 另类国产ts人妖高潮视频| 老司机免费视频久久| 国产精品久久久久久久7电影| 在线观看91精品国产入口| 亚洲一区二区少妇| 亚洲国产欧美另类丝袜| 久久成人18免费观看| 欧美无砖砖区免费| 99热在这里有精品免费| 麻豆成人小视频| 先锋影音久久| 国产精品视频网| 亚洲午夜精品在线|