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

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>
            麻豆成人小视频| 在线一区视频| 久久深夜福利免费观看| 亚洲欧美色婷婷| 国产色视频一区| 久久综合狠狠综合久久综合88| 久久久999精品| 91久久极品少妇xxxxⅹ软件| 日韩视频永久免费观看| 国产精品高潮呻吟久久av黑人| 欧美中文字幕| 欧美成人福利视频| 亚洲欧美日韩一区在线| 快播亚洲色图| 亚洲在线电影| 老牛影视一区二区三区| 亚洲少妇在线| 久久精彩视频| 亚洲一区二区三区免费观看| 欧美一区二区精品| 一区二区高清视频在线观看| 亚洲免费伊人电影在线观看av| 亚洲国产美女精品久久久久∴| 一区二区三区精品视频在线观看| 韩国v欧美v日本v亚洲v| 中日韩男男gay无套| 在线播放视频一区| 亚洲性感美女99在线| 在线观看成人网| 亚洲一区二区三区中文字幕| 亚洲国产欧洲综合997久久| 亚洲午夜在线观看视频在线| 亚洲精品国产精品乱码不99| 欧美资源在线观看| 亚洲欧美成人一区二区在线电影| 麻豆精品在线视频| 久久久久久黄| 国产精品捆绑调教| 亚洲精品免费在线观看| 国产亚洲永久域名| 一区二区av在线| 亚洲精品一区中文| 久久黄色级2电影| 欧美永久精品| 国产精品视频一二| 日韩午夜在线观看视频| 亚洲精品视频中文字幕| 久久久久国色av免费观看性色| 亚洲男女自偷自拍| 欧美日本高清视频| 亚洲国产免费| 亚洲国产精品一区在线观看不卡 | 亚洲视频精选在线| 老牛国产精品一区的观看方式| 久久国产88| 国产啪精品视频| 亚洲一区二区三区精品在线观看 | 国产亚洲福利| 亚洲一区二区精品视频| 亚洲欧美成人| 国产精品久久久久aaaa九色| 一本高清dvd不卡在线观看| 亚洲国产精品传媒在线观看| 久热国产精品| 亚洲成人自拍视频| 亚洲日本视频| 欧美成人第一页| 亚洲国产女人aaa毛片在线| 亚洲精品美女免费| 欧美日韩国产一中文字不卡| 亚洲日本免费| 亚洲视频福利| 国产精品日本一区二区| 午夜精品免费视频| 久久aⅴ乱码一区二区三区| 国产亚洲电影| 米奇777超碰欧美日韩亚洲| 亚洲二区在线| 中文欧美字幕免费| 国产精品一级在线| 久久国产精品免费一区| 免费av成人在线| 亚洲精品免费在线播放| 欧美体内she精视频在线观看| 亚洲一区二区精品在线| 久久综合99re88久久爱| 91久久精品国产91久久| 国产精品二区影院| 欧美亚洲日本一区| 欧美大片在线观看一区| 亚洲午夜免费福利视频| 国产一区二区高清| 欧美国内亚洲| 亚洲综合另类| 欧美激情精品久久久久久| 亚洲一区久久久| 国内伊人久久久久久网站视频| 免费在线日韩av| 亚洲欧美高清| 亚洲国产精品ⅴa在线观看 | 欧美在线视频导航| 亚洲丶国产丶欧美一区二区三区 | 欧美专区在线播放| 99视频精品全部免费在线| 久久精品在线视频| 一本色道久久综合狠狠躁篇的优点| 国产精品久久久久久五月尺| 久久亚洲精品欧美| 亚洲午夜电影网| 欧美激情中文字幕一区二区| 先锋影音国产精品| 亚洲日本激情| 加勒比av一区二区| 国产精品久久中文| 欧美金8天国| 久久在线视频在线| 午夜欧美不卡精品aaaaa| 最新国产精品拍自在线播放| 久久综合图片| 性欧美大战久久久久久久久| 一区二区三区高清在线观看| 在线观看91精品国产入口| 国产精品视频精品| 欧美日韩1区2区3区| 久久久777| 久久国产精品亚洲va麻豆| 亚洲一区二区免费视频| 99国产一区二区三精品乱码| 欧美黄色影院| 免费观看成人| 玖玖玖免费嫩草在线影院一区| 香蕉久久夜色| 亚洲女女女同性video| 一区二区三区视频观看| 亚洲久久一区| 91久久嫩草影院一区二区| 亚洲国产精品t66y| 在线观看欧美一区| 狠狠色噜噜狠狠色综合久| 国产专区欧美专区| 国产亚洲午夜高清国产拍精品| 国产精品尤物福利片在线观看| 国产精品久久一级| 国产精品夫妻自拍| 欧美午夜宅男影院| 国产精品高潮呻吟久久av无限| 国产精品av久久久久久麻豆网| 欧美日韩国产影院| 国产精品二区影院| 国产精品日本精品| 国内精品伊人久久久久av一坑| 国产一级精品aaaaa看| 国色天香一区二区| 亚洲高清在线| 99在线观看免费视频精品观看| 一区二区精品在线| 亚洲免费网址| 久久精品免费电影| 你懂的国产精品永久在线| 欧美成人69av| 亚洲激情网站| 在线中文字幕一区| 欧美中日韩免费视频| 久久亚洲高清| 欧美日韩日本网| 国产人成精品一区二区三| 激情丁香综合| 99国产精品自拍| 久久gogo国模裸体人体| 浪潮色综合久久天堂| 亚洲人屁股眼子交8| 亚洲欧美日韩国产一区二区| 久久久久久久久久久成人| 欧美激情一区二区三区四区| 国产精自产拍久久久久久| 在线高清一区| 午夜电影亚洲| 欧美电影免费网站| 在线视频欧美一区| 久久亚洲国产精品日日av夜夜| 欧美日韩一区二区高清| 国产主播在线一区| 在线综合亚洲欧美在线视频| 久久av一区二区| 亚洲日本成人网| 久久久精品一区| 国产精品高清免费在线观看| 亚洲第一在线视频| 久久av一区二区三区漫画| 亚洲日本中文字幕免费在线不卡| 亚洲免费影院| 欧美日本不卡| 亚洲国内自拍| 久久尤物视频| 午夜精品久久久久久久久| 欧美日韩国产精品一区| 亚洲国产裸拍裸体视频在线观看乱了中文| 亚洲一级影院| 亚洲激情在线激情| 久久久另类综合|