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

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>
            欧美人体xx| 亚洲手机视频| 免费在线日韩av| 亚洲第一成人在线| 亚洲成人在线视频网站| 噜噜噜噜噜久久久久久91| 亚洲成人资源| 亚洲国产综合在线| 欧美午夜精品久久久| 亚洲综合成人在线| 久久精品30| 日韩亚洲精品电影| 亚洲一区二区三区欧美| 国产一区二区三区久久久| 老司机精品久久| 欧美日韩黄色大片| 久久九九免费| 欧美激情导航| 久久成人av少妇免费| 另类专区欧美制服同性| 一本色道久久综合亚洲精品按摩 | 欧美中文字幕视频| 久久精品日韩一区二区三区| 亚洲欧洲精品一区二区| 亚洲一区二区三区高清不卡| 韩国av一区二区三区| 亚洲国产欧美一区二区三区丁香婷| 欧美日韩国产综合视频在线观看中文| 欧美亚洲专区| 欧美电影免费观看网站| 亚洲免费视频成人| 亚洲狠狠丁香婷婷综合久久久| 亚洲视频在线观看免费| 亚洲国产精品成人综合| 亚洲一区二区在线视频| 亚洲欧洲在线视频| 先锋影音国产一区| 宅男66日本亚洲欧美视频| 久久久久久精| 久久黄色网页| 国产精品黄色| 日韩一级二级三级| 亚洲国产婷婷香蕉久久久久久| 亚洲欧美一区二区视频| 一区二区三区www| 久热综合在线亚洲精品| 久久久久免费视频| 国产精品女人毛片| 日韩一级二级三级| 一本色道久久88亚洲综合88| 久久人人97超碰精品888| 欧美一级精品大片| 国产精品国产精品国产专区不蜜| 亚洲二区在线观看| 在线观看视频一区二区| 欧美亚洲色图校园春色| 羞羞答答国产精品www一本| 欧美三级视频| 日韩亚洲欧美在线观看| 在线视频欧美日韩| 欧美黄色影院| 亚洲精品综合精品自拍| 99精品视频免费在线观看| 欧美jizzhd精品欧美喷水 | 亚洲国产成人精品视频| 久久久久免费视频| 你懂的国产精品| 亚洲激情视频| 欧美精品一区二区蜜臀亚洲| 最近中文字幕mv在线一区二区三区四区 | 亚洲婷婷在线| 欧美一区二区三区视频| 国产小视频国产精品| 亚洲欧美一区二区原创| 久久国产精品毛片| 激情另类综合| 欧美成人黄色小视频| 99re6这里只有精品视频在线观看| 一区二区日韩精品| 国产精品日韩电影| 欧美一区二区三区四区夜夜大片| 久久人人爽人人爽| 亚洲国产视频a| 欧美日韩在线精品一区二区三区| 亚洲免费网址| 美女露胸一区二区三区| 日韩视频中文| 国产精品久久久久婷婷| 久久国产精品99久久久久久老狼| 欧美黑人国产人伦爽爽爽| 99综合在线| 国产午夜精品一区理论片飘花 | 欧美日韩在线三级| 亚洲欧美国产精品桃花| 另类图片国产| 亚洲一级二级在线| 国产自产v一区二区三区c| 老司机午夜精品视频在线观看| 亚洲精品欧美激情| 久久精品国产久精国产思思| 91久久国产综合久久| 国产精品久久久爽爽爽麻豆色哟哟| 欧美在线免费| 99国产精品99久久久久久| 久久三级视频| 亚洲一区欧美| 最新国产精品拍自在线播放| 国产欧美日本一区二区三区| 欧美经典一区二区| 欧美伊人久久久久久久久影院| 亚洲精品日韩在线| 久久精品视频一| 亚洲一区二区视频| 91久久久久久久久| 国内精品写真在线观看| 欧美日韩一区二区视频在线观看| 久久亚洲春色中文字幕久久久| 亚洲伊人网站| 一区二区三区视频在线播放| 亚洲丶国产丶欧美一区二区三区| 久久精品人人爽| 亚洲欧美综合网| 亚洲调教视频在线观看| 亚洲国产精品一区二区www| 国产欧美日韩精品在线| 欧美性感一类影片在线播放| 欧美成人乱码一区二区三区| 久久久久久尹人网香蕉| 欧美一二三视频| 亚洲永久免费| 亚洲欧美经典视频| 一区二区三区欧美激情| 亚洲免费黄色| 99视频热这里只有精品免费| 91久久精品久久国产性色也91 | 亚洲私人影吧| 一区二区三区久久网| 亚洲精品亚洲人成人网| 亚洲经典一区| 亚洲伦理在线免费看| 亚洲国产精品欧美一二99| 亚洲国产精品一区二区第四页av| 在线观看欧美精品| 又紧又大又爽精品一区二区| 一区二区三区我不卡| 一区二区三区在线不卡| 在线成人激情黄色| 亚洲国产高清在线| 亚洲精品久久久久久久久久久久久| 亚洲国产精品福利| 亚洲免费av网站| 中文欧美字幕免费| 午夜视频精品| 久久综合国产精品| 欧美大尺度在线| 亚洲欧洲日本在线| 在线综合亚洲| 欧美中文在线视频| 麻豆精品一区二区综合av| 男女av一区三区二区色多| 欧美国产一区二区在线观看 | 欧美三级精品| 国产欧美视频一区二区| 国外成人在线视频| 亚洲日本欧美日韩高观看| 亚洲免费不卡| 欧美主播一区二区三区| 老色批av在线精品| 亚洲激情啪啪| 亚洲欧美一级二级三级| 美腿丝袜亚洲色图| 欧美天天影院| 亚洲福利视频网站| 在线亚洲欧美专区二区| 欧美中在线观看| 亚洲日本乱码在线观看| 国产婷婷色一区二区三区四区 | 国产精品久久久久久久午夜片| 国内精品久久久久影院 日本资源 国内精品久久久久伊人av | 99精品欧美| 久久久久久91香蕉国产| 欧美母乳在线| 欲色影视综合吧| 亚洲欧美日韩国产成人| 欧美.com| 欧美一区二区三区的| 欧美日韩国产成人高清视频| 国产主播一区二区三区四区| 99精品国产高清一区二区 | 亚洲一区二区在线看| 欧美xx视频| 先锋影音国产一区| 欧美午夜影院| 亚洲免费av电影| 欧美aⅴ一区二区三区视频| 亚洲一二三区在线| 欧美精品 国产精品| 激情文学综合丁香| 性伦欧美刺激片在线观看| 亚洲欧洲一区二区三区|