什么是uint8

编程入门 行业动态 更新时间:2024-10-25 08:16:31
什么是uint8_t乘以布尔值的类型?(What is the type of uint8_t multiplied by boolean value?)

从标准来看,我试图理解哪种类型的表达最终会是:

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

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

有用的链接: 布尔到int转换

From the standard, I was trying to understand which type the expression will end up to be:

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

Am I guaranteed that myBool will be casted to uint8_t (a.k.a. unsigned char), or the whole result might be int?

Useful link: bool to int conversion

最满意答案

从您链接到的文档:

5个表达式

9许多期望算术或枚举类型操作数的二元运算符以相似的方式导致转换和产生结果类型。 目的是产生一个共同的类型,这也是结果的类型。 这种模式被称为通常的算术转换,其定义如下:

...

- 否则,积分促销( conv.prom )应在两个操作数上执行.1

4.5积分促销

1如果int可以表示源类型的所有值,则可以将类型为char,signed char,unsigned char,short int或unsigned short int的右值转换为int类型的右值; 否则,源rvalue可以转换为unsigned int类型的右值

...

4 bool类型的右值可以转换为int类型的右值,其中false值为零,true为1。

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

From the document you linked to:

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)

and

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.

In your case, both the LHS and RHS will be promoted to int before the arithmetic operation and the resultant will be of type int.

更多推荐

本文发布于:2023-07-27 15:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!