What Does "0b" And "0x" Stand For When Assigning Binary And Hex?

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 does "0b" and "0x" stand for when assigning binary and hex? Ask Question Asked 5 years, 3 months ago Modified 3 years, 9 months ago Viewed 50k times 23

When assigning a binary value and a hexadecimal value directly you can do it as follows (respectively):

uint8_t val1 = 0b10101; uint8_t val2 = 0xFF;

What does the 0b and 0x mean? Specifically the 0 at the front. Can you have other values instead of 0?

Also as another curious question, what other characters can go in the place of b and x? Is there one for octal as an example?

Share Improve this question Follow edited Feb 22, 2021 at 13:49 Neuron's user avatar Neuron 5,7535 gold badges43 silver badges62 bronze badges asked Aug 22, 2019 at 8:34 rrswa's user avatar rrswarrswa 1,0501 gold badge9 silver badges25 bronze badges 5
  • "Is there one for octal as an example?" Sure that's just a 0 prefix. – πάντα ῥεῖ Commented Aug 22, 2019 at 8:37
  • 1 Intuitively, "b" is for binary and "x" for hex. – Sweeper Commented Aug 22, 2019 at 8:38
  • these numbers are called "literals" and if you google you'll see all the literal syntax prefixes/suffixes. – Cory Nelson Commented Aug 22, 2019 at 8:38
  • 1 en.cppreference.com/w/cpp/language/integer_literal – πάντα ῥεῖ Commented Aug 22, 2019 at 8:43
  • 8 "x" is "exadecimal", because programming languages used to be manufactured exclusively in Manchester. – molbdnilo Commented Aug 22, 2019 at 8:46
Add a 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) 20

Any and all integer literals you can create are summarized in the C++ standard by the grammar production at [lex.icon]

integer-literal: binary-literal integer-suffixopt octal-literal integer-suffixopt decimal-literal integer-suffixopt hexadecimal-literal integer-suffixopt binary-literal: 0b binary-digit 0B binary-digit binary-literal 'opt binary-digit octal-literal: 0 octal-literal 'opt octal-digit decimal-literal: nonzero-digit decimal-literal 'opt digit hexadecimal-literal: hexadecimal-prefix hexadecimal-digit-sequence binary-digit: 0 1 octal-digit: one of 0 1 2 3 4 5 6 7 nonzero-digit: one of 1 2 3 4 5 6 7 8 9 hexadecimal-prefix: one of 0x 0X hexadecimal-digit-sequence: hexadecimal-digit hexadecimal-digit-sequence 'opt hexadecimal-digit hexadecimal-digit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

As we can deduce from the grammar, there are four types of integer literals:

  • Plain decimal, that must begin with a non-zero digit.
  • Octal, any number with a leading 0 (including a plain 0).
  • Binary, requiring the prefix 0b or 0B.
  • Hexadecimal, requiring the prefix 0x or 0X.

The leading 0 for octal numbers can be thought of as the "O" in "Octal". The other prefixes use a leading zero to mark the beginning of a number that should not be interpreted as decimal. "B" is intuitively for "binary", while "X" is for "hexadecimal".

Share Improve this answer Follow answered Aug 22, 2019 at 8:50 StoryTeller - Unslander Monica's user avatar StoryTeller - Unslander MonicaStoryTeller - Unslander Monica 170k22 gold badges393 silver badges472 bronze badges 4
  • 1 Given you've taken the "standard" route, you might want to mention things like \123. – Bathsheba Commented Aug 22, 2019 at 8:56
  • 3 @Bathsheba - '\123' is not strictly valid. One can only have octal and hex escape sequence. Also, since the issue at hand is integer literals, I feel personally that bringing character literals into scope can be confusing. Two monstrous grammar productions in a single answer feels like too much to me. – StoryTeller - Unslander Monica Commented Aug 22, 2019 at 9:03
  • Why 0x chosen, instead of 0h? And 0o is better than 0, isn't it? – DawnSong Commented Sep 6, 2021 at 14:39
  • 2 @DawnSong - Phonetically, the X stands out more than the H. Plus, the o seems redundant. In certain jargon, o and 0 are used interchangeably. 08:00 might be referred to as "oh eighthundred hours" for instance. – StoryTeller - Unslander Monica Commented Sep 6, 2021 at 14:45
Add a comment | 4

0b (or 0B) denotes a binary literal. C++ has allowed it since C++14. (It's not part of the C standard yet although some compilers allow it as an extension.) 0x (or 0X) is for hexadecimal.

0 can be used to denote an octal literal. (Interestingly 0 itself is an octal literal). Furthermore you use the escape sequence \ followed by digits to be read in octal: this applies only when defining const char[] literals using "" or char or multicharacter literals using ''. The '\0' notation that you often see to denote NUL when working with strings exploits that.

In the absence of a user defined literal suffix, any numeric literal starting with a non-zero is in denary.

There are rumblings in the C++ world to use 0o for an octal literal and perhaps even drop support for the leading zero version. Although that would be an hideous breaking change.

Share Improve this answer Follow edited Aug 22, 2019 at 9:05 answered Aug 22, 2019 at 8:40 Bathsheba's user avatar BathshebaBathsheba 234k35 gold badges369 silver badges496 bronze badges 2
  • @Angew: Indeed, I've spelt it out. You can use them in funnies such as multicharacter constants too, although that one could be for the pub quiz. – Bathsheba Commented Aug 22, 2019 at 9:03
  • In short, they're a type of character escape sequence, and not an integer literal. – Angew is no longer proud of SO Commented Aug 22, 2019 at 9:04
Add a comment | 2

What does the 0b and 0x mean?

They mean that the nuneric literal is respectively in binary and hexadecimal base.

Can you have other values instead of 0?

A numeric literal starting with a non zero digit will be a decimal literal.

Also as another curious question, what other characters can go in the place of "b" and "x"?

Besides b and x, any octal digit can go there in which case it is the most significant digit of an octal literal.

Share Improve this answer Follow edited Aug 22, 2019 at 8:50 answered Aug 22, 2019 at 8:45 eerorika's user avatar eerorikaeerorika 238k12 gold badges206 silver badges345 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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

Linked

0 typedef enum with binary definitions error (IAR 8.40) 6 How to interpret hexadecimal numbers like 0x0A? 4 HEX assignement in C 8 What are 0x01 and 0x80 representative of in C bitwise operations? 564 Why are hexadecimal numbers prefixed with 0x? 4 HEX and binary operations 161 What do numbers using 0x notation mean? 3 What is the point of writing your code with hexadecimal notation for numbers? 0 Confusion Converting Hexadecimal to Binary in C++ 0 What does 0x mean before a number? 2 What does 0b1 mean in C++?

Hot Network Questions

  • Correct place to store data required for custom addon
  • Non-reflexive use of laisser without a direct object in « The Stranger » ?
  • Diagonal analogue of symmetric functions
  • How would the Aboriginal Australians interact with and utilize a Sapient Octopus Species?
  • Is the Heter for music on Shabbat universal
  • Where can I find an up-to-date map of Stockholm Central Station that shows the platform layout?
  • Hearing the cry of a baby - abandoning practice for action?
  • Did anything ever use the -12V line on the original PC power supply?
  • Why is Ukraine's conscription age (still) so high (25)?
  • Can this strong directional blur at wide apertures still be explained by the usual arguments?
  • Can .zshrc be modified automatically by other programs, installers, etc.?
  • How to delete faces that intersect an edge with geometry nodes?
  • A fantasy movie with two races, "Big Ones" (=us) and smaller ones, about saving a newborn baby from a cruel queen
  • Implicit function theorem without manifolds (Steve Smale article)?
  • When to use the `AddEnumerateCounter` in enumitem?
  • What are some ways I can get an LM5176 buck-boost converter to operate down to 2.5V input voltage without a separate power supply?
  • Is it possible to use NAS hard drives in a desktop?
  • How do you write a page-centered abstract in a two column document
  • Apple falling from boat mast
  • What information does the admissions committee share with potential advisors / PIs? Disciplinary information?
  • The sum of multiple irrational numbers can be rational, even when they're not conjugates. Is this normal?
  • Reality check: energy source for power armour
  • Person of interest/under investigation living abroad - what can the UK police do?
  • Does ambigous license without a version refer to most recent? Is 'AGPL' currently equivalent to 'AGPL-3.0-only'?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-cpp

Từ khóa » C 0x 0b