c,unsigned char 和 char 上的相等运算符
c,equality operator on unsigned char and char
为什么我比较int和unsigned int[=29=时得到"x==y" ]
那么,为什么我比较char和unsigned char[=时得到"a!=b" 29=],尽管它们确实具有相同的位模式“0xff”
在应用相等运算符时,是否考虑变量类型?
代码:
#include <stdio.h>
int main()
{
unsigned int x = 0xFFFFFFFF;
int y = 0xFFFFFFFF;
printf("unsigned int x = 0xFFFFFFFF;\n");
printf("int y = 0xFFFFFFFF;\n");
if (x < 0)
printf("x < 0\n");
else
printf("x > 0\n");
if (y < 0)
printf("y < 0\n");
else
printf("y > 0\n");
if(x==y)
printf("x==y\n\n");
///////////-- char --////////////////////////
unsigned char a = 0xFF;
char b = 0xFF;
printf("unsigned char a = 0xFF\n");
printf("char b = 0xFF\n");
if (a < 0)
printf("a < 0\n");
else
printf("a > 0\n");
if (b < 0)
printf("b < 0\n");
else
printf("b > 0\n");
if(a==b)
printf("a==b\n");
else
printf("a!=b\n");
}
输出:
unsigned int x = 0xFFFFFFFF;
int y = 0xFFFFFFFF;
x > 0
y < 0
x==y
unsigned char a = 0xFF
char b = 0xFF
a > 0
b < 0
a!=b
C 一般将整数值提升到int
s 进行运算。对于 unsigned char u = 0xFFu;
和 signed char s = 0xFF;
,在计算 u == s
时,s
是符号扩展的,而 u
不是,所以它被解释为 0xFF == -1
。
有符号整数使用最高有效位存储负数,所以在硬件级逻辑中,你问过:
IF (-1 != 255){
然后 // 总是运行
}
因为促销。
char 或 short 类型将在任何比较之前提升为 int。
所以
unsigned char a = 0xFF
将提升为 0x000000FF(255)
char b = 0xFF
将提升为 0xFFFFFFFF(-1)
它们不相等。
来自C11ISO/IEC9899:201x标准:
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.
促销应用于 int y = 0xFFFFFFFF
,然后与 unsigned int x = 0xFFFFFFFF
进行比较。将 int y
提升为 unsigned int
将保留值 0xFFFFFFFF,这会导致 x == y
.
另一方面:
If an int can represent all values of the original type (as
restricted by the width, for a bit-field), the value is
converted to an int ; otherwise, it is converted to an unsigned
int . These are called the integer promotions . All other
types are unchanged by the integer promotions. The integer
promotions preserve value including sign. As discussed
earlier, whether a ‘‘plain’’ char is treated as signed is
implementation-defined.
表示unsigned char a = 0xFF
和char b = 0xFF
在比较前都转换为signed int
。然而,转换 b
将导致符号扩展,这意味着 b
的值扩展为 0xFFFFFFFF == -1
导致 int a = 255
大于 int b = -1
.
循序渐进:忽略位模式并关注类型和值。
0xFF
是一个 整数常量 ,值为 255,类型为 int
。 (C11 §6.4.4.1 5)
unsigned char a = 0xFF
将 255 分配给 unsigned char
,它可以在您的平台上表示值 [0-255]。 a
获取 255 的值并键入 unsigned char
。 6.3.1.3 1
char b = 0xFF;
将 255 分配给 char
,在您的平台上可以表示值 [-128 - 127]。该值以 实现定义的 方式转换。
在 OP 的例子中,减去 256 并且 b
得到 -1 的值并键入 char
。 6.3.1.3 3
当这些与 0 进行比较时,值不会改变,但会提升为 int
。 §6.3.1.1 2
当然,-1 < 0, 255 > 0 和 -1 != 255。
为什么我比较int和unsigned int[=29=时得到"x==y" ]
那么,为什么我比较char和unsigned char[=时得到"a!=b" 29=],尽管它们确实具有相同的位模式“0xff”
在应用相等运算符时,是否考虑变量类型?
代码:
#include <stdio.h>
int main()
{
unsigned int x = 0xFFFFFFFF;
int y = 0xFFFFFFFF;
printf("unsigned int x = 0xFFFFFFFF;\n");
printf("int y = 0xFFFFFFFF;\n");
if (x < 0)
printf("x < 0\n");
else
printf("x > 0\n");
if (y < 0)
printf("y < 0\n");
else
printf("y > 0\n");
if(x==y)
printf("x==y\n\n");
///////////-- char --////////////////////////
unsigned char a = 0xFF;
char b = 0xFF;
printf("unsigned char a = 0xFF\n");
printf("char b = 0xFF\n");
if (a < 0)
printf("a < 0\n");
else
printf("a > 0\n");
if (b < 0)
printf("b < 0\n");
else
printf("b > 0\n");
if(a==b)
printf("a==b\n");
else
printf("a!=b\n");
}
输出:
unsigned int x = 0xFFFFFFFF;
int y = 0xFFFFFFFF;
x > 0
y < 0
x==y
unsigned char a = 0xFF
char b = 0xFF
a > 0
b < 0
a!=b
C 一般将整数值提升到int
s 进行运算。对于 unsigned char u = 0xFFu;
和 signed char s = 0xFF;
,在计算 u == s
时,s
是符号扩展的,而 u
不是,所以它被解释为 0xFF == -1
。
有符号整数使用最高有效位存储负数,所以在硬件级逻辑中,你问过:
IF (-1 != 255){ 然后 // 总是运行 }
因为促销。
char 或 short 类型将在任何比较之前提升为 int。
所以
unsigned char a = 0xFF
将提升为 0x000000FF(255)
char b = 0xFF
将提升为 0xFFFFFFFF(-1)
它们不相等。
来自C11ISO/IEC9899:201x标准:
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.
促销应用于 int y = 0xFFFFFFFF
,然后与 unsigned int x = 0xFFFFFFFF
进行比较。将 int y
提升为 unsigned int
将保留值 0xFFFFFFFF,这会导致 x == y
.
另一方面:
If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int ; otherwise, it is converted to an unsigned int . These are called the integer promotions . All other types are unchanged by the integer promotions. The integer promotions preserve value including sign. As discussed earlier, whether a ‘‘plain’’ char is treated as signed is implementation-defined.
表示unsigned char a = 0xFF
和char b = 0xFF
在比较前都转换为signed int
。然而,转换 b
将导致符号扩展,这意味着 b
的值扩展为 0xFFFFFFFF == -1
导致 int a = 255
大于 int b = -1
.
循序渐进:忽略位模式并关注类型和值。
0xFF
是一个 整数常量 ,值为 255,类型为 int
。 (C11 §6.4.4.1 5)
unsigned char a = 0xFF
将 255 分配给 unsigned char
,它可以在您的平台上表示值 [0-255]。 a
获取 255 的值并键入 unsigned char
。 6.3.1.3 1
char b = 0xFF;
将 255 分配给 char
,在您的平台上可以表示值 [-128 - 127]。该值以 实现定义的 方式转换。
在 OP 的例子中,减去 256 并且 b
得到 -1 的值并键入 char
。 6.3.1.3 3
当这些与 0 进行比较时,值不会改变,但会提升为 int
。 §6.3.1.1 2
当然,-1 < 0, 255 > 0 和 -1 != 255。