Return value of bitRead() function

1. According to Arduino document, the bitRead() function returns 0 or 1. Which one of the following is more correct?

byte y = 0b01011011;
int n = bitRead(y, 1);

Or

byte y = 0b01011011;
bool n = bitRead(y, 1);

2. Does 1/0 stand for HIGH/LOW or true/false or integer?

  • My understanding is we get 1 or 0

  • I use this when need to do digitalWrite.

byte y = 0b01011011;
int n = bitRead(y, 1);

digitalWight(MyPin, bitRead(y, 1) == 1 ? HIGH : LOW);

I understand that you support:
bool n = bitRead(y, 1);

I would write:

byte y = 0b01011011;
bool n = bitRead(y, 1);

digitalWrite(MyPin, n);

Fixed the foobarred spelling, sorry.

I would say equally correct.

In C, zero is false and any other value is true, so that implicit conversion should be safe.

Either true/false or integer is safe.

But HIGH/LOW is not quite so simple. I don't know if Arduino has guaranteed that HIGH will always and forever be 1 and LOW will always and forever be 0. I think they would be absolutely crazy to change that, but...

This is bitRead

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

Based on that my take is that it returns a form of integer (byte, int).

I believe (on AVRs) bools are stored in a uint8_t (byte), don't know about 32 bit processors.

I believe boolean functions only see the LSB (least significant bit) of the addressed variable?

  • Hum, :thinking:
    Are you suggesting B0000010 is false ?
byte n = B0000010;

if( n == false)
{
. . .
}

Say:
value = 00101111
bit = 4

Reurn vlaue = (((value) >> (bit)) & 0x01)
===> Return value = 00000010 & 00000001 = 00000000 (byte)

Arduino document says that the return value is: 0 or 1

If the variable type is bool, yes.

I would write as follows as my intention is to do an arithmetic operation.

byte n = B0000010;
if( n == 0x02)
{
. . .
}

It is not equal to true.

void setup() {
  Serial.begin(115200);
  Serial.println("Hi Mom!\n");

  int x = 42;
  bool y = true;

  if (x) Serial.println("x (42) is true");
  if (y) Serial.println("y (true) is true");

  if (x == y) Serial.println("this does not print");
}

void loop() {
}

It makes comparing logical flags a little tricky? So maybe

bool n = bitRead(y, 1) != 0;

You can also booleanize a variable with !!, but it's not seen so much in the wild. At least I don't see it very often:

bool n = !!bitRead(y, 1);

a7

Oh well, another belief down the ol' porcelain convenience. :grimacing:

This depends on the type of y and the implicit type promotion rules. In your case, an int is returned.

The following snippet can be used to explore what happens in different cases (tested on a Mega 2560).

template <class T> String show(T const) {
  String s(__PRETTY_FUNCTION__);

  return s.substring(25, s.length() - 1);
}


void setup() {
  Serial.begin(9600);

  byte a {};
  auto r {bitRead(a, 1)};
  Serial.println(show(r));  // Prints "int".

  uint16_t b {};
  auto s {bitRead(b, 1)};
  Serial.println(show(s));  // Prints "unsigned int".
}

void loop() {}