C bitwise negation question

following code:

signed char sc = ~0xFC;
unsigned char uc = ~0xFC;

during compilation the following warnings are given:

integer conversion resulted in truncation
integer conversion resulted in truncation
  • why am I getting these warnings.
  • how can i write my code so that i don't get these warnings (without using #pragmas)

thank,

I am using the IAR compiler for 8051,

Do you get similar warnings when compiling using other compilers?

+3
source share
2 answers

In C, arithmetic is performed at least by size int. This means that it ~0xFCwill return int. Moreover, it is a value 0xFF03that is out of range char(signed or not).

, .

signed char sc = ~0xFC & 0xFF;

, , char (gcc & 0xFF). :

signed char sc = (signed char)~0xFC;
// also possible:
// signed char sc = ~(signed char)0xFC;

, ,

signed char sc = 3;
+2

int, , 0xFC.., , , 1 :

~((char) 0xFC)

0xFC 0x000000FC 32- , , 0xFFFFFF03, , char, 3 .

+6

Source: https://habr.com/ru/post/1762889/


All Articles