加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – uint8_t的类型乘以布尔值是什么?

发布时间:2020-12-16 09:54:17 所属栏目:百科 来源:网络整理
导读:从 standard开始,我试图了解表达式最终会是哪种类型: bool myBool;[...]uint8_t(255) * (myBool); 我保证myBool会被转换为uint8_t(a.k.a.unsigned char),或者整个结果可能是int吗? 有用链接:bool to int conversion 解决方法 从您链接到的文档: 5 Expres
从 standard开始,我试图了解表达式最终会是哪种类型:

bool myBool;
[...]
uint8_t(255) * (myBool);

我保证myBool会被转换为uint8_t(a.k.a.unsigned char),或者整个结果可能是int吗?

有用链接:bool to int conversion

解决方法

从您链接到的文档:

5 Expressions

9 Many binary operators that expect operands of arithmetic or enumera-
tion type cause conversions and yield result types in a similar way.
The purpose is to yield a common type,which is also the type of the
result. This pattern is called the usual arithmetic conversions,
which are defined as follows:

–Otherwise,the integral promotions (conv.prom) shall be performed
on both operands.1)

4.5 Integral promotions

1 An rvalue of type char,signed char,unsigned char,short int,or
unsigned short int can be converted to an rvalue of type int if int
can represent all the values of the source type; otherwise,the source
rvalue can be converted to an rvalue of type unsigned int

4 An rvalue of type bool can be converted to an rvalue of type int,with
false becoming zero and true becoming one.

在您的情况下,LHS和RHS都将在算术运算之前被提升为int,结果将是int类型.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读