Skip to main content
Knowledgebase
Home
Renesas Electronics America - Knowledgebase

The result of bitwise inverting variables of the unsigned char type

Last Updated:04/17/2017

Question:

I am using the CC-RL RL78 compiler. When I compare the results of bitwise inversion of variables of the unsigned char type as follows, however, it does not operate as I expected.
(Example)
volatile unsigned char val0 = 0xdd;
volatile unsigned char val1 = 0x22;
volatile unsigned char res;

if ( val0 == ~val1B ){
  res = 1;
}

Shouldn’t the result of ( val0 == ~val1B ) be “true”?
 

Answer:

In CC-RL, variables of the unsigned char type are extended to the int type before the operation.
The result of bitwise inverting val1, 0x22, becomes 0xffdd, and the result of the conditional expression is “false” since the value differs from that of val0, i.e. 0xdd.
The values of single bytes can be compared by either of the two types of operation in the examples below.
Example 1: Use only the value of the lower-order byte from the bitwise inversion in the comparison.
if ( val0 == (~val1B & 0xff) ){
Example 2: Cast the result of the bitwise inversion.
if ( val0 == (unsigned char)~val1B ){
Suitable Products
e² studio
CS+ (formerly CubeSuite+)
C Compiler Package for RL78 Family