将8位值与16位值进行比较 [英] compare 8bit value against 16bit value

查看:127
本文介绍了将8位值与16位值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将8位值与16位值进行比较会发生什么情况。

I was wondering what happens when an 8bit value is compared against a 16bit value.

我将尝试通过一个代码示例来解释问题:

I'll try to explain the problem by a code example:

bool result;
unsigned char a_8bit = 0xcd;
unsigned short b_16bit = 0xabcd;
result = a_8bit < b_16bit;

可能的结果可能是:


  • a_8bit隐式转换为unsigned short,并与b_16bit进行比较,作为16bit值。结果为真

  • b_16bit被隐式转换为无符号字符,并与a_8bit作为8bit值进行比较。结果为假

是否有人知道编译器将如何处理这段代码?当然,我可以尝试一下,但是不同的编译器对此代码有不同的解释吗?

Does anybody has a clue what the compiler will do with this piece of code? Sure, i can try it out, but are there different interpretations by different compilers of this code?

推荐答案


1 整数转换等级(4.13)小于int等级的bool,char16_t,char32_t或wchar_t以外的整数类型的prvalue可以转换为int类型的prvalue int可以表示源类型的所有值;否则,可以将源prvalue转换为unsigned int类型的prvalue。 [§4.5]

1 A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int. [§ 4.5]

因此,编译器可以将它们都升级为 unsigned int 然后进行比较。

So, compiler can promote both of them to unsigned int and then do the comparison.

这篇关于将8位值与16位值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆