Keil Logo

ARM: WRONG RESULT WITH BINARY NOT AND UNSIGNED CHAR


Information in this article applies to:

  • RealView Compiler any Version
  • GNU Compiler any Version
  • CARM Compiler any Version

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

Last Reviewed: Tuesday, December 18, 2007


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.