Good evening,
Would there be anyone one of you smart guys could potentially break down the bitRead function in Arduino for me, how it works? I have done a bit of research on the web but still don't completely understand.

P.S. if you could dumb it down a lot that would be helpful!

Thanks

As defined in Arduino.h

``````#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
``````

A variable is just a serie of bits, for example a variable of type uint8_t is 8 bits so its value can be from 0 (0b00000000 in binary) to 255 (0b11111111 in binary).

You can read individual bits of any variable, by shifting and masking the result to keep only the bit(s) that you want. The bit order is from right to left, starting at 0.

For example you have a uint8_t value and you want to read the state of bit 5 only:

``````bit id:  76[b]5[/b]43210
value: 0b11[b]1[/b]00011
``````

So you shift 5 bits to the right so bit 5 become bit 0

``````0b11[b]1[/b]00011 >> 5 = 0b0000011[b]1[/b]
``````

Then you mask the result with 0b00000001 to keep only the value of bit 0

``````0b0000011[b]1[/b] & 0b00000001 = 0b0000000[b]1[/b]
``````

bitRead do exactly this, as you can see in cattledog's post

In addition to above comments, you can see the example in this

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.