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

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

導航

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

統計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章分類

文章檔案

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>
            亚洲国产精品成人va在线观看| 国产精品普通话对白| 99国产精品99久久久久久粉嫩| 女人天堂亚洲aⅴ在线观看| 91久久极品少妇xxxxⅹ软件| 亚洲激情成人| 国产精品激情电影| 久久狠狠一本精品综合网| 欧美成人午夜激情视频| 亚洲一卡久久| 狠狠综合久久av一区二区小说| 欧美成人小视频| 亚洲欧美日韩国产成人| 久久漫画官网| 中国av一区| 91久久久久久国产精品| 久久国产欧美精品| 日韩视频永久免费| 美女视频黄 久久| 午夜精品免费视频| aa亚洲婷婷| 亚洲精品国产欧美| 国产中文一区二区三区| 欧美视频网址| 欧美人与性动交cc0o| 久久精品三级| 午夜精品美女久久久久av福利| 99re热这里只有精品视频| 久久综合福利| 久久久亚洲人| 久久婷婷色综合| 欧美专区在线观看| 欧美一区二区免费观在线| 国产精品青草久久久久福利99| 久久国产精品久久精品国产| 老司机久久99久久精品播放免费| 激情综合激情| 国产精品videossex久久发布| 99精品国产在热久久下载| 久久成人精品无人区| 亚洲国产乱码最新视频| 国产精品乱人伦中文| 欧美伊人精品成人久久综合97 | 久久久久九九九九| 日韩视频国产视频| 亚洲精品国产欧美| 国产一区二区剧情av在线| 欧美系列精品| 欧美无乱码久久久免费午夜一区| 免费亚洲一区| 欧美福利视频| 欧美精品福利在线| 欧美日韩国产三区| 国产精品亚洲成人| 曰韩精品一区二区| 中日韩视频在线观看| 欧美在线看片| 欧美成人69| 91久久黄色| 亚洲欧美国产日韩天堂区| 夜夜爽av福利精品导航 | 欧美成人r级一区二区三区| 午夜精品久久久久久久久久久久久| 亚洲欧洲精品一区二区三区| 亚洲福利视频一区| 99天天综合性| 免费成人在线视频网站| 国产精品女人网站| 在线亚洲观看| 农夫在线精品视频免费观看| 国产精品日韩在线播放| 亚洲国产日韩一区二区| 久久精品国产在热久久| 亚洲人成网站精品片在线观看 | 久久久久一区| 国产精品乱子久久久久| 亚洲人成网站色ww在线| 久久蜜桃资源一区二区老牛| 亚洲欧美高清| 国产日韩精品视频一区| 性高湖久久久久久久久| 亚洲视频一二| 亚洲国产高清一区| 欧美日韩精品伦理作品在线免费观看 | 欧美亚洲色图校园春色| 日韩亚洲欧美一区二区三区| 国产精品99久久久久久久久 | 一区二区三区在线观看视频| 在线中文字幕一区| 亚洲一区二区三区中文字幕| 欧美伦理视频网站| 一区二区三区偷拍| 欧美中文字幕久久| 亚洲精一区二区三区| 国产精品三区www17con| 亚洲自拍偷拍麻豆| 久久综合一区| 美脚丝袜一区二区三区在线观看| 亚洲在线观看视频网站| 亚洲视频欧美在线| 国产亚洲亚洲| 欧美精品在线网站| 欧美中文字幕不卡| 欧美电影在线观看| 欧美中文字幕久久| 99热这里只有精品8| 国产精品视频免费观看www| 久久精品国产精品| 艳女tv在线观看国产一区| 久久香蕉国产线看观看网| 亚洲午夜在线视频| 亚洲精品国精品久久99热| 国产麻豆日韩| 国产精品视频观看| 国产九九精品| 国产精品揄拍一区二区| 欧美片第1页综合| 欧美aⅴ99久久黑人专区| 久久久福利视频| 欧美自拍偷拍午夜视频| 午夜精品久久| 亚洲欧美日韩国产综合| 翔田千里一区二区| 欧美一级成年大片在线观看| 一本综合久久| 亚洲欧美变态国产另类| 亚洲综合丁香| 久久女同互慰一区二区三区| 欧美专区亚洲专区| 欧美va天堂| 欧美精品久久久久久| 欧美性猛片xxxx免费看久爱| 欧美成人一区二区| 久久久人成影片一区二区三区观看| 欧美一区二区啪啪| 欧美国产视频日韩| 欧美黄在线观看| 一区二区欧美日韩视频| 欧美综合二区| 欧美日韩综合不卡| 影音先锋日韩资源| 亚洲婷婷在线| 亚洲丁香婷深爱综合| 正在播放亚洲| 欧美日韩国产成人高清视频| 国产伦精品一区二区| 99精品久久| 免费一级欧美片在线播放| 亚洲欧洲视频| 久久综合伊人77777蜜臀| 国产精品视区| 亚洲免费伊人电影在线观看av| 美日韩精品免费| 久久av免费一区| 国产一区二三区| 久久婷婷久久| 美女黄毛**国产精品啪啪| 国产色综合久久| 欧美在线视频全部完| 亚洲欧洲精品一区二区| 久久综合久久综合这里只有精品| 国产午夜精品在线| 在线一区观看| 亚洲一区二区在线| 国产精品网站在线播放| 亚洲欧洲av一区二区| 亚洲尤物影院| 韩国一区二区三区美女美女秀| 欧美一区二区三区视频免费| 亚洲一区二区在线观看视频| 国产精品丝袜91| 你懂的国产精品永久在线| 免费高清在线视频一区·| 91久久久国产精品| 亚洲午夜在线观看| 国外成人在线| 在线欧美一区| 亚洲高清视频中文字幕| 麻豆视频一区二区| 亚洲国产精品va在线看黑人| 亚洲日本电影在线| 国产日韩欧美成人| 狠狠入ady亚洲精品经典电影| 麻豆91精品| 国产精品一区二区a| 欧美成人精品三级在线观看| 欧美日韩一区二区三区四区在线观看 | 国产精品老牛| 另类国产ts人妖高潮视频| 欧美日韩成人在线播放| 久久久999国产| 欧美视频在线播放| 91久久精品国产91久久性色| 久久国产高清| 亚洲国产精品成人综合| 欧美日韩视频在线一区二区| 久久久久久久999精品视频| 欧美视频在线观看| 亚洲桃花岛网站| 亚洲资源在线观看|