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

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>
            久久精品日产第一区二区三区| 国内精品久久久久久久影视麻豆| 亚洲黄色成人| 男女视频一区二区| 欧美大片免费久久精品三p | 欧美日韩一区免费| 亚洲男人的天堂在线| 亚洲在线观看视频| 在线不卡中文字幕| 亚洲人成人一区二区三区| 欧美国产91| 午夜精品成人在线视频| 性欧美办公室18xxxxhd| 亚洲国产99精品国自产| 亚洲人精品午夜| 国产精品男gay被猛男狂揉视频| 久久精品国产99精品国产亚洲性色| 久久国产手机看片| 99视频在线观看一区三区| 亚洲一区二区三区四区在线观看 | 另类天堂av| 亚洲亚洲精品在线观看 | 这里只有精品电影| 伊人成年综合电影网| 日韩午夜激情| 黑人极品videos精品欧美裸| 亚洲欧洲视频| 国产婷婷成人久久av免费高清| 亚洲承认在线| 国产日韩亚洲欧美综合| 欧美激情一区二区三区蜜桃视频| 国产精品国码视频| 欧美激情精品久久久久久免费印度 | 亚洲免费人成在线视频观看| 欧美在线观看视频一区二区| 亚洲欧洲一区二区三区| 亚洲一区二区在线免费观看| 狠狠色噜噜狠狠狠狠色吗综合| 亚洲电影免费观看高清完整版在线观看 | 国产亚洲欧美一区二区三区| 欧美黄色精品| 国产精品视频免费观看www| 久久一区亚洲| 欧美午夜电影完整版| 久久麻豆一区二区| 欧美日韩少妇| 免费观看欧美在线视频的网站| 欧美日韩亚洲免费| 久色成人在线| 国产精品一区免费视频| 91久久久国产精品| 国内精品久久久久久久影视蜜臀 | 久久精品国产第一区二区三区最新章节 | 日韩亚洲一区二区| 亚洲国产成人91精品| 亚洲欧美成人一区二区三区| 亚洲国产色一区| 午夜欧美电影在线观看| 亚洲影音一区| 久久久久9999亚洲精品| 欧美一级免费视频| 久久午夜av| 久久久精品国产99久久精品芒果| 欧美日韩免费观看一区| 久久国内精品视频| 国产精品一区免费在线观看| 99re热这里只有精品免费视频| 亚洲精品国产拍免费91在线| 亚洲一级片在线看| 一区二区三区四区五区视频| 亚洲欧美一区在线| 中文国产一区| 欧美日韩免费观看一区二区三区| 亚洲人精品午夜在线观看| 亚洲另类黄色| 欧美3dxxxxhd| 亚洲国产精品久久人人爱蜜臀| 亚洲国产清纯| 美日韩精品视频| 欧美高清在线一区| 亚洲国产精品精华液2区45| 久久久国产成人精品| 久久免费精品日本久久中文字幕| 欧美午夜激情在线| 亚洲深夜影院| 欧美在线播放| 狠狠色狠狠色综合日日tαg | 久久亚洲春色中文字幕| 国产一区在线观看视频| 一区二区三区久久精品| 亚洲日本久久| 欧美日韩一区二区三区在线看| 亚洲精品日韩激情在线电影 | 国产精品五月天| 西西裸体人体做爰大胆久久久| 久久精品国产第一区二区三区| 国产日韩欧美在线| 久久久久9999亚洲精品| 欧美成人高清视频| 99热在这里有精品免费| 欧美色欧美亚洲另类二区| 亚洲天堂av综合网| 久久九九热re6这里有精品 | 亚洲免费成人av电影| 小黄鸭精品密入口导航| 国产在线精品自拍| 欧美jizzhd精品欧美巨大免费| 99热精品在线观看| 久久久久久电影| 国产午夜精品全部视频播放| 免费观看一级特黄欧美大片| 日韩一区二区精品葵司在线| 欧美亚洲色图校园春色| 亚洲高清不卡| 国产精品免费网站在线观看| 久久精品国产99国产精品| 亚洲国产欧美国产综合一区| 午夜亚洲性色福利视频| 亚洲国产高清自拍| 国产精品裸体一区二区三区| 久久久久久高潮国产精品视| 亚洲精品亚洲人成人网| 久久夜色精品国产亚洲aⅴ| 99re国产精品| 黄色欧美日韩| 欧美色视频在线| 久久综合精品国产一区二区三区| 99视频一区| 亚洲第一福利视频| 久久久久久亚洲精品杨幂换脸| 一区二区久久久久久| 激情av一区| 国产精品久久久久久久午夜片| 欧美一区二区精品| 在线一区亚洲| 亚洲欧洲在线播放| 欧美国产国产综合| 亚洲一卡二卡三卡四卡五卡| 日韩视频免费观看| 亚洲全黄一级网站| 一区二区三区我不卡| 国产乱人伦精品一区二区| 欧美另类极品videosbest最新版本| 欧美综合二区| 翔田千里一区二区| 亚洲小说春色综合另类电影| 亚洲精品一线二线三线无人区| 欧美激情一区二区三区不卡| 久久综合久久综合这里只有精品| 亚洲欧美成人一区二区在线电影 | 欧美精品在线网站| 欧美成年人视频网站| 久久久久久久久久久久久女国产乱| 99re6热在线精品视频播放速度| 亚洲精选在线| 日韩性生活视频| 亚洲理伦在线| 亚洲毛片在线观看.| 日韩视频中文| 一区二区国产在线观看| 一区二区三区国产精品| 亚洲最新在线| 亚洲自拍电影| 欧美一区二区| 久久米奇亚洲| 欧美日韩三区四区| 国产精品国产三级国产专播品爱网 | 亚洲乱码国产乱码精品精可以看 | 99国内精品| 亚洲欧美日韩国产一区| 亚洲欧美影音先锋| 久久久久国产精品厨房| 欧美成人久久| 欧美午夜精品| 国产一区二区三区在线观看精品| 狠狠色狠狠色综合日日91app| 国产精品免费网站在线观看| 国产精品一区二区三区乱码 | 宅男噜噜噜66一区二区| 亚洲综合电影一区二区三区| 久久成年人视频| 快she精品国产999| 欧美日韩在线观看一区二区三区 | 免费在线一区二区| 久久大逼视频| 午夜亚洲福利| 欧美99久久| 一区二区毛片| 久久99伊人| 欧美日韩精品一区二区| 国产女主播一区二区| 激情小说亚洲一区| 亚洲图片自拍偷拍| 久久综合99re88久久爱| 免费亚洲一区二区| 亚洲欧美国产视频| 男女精品视频| 国产日韩精品一区二区浪潮av| 亚洲国产精品ⅴa在线观看| 亚洲免费网站|