整形提升(C语言基础)续

Conversion rules are more complicated when unsigned operands are involved. The problem is that comparisons between signed and unsigned values are machine-dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits and long is 32 bits. Then -1L < 1U, because 1U, which is an unsigned int, is promoted to a signed long. But -1L > 1UL because -1L is promoted to unsigned long and thus appears to be a large positive number.

I tried the code below in two different scenarios:

  1. compiled on an x86 64bits platform and executed. Where sizeof(-1L) -> 8byte and sizeof(1U) -> 4 bytes
  2. compiled on an x86 32bits platform and executed. Where sizeof(-1L) -> 4byte and sizeof(1U) -> 4 bytes

The code:

int main() {
 if(-1L > 1U)
    printf("true");
    else
    printf("false");
    return 0;
}

The results:

  1. x86 64bits: false
  2. x86 32bits: true

lets explain the rank system first

6.3.1 Arithmetic operand(c99 standard)

A) The rank of a signed integer type shall be greater than the rank of any signed integer
type with less precision(more bytes higher precision higher rank)

B) The rank of long long int shall be greater than the rank of long int, which shall be
greater than the rank of int, which shall be greater than the rank of short int, which
shall be greater than the rank of signed char.

C) The rank of any unsigned integer type shall equal the rank of the corresponding signed
integer type, if any.
(in other words if your system unsigned int is 32bits and your int is 32bits then the
ranks of these are the same.)

the above explains the rank.

now coming to arithmetic conversions.

6.3.1.8 Usual arithmetic conversions (c99 standard)

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.(similar to 1)

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.

2) compiled on an x86 32bits platform and executed. Where sizeof(-1L) -> 4byte and sizeof(1U) -> 4 bytes

in your case look at statement 3 & C. the unsigned value(4bytes) has rank equal to the signed value(4btyes) therefore the singed value is converted to an unsigned value, when this happens the, the sign bit makes this look like a extremely large value. -1L > 1U therefore is true

1) compiled on an x86 64bits platform and executed. Where sizeof(-1L) -> 8byte and sizeof(1U) -> 4 bytes

in this case, the unsigned value rank is less than the rank of the singed value. look at 4). the signed integer(8bytes) can represent any 4byte unsigned value. therefore the unsigned 4byte value is converted to a signed value.(this will preserve the sign bit, sign bit is 0)

therefore -1L > 1U is false

整形提升(C语言基础)续,布布扣,bubuko.com

时间: 2024-10-22 00:13:33

整形提升(C语言基础)续的相关文章

整形提升(c语言基础)

When you use an arithmetic operator, the operands go through two conversions. Integer promotions: If int can represent all values of the type, then the operand is promoted to int. This applies to both short and unsigned short on most platforms. The c

2.4.5-Java语言基础(类型提升&amp;强制转换)

自动类型转换(也叫隐式类型转换) 强制类型转换(也叫显式类型转换) 类型转换的原理 什么时候要用强制类型转换? 表达式的 数据类型自动提升 ?  所有的byte型.short型和char的值将被提升到int型. ?  如果一个操作数是long型,计算结果就是long型: ?  如果一个操作数是float型,计算结果就是float型: ?  如果一个操作数是double型,计算结果就是double型. 分析 ?  System.out.println('a') 与System.out.printl

C语言的整形提升

1.算术转换 许多运算符都会引发转换,以类似的方式产生结果类型,这个模式称为“寻常算术转换”. 首先,任何类型为char或short的操作数被转换为int,任何类型为float的操作数被转换为double.其次,如果其中一个操作数类型为double,那么另一个操作数被转换为double,计算结果类型也是double.再次,如果其中一个操作数类型为long,那么另一个操作数被转换为long,计算结果类型也为long.或者,其中一个操作数的类型数unsigned,那么另一个操作数被转换为unsign

Object Pascal 语法之语言基础(四)

1.8 过程与函数 过程与函数是实现一定功能的语句块,是程序中的特定功能单元.可以在程序的其他地方被调用,也可以进行递归调用.过程与函数的区别在于过程没有返回值,而函数有返回值. 1.过程与函数的定义过程与函数的定义包括过程原型或函数原型.过程体或函数体的定义.过程定义的形式如下: procedure ProcedureName(ParameterList); directives; var LocalDeclarations; begin statements end; ProcedureNa

02 java语言基础

常量:字面值常量(字符串,字符,整数,小数,布尔,null),自定义常量,''这个不是字符常量,""这个是字符串常量 进制: 02.01_Java语言基础(常量的概述和使用)(掌握) A:什么是常量 在程序执行的过程中其值不可以发生改变 B:Java中常量的分类 字面值常量 自定义常量(面向对象部分讲) C:字面值常量的分类 字符串常量 用双引号括起来的内容 整数常量 所有整数 小数常量 所有小数 字符常量 用单引号括起来的内容,里面只能放单个数字,单个字母或单个符号 布尔常量 较为特

C#语言基础

第一部分 了解C# C#是微软公司在2000年7月发布的一种全新且简单.安全.面向对象的程序设计语言,是专门为.NET的应用而开发的.体现了当今最新的程序设计技术的功能和精华..NET框架为C#提供了一个强大的.易用的.逻辑结构一致的设计环境.其特点: 语言简洁 保留了C++的强大功能: 快速应用开发功能: 语言的的自由性: 强大的Web服务器控件: 支持跨平台: 与XML相融合: 第二部分 C#语言基础: 一.C#项目的组成结构: 1.项目后缀 .config——配置文件(存放配置参数文件)

03 java语言基础逻辑运算符

03.01_Java语言基础(逻辑运算符的基本用法)(掌握) A:逻辑运算符有哪些 &,|,^,! &&,|| B:案例演示 逻辑运算符的基本用法 注意事项: a:逻辑运算符一般用于连接boolean类型的表达式或者值. b:表达式:就是用运算符把常量或者变量连接起来的符合java语法的式子. 算术表达式:a + b 比较表达式:a == b(条件表达式) C:结论: &逻辑与:有false则false. |逻辑或:有true则true. ^逻辑异或:相同为false,不同

语言基础:C#输入输出与数据类型及其转换

今天学习了C#的定义及特点,Visual Studio.Net的集成开发环境和C#语言基础. C#语言基础资料——输入输出与数据类型及其转换 函数的四要素:名称,输入,输出,加工 输出 Console.Write("这是我的第一个程序");//不换行,(不要漏掉:)(所有的符号全都用英文)(方法,也就是函数后面必须跟括号) Console.WriteLine("第二句");//输出这句后会换行 “这是我的第一个程序”后不换行,紧跟“第二句”,“第二句”换行,光标进入

C语言基础(转载自大海笔记)

# C语言基础2015年03月26日10:04:411.    语言排行榜C——java——objective-C2.    进制:进制:进位机制.用普通的话讲,应该为人为的定义一种度量来标识一样东西.计算机常用的进制有:十进制.二进制.八进制和十六进制.?    十进制:0-9(十个指头)(进制表示基数:10)?    二进制:0,1(基数为2)?    八进制:0-7(基数为8)?    十六进制:0-9,A-F(基数为16)    可以有很多进制,比如分钟为60进制等等.3.    位权为