Products > Programming

c operator precedence error

<< < (3/4) > >>

snarkysparky:
That was the first line of a list of defines. Each shifting further to the right.

#define THE_FIRST                                 (_U_(0x1) << 0)
#define RET_WRD_1                                 (_U_(0x1) << 1)
#define RET_WRD_2                                 (_U_(0x1) << 2)
#define RET_WRD_3                                 (_U_(0x1) << 3)
#define RET_WRD_T1_U                              (_U_(0x1) << 4)
#define RET_WRD_T2_U                              (_U_(0x1) << 5)
#define RET_WRD_T3_U                              (_U_(0x1) << 6)
#define RET_WRD_T1_L                              (_U_(0x1) << 7)
#define RET_WRD_T2_L                              (_U_(0x1) << 8)
#define RET_WRD_T3_L                              (_U_(0x1) << 9)
#define RET_WRD_MAIN_STEP_ERROR                   (_U_(0x1) << 10)
#define RET_WRD_FEED_STEP_ERROR                   (_U_(0x1) << 11)
#define RET_WRD_UP_SOLENOID_ERROR                 (_U_(0x1) << 12)
#define RET_WRD_LW_SOLENOID_ERROR                 (_U_(0x1) << 13)
#define RET_WRD_CUTTER_MOTOR_ERROR                (_U_(0x1) << 14)
#define RET_WRD_CUTTER_FINISHED_OK                (_U_(0x1) << 15)


usage was like

if(StatusWord & RET_WRD_UP_SOLENOID_ERROR)DoSomething();
else
    DoSomethingElse();


Funny..  It worked with only the first two bit shifts of 0 and 1.  When i added the others it failed.

mfro:

--- Quote from: snarkysparky on January 20, 2022, 01:52:01 pm ---Funny..  It worked with only the first two bit shifts of 0 and 1.  When i added the others it failed.

--- End quote ---

Doesn't make sense. If the first two worked, the others would work as well.

Might be a matter of taste, but in my opinion, that coding style to me is more like "preprocessor bragging" ("hey, this is to show I know how the preprocessor's concatenation operator works") than something that would improve code quality or readability. The shifts are o.k. (as it makes immediately obvious which bit is meant), however.

This would do just as well without such unnecessary obfuscation:

--- Code: ---#define THE_FIRST                                 (1U << 0)
#define RET_WRD_1                                 (1U << 1)
#define RET_WRD_2                                 (1U << 2)
#define RET_WRD_3                                 (1U << 3)
.
.
.

--- End code ---

newbrain:
Still does not explain the issue, & has lower priority than <<.

I agree with golden_labels and TheCalligrapher:
What exactly did not work?
At compilation or at run time?
What where the effects (I think we all agree on the expected effect)?

In any case writing a #define like that without surrounding brackets is borderline criminal, as it relies heavily on operator priority rules.

On the pointlessness of the _U_ macro, Siwastaja said it all.

snarkysparky:
what was happening was that when one of the later tests was false and the code to clear the bit executed the whole word was being cleared...

I apologize for not clearly stating what the code was.

there were lines like this for each bit

if(UP_SOL_ERROR) RemIoDiscreteLocalCopy |= RET_WRD_UP_SOLENOID_ERROR;
   else
   RemIoDiscreteLocalCopy &= ~RET_WRD_UP_SOLENOID_ERROR;


The problem was happening in this part   ->   RemIoDiscreteLocalCopy &= ~RET_WRD_UP_SOLENOID_ERROR;      when    #define RET_WRD_LW_SOLENOID_ERROR        _U_(0x1) << 13

after  changing #define RET_WRD_LW_SOLENOID_ERROR                 (_U_(0x1) << 13)

it worked.




newbrain:
Now it makes sense, and confirm what I wrote:

--- Quote ---In any case writing a #define like that without surrounding brackets is borderline criminal, as it relies heavily on operator priority rules.
--- End quote ---

~ has, as already stated at the beginning of the thread, higher priority than <<.

So the the effect was:

--- Code: ---~1u << 13 = 0xFFFFFFFE << 13 = 0xFFFFC000
--- End code ---

Bits 0-13 are then lost.

EtA: the define for bit 0 indeed worked as <<0 is not doing anything, the one for bit 1 was clearing bit 0 too.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

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