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

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

導航

<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

統(tǒng)計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章分類

文章檔案

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>
            精品91久久久久| 国产精品一区2区| 经典三级久久| 久久资源在线| 另类尿喷潮videofree | 亚洲电影自拍| 久久综合伊人77777麻豆| 亚洲国产成人久久综合一区| 欧美黄色aaaa| 欧美日韩在线三级| 午夜亚洲视频| 久久亚洲春色中文字幕久久久| 在线欧美不卡| 亚洲精品午夜精品| 国产精品久久久一本精品| 久久精品成人| 欧美成人免费全部| 亚洲欧美国产高清va在线播| 久久精品免视看| 亚洲激情在线| 亚洲一区二区三区精品视频| 一区二区三区在线免费视频| 亚洲电影免费在线观看| 欧美视频一区二区| 麻豆国产精品va在线观看不卡| 欧美成人dvd在线视频| 亚洲综合首页| 久久亚洲一区二区三区四区| 一本大道久久a久久综合婷婷| 亚洲影院在线| 最近中文字幕mv在线一区二区三区四区 | 国产欧美精品| 欧美国产一区二区在线观看| 国产精品国产精品| 嫩模写真一区二区三区三州| 国产精品毛片一区二区三区 | 99视频精品在线| 先锋影音网一区二区| 亚洲精品九九| 久久精品国语| 欧美亚洲视频在线观看| 欧美精品电影在线| 免费的成人av| 国产欧美一区二区精品婷婷| 亚洲日本久久| 尤物九九久久国产精品的特点 | 久久久久久久91| 欧美日韩亚洲视频一区| 免费的成人av| 国产专区欧美专区| 亚洲一区二区四区| 亚洲特级毛片| 欧美日韩国产在线一区| 欧美国产日韩在线| 在线观看视频免费一区二区三区| 亚洲中午字幕| 亚洲欧美视频一区二区三区| 欧美日韩精品不卡| 91久久久亚洲精品| 亚洲区第一页| 免费成人性网站| 欧美va天堂在线| 在线成人激情黄色| 久久久久高清| 欧美gay视频| 在线免费不卡视频| 蜜桃av综合| 亚洲电影观看| 亚洲精品免费电影| 欧美大片一区二区| 亚洲精选91| 亚洲亚洲精品在线观看 | 午夜一区二区三区在线观看| 欧美亚洲视频在线观看| 国产欧美日韩精品一区| 亚洲欧美日韩成人高清在线一区| 欧美一区二区国产| 国产丝袜美腿一区二区三区| 午夜精品在线观看| 老司机67194精品线观看| 黄色成人在线| 欧美大片免费观看在线观看网站推荐| 亚洲国产一区二区三区在线播| 99re热这里只有精品免费视频| 欧美精品福利| 亚洲视频香蕉人妖| 欧美一级免费视频| 在线不卡视频| 欧美日韩在线观看一区二区三区| 一本一道久久综合狠狠老精东影业| 亚洲一线二线三线久久久| 国产精品视频成人| 久久精品一区二区三区四区| 亚洲高清免费| 亚洲制服av| 在线观看的日韩av| 欧美黑人在线观看| 午夜精品在线视频| 亚洲大胆视频| 欧美一区二区三区婷婷月色| 狠狠色综合网站久久久久久久| 欧美成年人在线观看| 亚洲手机在线| 欧美大秀在线观看| 亚洲欧美国产另类| 亚洲激情一区二区| 国产日本欧美一区二区三区| 另类av导航| 欧美一级淫片播放口| 亚洲国产精品va在线看黑人动漫| 西西裸体人体做爰大胆久久久| 亚洲国产一区二区三区青草影视| 国产精品久久国产愉拍| 免费亚洲电影在线观看| 在线视频日韩精品| 欧美激情精品| 久久久综合网站| 亚洲一区二区三区777| 亚洲茄子视频| 精品91在线| 国产乱码精品| 欧美日韩一区免费| 欧美成年人视频网站欧美| 久久aⅴ国产欧美74aaa| 一区二区三区视频观看| 亚洲精品1区| 欧美国产日本在线| 久久婷婷一区| 久久国产精品99久久久久久老狼| 99精品国产福利在线观看免费 | 在线日韩av| 国产亚洲精品久久久久动| 欧美体内she精视频| 欧美精品18+| 美女脱光内衣内裤视频久久网站| 欧美制服丝袜第一页| 亚洲欧美文学| 午夜精品福利在线| 亚洲午夜在线视频| 亚洲一区二区免费视频| 夜夜爽av福利精品导航| 日韩午夜精品| 99精品国产在热久久下载| 99这里有精品| 99re6热在线精品视频播放速度| 亚洲国产你懂的| 亚洲第一精品在线| 亚洲国产精品第一区二区| 亚洲第一久久影院| 亚洲人成免费| 99视频一区二区| 日韩视频免费看| 亚洲天堂成人在线观看| 这里只有精品视频| 亚洲欧美日韩在线不卡| 性久久久久久久久| 久久精品中文字幕一区二区三区| 久久国产精品99国产| 老司机免费视频一区二区| 欧美国产精品| 欧美日韩一区不卡| 国产欧美精品一区| 黄色成人av在线| 亚洲精品一线二线三线无人区| 亚洲精品日韩欧美| 亚洲一区二区免费| 久久精品一二三| 亚洲第一区中文99精品| 亚洲精品免费网站| 亚洲欧美日本视频在线观看| 欧美中文字幕视频| 欧美xart系列高清| 国产精品美女在线观看| 激情自拍一区| 99国产精品一区| 欧美专区在线观看一区| 免费高清在线一区| 一本色道久久综合亚洲二区三区 | 亚洲第一天堂无码专区| 日韩视频一区二区| 欧美在线观看网站| 欧美黄色免费| 国产一区二区三区久久| 亚洲日韩成人| 欧美亚洲一区二区在线| 欧美二区在线看| 亚洲婷婷综合色高清在线| 久久久久久久久久久久久久一区 | 久久亚洲免费| 国产精品免费一区二区三区在线观看| 精品成人在线观看| 亚洲伊人伊色伊影伊综合网| 蜜桃av一区二区| 在线午夜精品自拍| 欧美国产精品日韩| 国内外成人免费视频| 亚洲在线国产日韩欧美| 蜜臀av性久久久久蜜臀aⅴ| 亚洲视频图片小说| 欧美激情欧美激情在线五月|