| |||||
Technical Support Support Resources
Product Information | ARM: WRONG RESULT WITH BINARY NOT AND UNSIGNED CHARInformation in this article applies to:
QUESTIONI 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? ANSWERThe 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 | ||||
| |||||