cancel
Showing results for 
Search instead for 
Did you mean: 

Result of logic operation on 8-bits data need to casts

hgprojekt
Associate II
Posted on May 16, 2017 at 23:42

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 REPLIES 3
Posted on May 17, 2017 at 00:27

>>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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hgprojekt
Associate II
Posted on May 17, 2017 at 02:53

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.

Posted on May 18, 2017 at 09:37

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