本文介绍了将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?
推荐答案
因此,编译器可以将它们都升级为 unsigned int
然后进行比较。
So, compiler can promote both of them to unsigned int
and then do the comparison.
这篇关于将8位值与16位值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!