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

Có thể bạn quan tâm

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...
Visit chat

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

  • Where on Earth do tides go out furthest?
  • Is Isaiah's suffering servant the prophet Jeremiah?
  • Trump's tariff plan
  • Perfect eden - Whence conflict?
  • Can you make 5 x 3 “magic” rectangles?
  • Why sand dunes appear dark in Sentinel-1 SAR Images but bright in optical images
  • The British used to (still?) classify their guns by weight in pounds rather than caliber. Was that a constant across shell types?
  • If scent means a pleasant smell, why do we say "lovely scent" or "sweet scent"?
  • Static vs dynamic certificate pinning
  • Why is it safe to soak an electric motor in isopropyl alcohol but not distilled water?
  • Deutsche Bahn Berlin: can I use a different departure station?
  • Why hot refers to being closer and cold refers to moving away in the hotter/colder game?
  • Will a laptop battery that stays connected to its charger be damaged?
  • Is there any advantage to using gifts to avoid estate tax?
  • Walks in Nice (Nizza)
  • Are there any examples of exponential algorithms that use a polynomial-time algorithm for a special case as a subroutine (exponentially many times)?
  • testing for a correlation between a real number and percentage accuracy
  • Polynomial.java - a Java class for dealing with polynomials with BigDecimal coefficients
  • Comedy/Sci-Fi movie about one of the last men on Earth living in a museum/zoo on display for humanoid robots
  • Calculate the sum of all the different real roots of the equation
  • How to say "Each one of the following" in German?
  • Skylab's Boom during EVA
  • How are the locations in the select your location screen in the debian installation categorized?
  • Wouldn't the ban of TikTok violate freedom of speech?
more hot questions lang-cpp

Từ khóa » C 0b00