I would like an if statement of mine to check a binary value, and of course do something depending on its condition. However, I'm only interested in the first five bits. Currently, I've got the following - and the device ID I'm expecting is 01100110, however - the final three bits will sometimes change.
The following works, but it's not quite elegant - as if for example I received back B10000000, the code would print comms successful, when that is in this case an invalid identity byte.
How can I write the statement so it would read as following - where the last three bits marked as x don't matter, so the statement just checks that the identity register contains a byte that starts with 01100, and doesn't care about the last three bits?
It depends. 0bXXXXX is a gcc extension for binary constants.
BXXXX are macros defined in one of the files included by the arduino IDE.
So 0bxx is more portable to other gcc uses (for instance, Atmel Studio), while Bxxxx is more portable to random other compilers (assuming you copy over the correct .h file...)
I'm inclined to believe that the shift is less efficient, since a 3bit shift is at least three instructions. But the compiler might be smart enough to generate similar code for the case where constants are involved...
liudr:
Notice, there is not supposed to be a zero before the B.
The Arduino has some defined constants for binary values or various sizes such as B01100000. While this defines the commonly-used values such as all 8-bit and 16-bit numbers, it doesn't support all possible values.
The compiler used by the Arduino IDE already supports arbitrary binary literal values so that the Arduino's B01100000 is equivalent to 0b01100000. Unless you are planning to use a different compiler I can't see any advantage to using the Arduino defined constants over using the compiler-supported literal values.
Thanks for all the replies so far. That gives me some solutions to work with.
Regarding portability with other microcontroller platforms, that's not an issue for me. This is just a very small piece of code from a code spanning many, many pages that will eventually be dealt with in Atmel Studio - so I'll be changing all the Bxxxxxx to 0bxxxxxxxx to support that.
Whilst I understand the shifting solution, I"m not entirely sure of the solution that takes the following form; could anybody enlighten me as to what is going on? Especially with regards to the &?
'&' is the bitwise and operator. It compares each bit in each operand and outputs a result according to the following truth table:
Inputs Output
0 0 0
0 1 0
1 0 0
1 1 1
So '&' only outputs a 1 if both inputs are 1. This lets us create a "mask"
B11111000 & ident_reg_contents will return the value of the corresponding bit in ident_reg-contents when the mask is 1 and return 0 where the mask is 0. Since we do not care about the last 3 bits, setting them to zero will allow us to use a constant to compare.