Products > Programming

c operator precedence error

(1/4) > >>

snarkysparky:
I just spent 6 hours finding this one

var &= ~_U_(0x1) << 0     //WRONG!!!!!

var &= ~(_U_(0x1) << 0)    //  good job !!!

the intent was clearing the bit.

without the parenthesis what was the ~ operator doing ?



langwadt:
must be something else going on, << 0 doesn't do anything so it shouldn't matter if it is done after or before ~

Twoflower:
The ~ without brackets inverts the 1. How that will look like depends on the datatype of the var.


--- Code: (c) ---#include "stdio.h"
#include "stdint.h"

void main () {
    uint8_t u8Var;
    int8_t i8Var;

    u8Var = ~0x01;
    i8Var = ~0x01;

    printf("u8Var = %d\n", u8Var);
    printf("i8Var = %d\n", i8Var);
}

--- End code ---

Returns

--- Code: ---u8Var = 254
i8Var = -2

--- End code ---

But in both cases the binary pattern is the same. But langwadt is right, with the << 0 it doesn't matter.

SiliconWizard:
Maybe you can tell us...

* What the definition of '_U_()' is
* What the type of 'var' is
* What the compiler you use is

snarkysparky:
#define _U_(x)         x ## U            /**< C code: Unsigned integer literal constant value */

var is unsigned int on arm compiler so 32 bit

compiler is whatever comes with atmel studio.   GCC i think
thanks

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version