ARM Technical Support Knowledge Articles

WRONG RESULT WITH BINARY NOT AND UNSIGNED CHAR

Applies to: ARM Development Tools

Answer


Information in this article applies to:


QUESTION

I am porting the following code from the Keil C51 Compiler.

volatile unsigned char uc1, uc2;

void test (void)   {
  uc1 = 0x69;
  uc2 = 0x96;

  if (uc1 != ~uc2)  {
    uc1 = 0;        // this line should not be executed
  }
}

It appears that the binary NOT operation (~) delivers incorrect results with an ARM compiler. What can be wrong?

ANSWER

The ARM architecture is a 32-bit CPU and all operations are performed with 32-bit numbers which conforms to the ANSI standard. The value of ~uc2 is therefore 0xFFFFFF69 and not just 0x69.

On C51 the operation is performed with 8-bit operations and therefore gives the result that you have expected. The solution to your problem is to use explicit cast operations:

  if ((unsigned char)uc1 != (unsigned char)~uc2)  {

MORE INFORMATION

SEE ALSO

Article last edited on: 2007-12-18 00:48:17

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential