0


I have a small question I hope there is a simple answer there. When programming an Arduino in C/C++ the line "DDRB |= 0b00101000;" occurs. While I know DDRB is the Data Direction Register for port B and the meaning of the numbers after "0b00" (which are the slots 13 to 9), I still don't really know what "0b00" means.

In definitions I only read it means HIGH (while 0b11 means LOW) but what does that mean?

Full code:

#include <avr/io.h>
#include <util/delay.h>

int main (void) {

  float seconds = 0.5;
  int time = 1000 * seconds;

  DDRB |= 0b00101000;

  while (1) {

    PORTB |= 0b00001000; 
    _delay_ms(time);
    PORTB &= 0b11110111; 
    PORTB |= 0b00100000;
    _delay_ms(time); 
    PORTB &= 0b11011111; 
  }
  return 0;
}
Lali
  • 13
  • 1
  • 4
  • 3
    A [binary number](https://en.cppreference.com/w/cpp/language/integer_literal)? – Evg Jan 13 '20 at 13:10
  • 2
    Are you asking about the `0b` syntax? Or Arduino specific part? – Yksisarvinen Jan 13 '20 at 13:10
  • The b prefix specifies a binary literal, as opposed to a decimal literal. In this case `0b00101000 == 40`, but since you are using it to check bits, it is more readable to express in binary. – Cory Kramer Jan 13 '20 at 13:10
  • Please note that you are likely writing C++, not C, if you are working with Arduino. – walnut Jan 13 '20 at 13:10
  • Thank you for clarification walnut. And thank you CoryKramer I think I'm starting to get it. – Lali Jan 13 '20 at 13:17
  • Edit the question to clearly say whether you are asking (a) what is the meaning of `0b00101000` in the C language (as extended by the GNU compiler) or (b) what is the effect of ORing the bits 00101000 into the `DDRB` register or (c) something else. If you do not explain precisely what you are asking, your question will likely be closed. – Eric Postpischil Jan 13 '20 at 13:20

3 Answers3

7

0b means that a number in binary representation is expected.

For Data direction registers, setting the bits to 1 will make the respective lines outputs, and setting to 0 will make them inputs.

The DDRB |= 0b00101000 will do a binary OR operation between the current value of the bits in DDRB with the mask.

This will result in DDRB = 0b××1×1xxx, so that means DDRB will keep the value for lines 7 and 6. This operation basically sets lines 5 and 3 as Output and leaves the rest as they were.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

As you tag your question "Arduino", you might be interested that besides the standard c++ 0b... notation the IDE also provides all 8-bit combinations of binary numbers in B00101000 format, with and without leading zeros.

Usually, hex notation (0x28 in your example) is even easier readable, IMO

datafiddler
  • 1,755
  • 3
  • 17
  • 30
0

The line

DDRB |= 0b00101000

basically does a bitwise OR with the mask 0b00101000 and reassigns the results to DDRB.

0b indicates that whatever comes next should be interpreted as binary, so it's easier to see which bits you are masking.

The code is just setting the bits masked as 1 to HIGH and leaving the others unchanged.