What Is The Meaning Of 0b00 And 0b11 In C/C++? - Stack Overflow

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs What is the meaning of 0b00 and 0b11 in C/C++? [duplicate] Ask Question Asked 4 years, 10 months ago Modified 4 years, 10 months ago Viewed 13k times 0 This question already has answers here: Can I use a binary literal in C or C++? (25 answers) Closed 4 years ago.

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; } Share Improve this question Follow edited Jan 13, 2020 at 13:17 Lali asked Jan 13, 2020 at 13:08 Lali's user avatar LaliLali 131 gold badge1 silver badge4 bronze badges 6
  • 3 A binary number? – Evg Commented Jan 13, 2020 at 13:10
  • 2 Are you asking about the 0b syntax? Or Arduino specific part? – Yksisarvinen Commented Jan 13, 2020 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 Commented Jan 13, 2020 at 13:10
  • Please note that you are likely writing C++, not C, if you are working with Arduino. – walnut Commented Jan 13, 2020 at 13:10
  • Thank you for clarification walnut. And thank you CoryKramer I think I'm starting to get it. – Lali Commented Jan 13, 2020 at 13:17
| Show 1 more comment

3 Answers 3

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 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.

Share Improve this answer Follow edited Jan 13, 2020 at 13:52 Ted Lyngmo's user avatar Ted Lyngmo 115k7 gold badges79 silver badges128 bronze badges answered Jan 13, 2020 at 13:37 Vlad Stoian's user avatar Vlad StoianVlad Stoian 862 bronze badges Add a comment | 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

Share Improve this answer Follow answered Jan 13, 2020 at 13:29 datafiddler's user avatar datafiddlerdatafiddler 1,8353 gold badges19 silver badges30 bronze badges Add a comment | 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.

Share Improve this answer Follow answered Jan 13, 2020 at 13:14 user2261062user2261062 Add a comment |

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test [updated with phase 2]
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

252 Can I use a binary literal in C or C++? 10191 What is the '-->' operator in C/C++? 3932 What are the differences between a pointer variable and a reference variable? 4228 The Definitive C++ Book Guide and List 3719 What does the explicit keyword mean? 3133 What is the difference between #include <filename> and #include "filename"? 2412 What is the copy-and-swap idiom? 2478 What are the basic rules and idioms for operator overloading? 2547 What is The Rule of Three? 3374 How do I iterate over the words of a string? 3432 What's the problem with "using namespace std;"?

Hot Network Questions

  • What do border officials do with my passport when I tell them that I'm entering as the spouse of an EU national?
  • Rational divisors on Calabi–Yau threefolds
  • How to identify unsafe trees for climbing stand?
  • Why is Anarchism not considered fundamentally against the "democratic order" in Germany?
  • Group cohomology valued in a bimodule
  • Find the Smallest Data Type for a Number
  • How do I go about rebranding a fully deleted project that used to have a GNU General Public License v3.0 but is now fully inaccessible
  • Why is a pure copper cathode necessary in the electrolytic refining of copper?
  • How to design for API use cases that need different data from the same table?
  • Comparing the time duration to do a job
  • LTspice's bv unexpected output
  • Angular orientation of exact solution of the Hydrogen Schrödinger Equation
  • Why Adam and Eve were created naked?
  • Are there any examples of exponential algorithms that use a polynomial-time algorithm for a special case as a subroutine (exponentially many times)?
  • Why is Ukraine's conscription age (still) so high (25)?
  • Is it problematic to use percentages to describe a sample with less than 100 people?
  • What exactly is the cornerstone that Mark 12:10 speaks of?
  • How do I remove a hat from my horse?
  • T47 to BSA bottom bracket adapter - good idea?
  • If scent means a pleasant smell, why do we say "lovely scent" or "sweet scent"?
  • Is there a symbol for the Hyper key?
  • Why is it safe to soak an electric motor in isopropyl alcohol but not distilled water?
  • Reality check: energy source for power armour
  • Mega Man: Powered Up
more hot questions lang-cpp

Từ khóa » C 0b11