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

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>
            国产人久久人人人人爽| 久久久久久尹人网香蕉| 欧美福利一区二区三区| 精品成人国产在线观看男人呻吟| 久久一二三国产| 免费在线播放第一区高清av| 91久久久国产精品| 日韩天堂在线观看| 国产精品亚洲一区| 欧美aaaaaaaa牛牛影院| 欧美日韩精品欧美日韩精品一| 日韩视频一区二区三区在线播放免费观看 | 在线看不卡av| 日韩视频免费观看高清完整版| 国产精品初高中精品久久| 久久天天狠狠| 欧美日韩中文字幕在线| 久久久在线视频| 欧美日韩免费观看一区三区 | 1000精品久久久久久久久| 亚洲激情av| 国产欧美精品一区二区三区介绍| 老司机久久99久久精品播放免费| 欧美激情国产精品| 久久精品国产一区二区三| 欧美国产日韩视频| 久久久亚洲国产天美传媒修理工 | 久久久久综合网| 亚洲一区二区网站| 另类图片国产| 欧美一区二区三区四区夜夜大片| 开心色5月久久精品| 亚洲欧美另类综合偷拍| 欧美电影专区| 久久午夜羞羞影院免费观看| 欧美日韩卡一卡二| 欧美成人精品在线观看| 国产精品人成在线观看免费 | 亚洲精品在线免费| 亚洲大片av| 欧美一区二区三区视频免费播放| 亚洲免费观看高清完整版在线观看熊| 欧美一区三区二区在线观看| 午夜精品久久| 欧美日韩国产一级| 欧美激情免费观看| 在线观看欧美日韩| 久久精品91久久久久久再现| 亚洲欧美日韩成人高清在线一区| 欧美精品免费播放| 欧美成人影音| 在线看欧美日韩| 久久不射网站| 久久久免费av| 国语精品一区| 久久久久成人网| 久久免费视频在线观看| 国产欧美视频一区二区| 亚洲伊人网站| 先锋影院在线亚洲| 国产精品亚洲综合色区韩国| 一本一本久久| 亚洲一区二区精品| 欧美深夜影院| 亚洲欧美国产精品va在线观看| 亚洲专区在线| 国产模特精品视频久久久久 | 亚洲午夜精品久久久久久浪潮| 一区二区三区欧美亚洲| 欧美日韩成人在线视频| 亚洲精品免费网站| 亚洲一区二区精品视频| 国产精品自拍在线| 欧美中在线观看| 蜜臀99久久精品久久久久久软件| 一区二区三区在线视频播放| 久热re这里精品视频在线6| 欧美18av| 一区二区三区日韩精品| 国产精品专区h在线观看| 久久精品日产第一区二区| 欧美成人福利视频| 亚洲免费电影在线观看| 国产精品九九| 久久在线视频在线| 99re视频这里只有精品| 久久aⅴ国产欧美74aaa| 伊人久久大香线| 欧美黄色日本| 亚洲欧美日本日韩| 免费观看亚洲视频大全| 99国产精品久久久| 国产欧美日韩亚洲精品| 久久久久一区二区| 99在线视频精品| 久久天堂成人| 亚洲一区二区在线免费观看| 黄色日韩精品| 国产精品爱久久久久久久| 久久久精品999| 日韩视频不卡| 久久夜色精品国产亚洲aⅴ| 99精品欧美一区| 国产在线不卡| 欧美性猛交xxxx免费看久久久| 久久久久99| 亚洲综合久久久久| 亚洲高清免费| 久久嫩草精品久久久精品一| 99视频日韩| 激情91久久| 国产精品久久久久久久久果冻传媒| 久久亚洲国产精品一区二区 | 一区二区三区高清| 你懂的国产精品| 欧美一区亚洲一区| 一本色道**综合亚洲精品蜜桃冫| 一区二区亚洲精品| 国产精品一级久久久| 欧美三级午夜理伦三级中文幕| 久久久久久综合| 亚洲欧美日韩精品综合在线观看| 91久久精品一区| 欧美a级一区| 久久免费国产精品| 久久狠狠久久综合桃花| 亚洲一区视频在线| 一二三区精品福利视频| 亚洲精品乱码久久久久久日本蜜臀 | 欧美激情视频在线播放 | 亚洲精品视频在线看| 在线不卡欧美| 很黄很黄激情成人| 国外精品视频| 狠狠色丁香久久综合频道| 国产精品一区在线播放| 欧美午夜一区二区福利视频| 欧美日韩在线播放三区四区| 欧美激情 亚洲a∨综合| 欧美激情免费在线| 免费亚洲一区二区| 欧美xx69| 欧美激情一区在线观看| 欧美国产日产韩国视频| 欧美精品日韩一区| 欧美日韩一区二区三区在线看 | 久久精品日产第一区二区| 久久精品国产久精国产思思| 久久久999精品视频| 久久精品毛片| 久久综合国产精品| 免费亚洲电影| 欧美精品v日韩精品v国产精品| 欧美成人性生活| 欧美三级乱码| 国产目拍亚洲精品99久久精品| 国产精品久久久久久久午夜| 国产日韩精品入口| 黄色在线一区| 亚洲精选一区| 亚洲欧美一区二区三区久久| 欧美一区三区二区在线观看| 久久久久久**毛片大全| 欧美韩日精品| 夜夜精品视频| 日韩天堂在线观看| 亚洲一区二区在| 久久午夜羞羞影院免费观看| 亚洲国产成人在线视频| 亚洲一区二区精品在线观看| 久久久久久精| 欧美日韩视频免费播放| 国产日韩欧美在线视频观看| 18成人免费观看视频| 亚洲少妇最新在线视频| 久久精品二区亚洲w码| 亚洲国产视频一区| 亚洲免费小视频| 一区二区国产日产| 久久另类ts人妖一区二区| 亚洲网站视频| 99精品视频免费观看视频| 性色av一区二区三区在线观看| 最新国产の精品合集bt伙计| 国产精品入口尤物| 黄色日韩在线| 亚洲国产成人tv| 精品动漫一区| 午夜国产欧美理论在线播放| 亚洲福利专区| 久久久精品国产免费观看同学| 欧美色精品天天在线观看视频 | 香港久久久电影| 久久精品国产2020观看福利| 欧美激情综合色综合啪啪| 国产日韩欧美综合一区| 亚洲视频一起| 亚洲激情六月丁香| 久久一区二区三区av| 国产日韩欧美一区二区|