ST意法半导体
直播中

李想

7年用户 147经验值
私信 关注
[问答]

8位数据进行逻辑运算出现警告

你好,
在STVD(+ Cosmic)C编译器设置中,我选择* Display Errors&警告*。
我写了简单的代码:

uint8_t a = 0x01;

uint8_t b = 0x10;
void test(uint8_t licz){

}
void main(void){
test(a& 0x01);

测试(a | b);

测试(a + b);

}

编译后有警告:
#warning cpstm8 main.c:122(9)截断赋值
#warning cpstm8 main.c:123(9)截断赋值
#warning cpstm8 main.c:124(10)截断赋值

为了避免警告,我需要使用强制转换,例如:
test((uint8_t)(a& 0x01));

(a + b)操作的结果不超过最大值8位长数据。
为什么会有警告?
所有数据都声明为8位长。
如果数据将被声明为整数16位长,那么就不需要使用强制转换。

以上来自于谷歌翻译


以下为原文




Hello,
In STVD (+Cosmic) C compiler settings I choose *Display Errors & Warnings*.
I wrote simple code:


uint8_t a=0x01;

uint8_t b=0x10;

void test (uint8_t licz) {

}

void main (void) {

    test (a & 0x01);

    test (a | b);

    test (a + b);

}

After compilation there are warnings:

#warning cpstm8 main.c:122(9) truncating assignment

#warning cpstm8 main.c:123(9) truncating assignment

#warning cpstm8 main.c:124(10) truncating assignment

To avoid warnings I need to use casts, for example:

test ((uint8_t)(a & 0x01));

The result of (a + b) operation do not exceed max value 8-bit long data.
Why are there warnings?
All data are declared as 8-bits long.
If the data will be declared as integer 16-bits long there’s no need to use casts.

回帖(3)

符伯峪

2019-2-21 16:18:56
>>(a + b)操作的结果不超过最大值8位长数据。
是的,在你的具体情况下,它不是针对那种情况或你的常数进行优化/警告,而是抱怨值变化且240 + 230不适合的一般情况。

以上来自于谷歌翻译


以下为原文




>>The result of (a + b) operation do not exceed max value 8-bit long data.
Yeah in your specific case, it is not optimizing/warning about that case or with your constants, it is complaining about the general case where the values change and 240 + 230 doesn't fit.
举报

李想

2019-2-21 16:35:25
我明白了,但为什么会有警告
测试(a | b);

test(a& 0x01);

这些表达式永远不会超过最大值8位长数据。
而且,如果将存在所有16位数据,并且例如值
uint16_t a = 0x0001;
 
 uint16_t b = 0x1000;相同的逻辑和算术运算不会产生任何警告。

以上来自于谷歌翻译


以下为原文




I understand that, but why are there warnings in case of

test (a | b);
and

test (a & 0x01);
?
These expressions will never exceed max value 8-bit long data.
Moreover, if there will be all 16-bit data, and value for example
uint16_t a=0x0001;

uint16_t b=0x1000;The same logic and arithmetic operations don’t generate any warnings.
举报

谭齐慧

2019-2-21 16:45:16
那是因为C语言需要对int进行大量的隐式转换:如果你写的话
 test((unsigned char)(a& 0x01));
 
 test((unsigned char)(a | b));警告将消失。
如果a和b已经是16位,则隐式转换为int(即stm8上的16位)不会改变任何内容,因此没有警告。
问候,
卢卡

以上来自于谷歌翻译


以下为原文




that's because the C language requires a lot of implicit conversions to int: if you write
    test ((unsigned char)(a & 0x01));

    test ((unsigned char)(a | b));the warnings will go away.
If a and b are already 16 bits the implicit conversion to int (that is 16 bits on the stm8) does not change anything, hence no warning.
Regards,
Luca
举报

更多回帖

发帖
×
20
完善资料,
赚取积分