Would someone explain how bit(n) works to read a bit value.
#define bit(b) (1UL << (b))
It just shifts 1 left the appropriate amount. You still have to "and" it in to read a bit.
I guess I am missing a simple concept.
In the Arduino Cook Book (page 81).
"bit(bitPosition)
Returns the value of the given bit position: B(0) is 1, bit(1) is 2, bit(2) is 4 and
so on."
How would I use this in a program?
How would I use this in a program?
Use it to isolate single bits (like the sign bit), or to form masks from multiple bits.
char x;
...
signBit = x & bit(7);
LarryD:
I guess I am missing a simple concept.In the Arduino Cook Book (page 81).
"bit(bitPosition)
Returns the value of the given bit position: B(0) is 1, bit(1) is 2, bit(2) is 4 and
so on."
How would I use this in a program?
I think that description is a bit misleading. What it actually returns is a value that has the specified bit set (1) and the other bits unset (0).
Thanks.
char x;
...
signBit = x & bit(7);
Yes. using it this way makes sense now.
Returns the value of the given bit position
Is still odd though.
It can be useful for creating a mask to get or set the value of a bit flag. I'd probably use bitRead() and bitWrite() myself (and maybe bitSet() and bitClear() depending on the intent of the segment of code), but someone else may choose a different technique.