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

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

導(dǎo)航

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

統(tǒng)計(jì)

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章分類

文章檔案

my

搜索

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产亚洲一区二区精品| 日韩系列在线| 一区二区三区国产在线| 亚洲福利视频一区二区| 在线精品国产欧美| 亚洲日本无吗高清不卡| 日韩视频第一页| 亚洲视频在线观看网站| 欧美亚洲一区二区在线观看| 亚洲欧美清纯在线制服| 欧美黑人在线播放| 亚洲最新色图| 99在线观看免费视频精品观看| 欧美日韩hd| 亚洲一区二区毛片| 亚洲免费大片| 国产精品mm| 欧美亚洲专区| 久久精品国产清高在天天线| 韩国欧美国产1区| 免费视频最近日韩| 蜜臀av性久久久久蜜臀aⅴ四虎| 最新国产成人在线观看| 亚洲黄色影片| 免费不卡在线观看| 免费试看一区| 亚洲欧美bt| 欧美激情视频一区二区三区在线播放| 国产精品系列在线| 亚洲美女av在线播放| 久久狠狠婷婷| 99精品久久| 欧美国产极速在线| 精品不卡一区| 久久国产乱子精品免费女| avtt综合网| 欧美精品电影| 国内精品一区二区三区| 午夜一级在线看亚洲| 亚洲国产高清视频| 久久婷婷激情| 精品二区视频| 狼狼综合久久久久综合网 | 欧美日韩一区三区| 一区二区三区在线看| 小处雏高清一区二区三区 | 亚洲一区二区三区精品在线| 欧美xx视频| 久久午夜电影网| 国内精品久久久久久| 性视频1819p久久| 日韩视频在线观看国产| 欧美激情精品| 亚洲欧洲精品成人久久奇米网| 久久噜噜亚洲综合| 欧美一区视频| 国产在线拍偷自揄拍精品| 欧美一区二区三区免费大片| 99综合视频| 欧美日韩视频在线一区二区| 日韩亚洲欧美一区| 日韩图片一区| 鲁大师成人一区二区三区| 麻豆91精品91久久久的内涵| 国产精品黄色| 欧美激情一区二区三区在线视频观看| 国产精品爽爽ⅴa在线观看| 欧美激情一二三区| 韩日在线一区| 欧美一区二区高清| 亚洲欧美卡通另类91av| 欧美大片网址| 免费人成网站在线观看欧美高清| 国产午夜精品福利| 亚洲一区二区欧美| 国产欧美韩日| 亚洲一区二区毛片| 亚洲一区二区在线免费观看视频| 欧美成人在线影院| 国产精品视频免费在线观看| 香蕉乱码成人久久天堂爱免费 | 亚洲欧美日韩中文播放| 国产一区二区激情| 亚洲高清精品中出| 国产精品成人午夜| 麻豆成人在线观看| 欧美日韩中文| 久久夜色精品国产亚洲aⅴ| 麻豆9191精品国产| 亚洲一区二区动漫| 久久天天躁夜夜躁狠狠躁2022| 亚洲午夜在线观看视频在线| 欧美一区二区三区精品电影| 91久久精品美女高潮| 亚洲男人av电影| 亚洲精品欧美在线| 亚洲欧美欧美一区二区三区| 亚洲精品一二三| 欧美一级视频精品观看| 日韩亚洲欧美一区| 久久精品久久综合| 亚洲欧美激情视频| 欧美91精品| 久久久精品国产99久久精品芒果| 欧美黄色aaaa| 欧美1区视频| 国产欧美日韩在线播放| 亚洲精品视频一区| 一区二区亚洲精品国产| 亚洲男人av电影| 亚洲欧美日韩成人高清在线一区| 美女网站久久| 另类酷文…触手系列精品集v1小说| 欧美日韩在线精品| 欧美国产乱视频| 国产老女人精品毛片久久| 最新国产乱人伦偷精品免费网站| 黑人操亚洲美女惩罚| 亚洲午夜激情网页| 在线性视频日韩欧美| 久久综合色综合88| 另类欧美日韩国产在线| 国内精品久久国产| 亚洲欧美日韩国产综合| 久久综合九色| 黄色亚洲网站| 欧美成人一区二区三区| 国产一区三区三区| 欧美激情第8页| 性欧美xxxx视频在线观看| 国产最新精品精品你懂的| 亚洲美女在线看| 亚洲免费网址| 国产日本欧美在线观看| 亚洲综合清纯丝袜自拍| 一区二区三区av| 欧美色网在线| 亚洲女人天堂成人av在线| 香蕉免费一区二区三区在线观看| 国产精品一区久久| 欧美亚洲免费高清在线观看| 久久国产福利国产秒拍| 国产伦精品一区二区三区| 欧美亚洲三级| 欧美va天堂| 亚洲精品中文字幕在线| 欧美精品一线| 亚洲一区二区在线视频| 欧美在线首页| 在线观看欧美激情| 欧美黄色影院| 亚洲一区二区精品| 久久综合九色99| 99爱精品视频| 国产日韩精品久久| 久久综合999| 一区二区三区成人| 久久久久久**毛片大全| 亚洲国产精品尤物yw在线观看| 欧美国产日本在线| 亚洲图片自拍偷拍| 久久综合久久综合久久| 亚洲三级影片| 国产精品每日更新在线播放网址| 欧美一级播放| 亚洲精品在线免费| 久久精品国产一区二区三区免费看| 在线看欧美视频| 国产精品白丝黑袜喷水久久久| 久久精品国产亚洲一区二区三区| 亚洲国内欧美| 久久精品国产99国产精品| 亚洲人成网站999久久久综合| 国产精品高清在线观看| 久久久噜噜噜久久狠狠50岁| 在线一区二区三区四区| 美女尤物久久精品| 亚洲一区二区三区免费观看 | 国精品一区二区三区| 欧美国产亚洲另类动漫| 性色av一区二区三区在线观看| 欧美成人激情在线| 午夜视频一区在线观看| 最新亚洲一区| 国产一区二区三区无遮挡| 欧美精品自拍偷拍动漫精品| 午夜精品影院| 亚洲女同在线| 亚洲视频 欧洲视频| 亚洲国产欧美久久| 麻豆视频一区二区| 欧美在线视频一区二区三区| 国产精品99久久久久久久vr| 亚洲国产精品久久久久婷婷老年 | 亚洲欧美国产毛片在线| 亚洲欧美日韩视频一区| 欧美在线电影| 欧美成人精品一区二区三区| 日韩手机在线导航| 欧美亚洲在线视频|