-->
Page 1 of 1

Why make a bitshift operation on a fixed value ?

PostPosted: Fri Jan 04, 2019 11:07 pm
by cgiles
Hello,

I'm tinkering with some code, looking how it works in libraries, it is a library for dmx, the light communication protocol, a serial communication, over rs 485 connection.

I often see this kind of code :

Code: Select allif ((U0IS & (1 << UIBD)))


U0IS is the interrupter status, and UIBD a kind of interrupter.

I don't understand why the 1 is at left the variable at right, can someone explains me why ?

Thanks, and happy new year :)

Re: Why make a bitshift operation on a fixed value ?

PostPosted: Mon Jan 07, 2019 12:56 am
by Pablo2048
Hi,
it depends on how the bits of the status register are described - if they are described in form of mask (for example
Code: Select all#define UIBO 0x08
) you can do direct check
Code: Select allU0IS & UIBO
, but if they are described as bit numbers (for example
Code: Select all#define UIBO 3
) you have to build bit mask by your own so there is this
Code: Select all(1 << UIBO)
. This math is done only once at compile time because the compiler optimize constants math expressions.