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

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>
            另类尿喷潮videofree | 欧美片网站免费| 欧美一区二区三区久久精品茉莉花| 亚洲三级视频在线观看| 欧美激情亚洲一区| 亚洲人体1000| 亚洲免费在线视频| 久久久久久久综合| 欧美国产精品久久| 国产精品久久久久7777婷婷| 国产性天天综合网| 亚洲国产cao| 中文在线一区| 久久久久亚洲综合| 亚洲国产小视频在线观看| 99视频超级精品| 久久精品国产久精国产爱| 欧美久久久久久蜜桃| 国产欧美日韩精品丝袜高跟鞋| 国外成人网址| 在线性视频日韩欧美| 久久久久国产成人精品亚洲午夜| 欧美国产激情二区三区| 亚洲视频axxx| 男人的天堂成人在线| 国产精品视频第一区| 亚洲国产第一| 欧美影院一区| 日韩午夜激情| 猛男gaygay欧美视频| 国产精品日韩欧美大师| 日韩一级免费观看| 久久人人超碰| 亚洲深爱激情| 欧美日韩一二区| 在线免费观看成人网| 羞羞漫画18久久大片| 亚洲伦伦在线| 欧美国产日韩一二三区| 激情五月***国产精品| 亚洲欧美一区二区三区久久| 亚洲国内精品| 欧美成人首页| 一区二区在线看| 久久se精品一区精品二区| av成人毛片| 欧美日韩国产丝袜另类| 亚洲欧洲一区二区在线观看 | 亚洲激情视频在线观看| 久久视频精品在线| 国内精品久久久久久久果冻传媒| 午夜精品成人在线视频| 一本久久综合亚洲鲁鲁| 欧美激情国产精品| 亚洲区在线播放| 亚洲电影观看| 欧美精品在线视频| 在线亚洲一区二区| 9久草视频在线视频精品| 欧美国产一区二区| aa级大片欧美三级| 99www免费人成精品| 欧美视频网址| 亚洲伊人色欲综合网| 99精品视频网| 国产精品嫩草99a| 欧美在线国产精品| 午夜视频在线观看一区二区三区| 国产亚洲一区在线| 玖玖在线精品| 免费久久久一本精品久久区| 91久久夜色精品国产网站| 亚洲国产精品专区久久| 欧美区二区三区| 亚洲一区二区三区四区五区午夜| 在线亚洲一区二区| 国产亚洲视频在线| 欧美+日本+国产+在线a∨观看| 久久婷婷麻豆| 一区二区三区四区在线| 亚洲欧美第一页| 精品动漫一区| 亚洲人成绝费网站色www| 欧美视频在线观看 亚洲欧| 午夜欧美视频| 久久婷婷麻豆| 午夜激情亚洲| 久久综合99re88久久爱| 中文在线资源观看视频网站免费不卡| 亚洲午夜精品17c| 黄色亚洲精品| 99国产精品| 今天的高清视频免费播放成人| 91久久黄色| 国产午夜精品一区理论片飘花 | 亚洲一区二区三区在线| 欧美在线观看你懂的| 日韩午夜激情av| 午夜国产一区| 99国内精品久久久久久久软件| 亚洲视频精选| 亚洲国产三级在线| 亚洲综合激情| 一区二区精品在线| 久久天堂av综合合色| 亚洲专区国产精品| 女女同性精品视频| 久久免费视频网| 欧美亚州韩日在线看免费版国语版| 久久久久se| 国产精品免费在线| 亚洲欧洲在线一区| 亚洲成人在线视频网站| 亚洲免费网站| 一区二区三区 在线观看视频| 欧美在线免费看| 亚洲影视在线播放| 欧美福利在线观看| 男女视频一区二区| 国产一级精品aaaaa看| 日韩一级黄色片| 日韩视频不卡中文| 久久综合综合久久综合| 久久久久亚洲综合| 国产亚洲激情视频在线| 在线视频免费在线观看一区二区| 亚洲激情小视频| 久久久免费精品| 久久在线精品| 影音先锋亚洲电影| 久久久久网址| 免费视频一区| 亚洲国产精品一区| 免费成人激情视频| 亚洲成色www久久网站| 伊人精品成人久久综合软件| 久久精彩视频| 免费h精品视频在线播放| 一区二区亚洲精品国产| 久久在线播放| 亚洲成色最大综合在线| 亚洲日韩欧美一区二区在线| 欧美va天堂| 亚洲精品一区二区三区四区高清| 日韩一级网站| 国产精品毛片a∨一区二区三区|国 | 玖玖综合伊人| 亚洲国产精品电影| 欧美成年人视频网站| 亚洲区一区二| 亚洲免费小视频| 国产综合色产| 蜜桃av噜噜一区二区三区| 亚洲黄色毛片| 午夜激情亚洲| 亚洲第一精品久久忘忧草社区| 老司机成人在线视频| 亚洲激情视频| 欧美亚洲一区三区| 国产在线日韩| 欧美精品一区二区三区四区| 一区二区三区精品视频| 欧美在线一二三区| 亚洲国内欧美| 国产精品久久久久久久久久尿 | 免费成人av在线| 一区二区不卡在线视频 午夜欧美不卡'| 欧美国产视频在线| 亚洲男人第一av网站| 欧美成人亚洲成人日韩成人| 一区二区三区视频观看| 国产日韩欧美日韩| 欧美国产日韩一二三区| 亚洲欧美一区在线| 亚洲国产99精品国自产| 午夜国产精品视频免费体验区| 亚洲大片一区二区三区| 国产精品久在线观看| 玖玖玖国产精品| 亚洲在线视频免费观看| 欧美激情在线观看| 欧美怡红院视频一区二区三区| 亚洲区一区二区三区| 国产在线精品一区二区夜色| 欧美日韩免费一区二区三区| 久久久女女女女999久久| 亚洲一区二区三区777| 亚洲高清精品中出| 久久噜噜噜精品国产亚洲综合| 中文日韩在线视频| 亚洲激情av| 樱桃国产成人精品视频| 国产精品夜夜嗨| 欧美日韩国产系列| 欧美高清一区二区| 老色批av在线精品| 久久久91精品国产一区二区精品| 亚洲一区久久久| 一区二区三区www| 亚洲人成高清|