0

I used Google search to convert numbers from one numeral system to another.

  • 10 to binary is 0b1010;
  • 10 to hexadecimal is 0xA.

What's the meaning of 'b' and 'x', accordingly? I think these are abbrevations from binary and hexadecimal. I understand the way converting works. I thought 10 to binary is 1010 and A to hexadecimal. Maybe some scientific notation or something?

Christophe
  • 81,699
floreapaun
  • 31
  • 4

3 Answers3

3

If you see 1010 written without any qualification, you don't know if that's:

  • Decimal 10 written in base 2 (binary)
  • Decimal 30 written in base 3 (ternary)
  • Decimal 68 written in base 4 (quarternary)
  • [...]
  • Decimal 1010 written in base 10 (decimal!)
  • Decimal 1452 written in base 11
  • [...]
  • Decimal 4112 written in base 16 (hexadecimal)
  • [...]
  • Decimal 1000100 written in base 100

or any one of an infinite number of other possibilities for which base its written in. 0b and 0x are just how programmers sometimes indicate a number is written in binary or hexadecimal to avoid this confusion.

2

The 0 prefix is a common usage in programming languages to tell that a number will follow and not a symbol. Then, b means binary and x means hexadecimal.

In some programming languages, the leading 0 prefix followed by decimal digits will be understood as an octal number (digits should be between 0 and 7). Demo

More infos about the origin of this notation here.

Christophe
  • 81,699
0

As Christophe correctly points out, "a leading zero" – which is a thing never used in ordinary "human" notation of a number – is very often used as an indication.

In my experience, if the next character is a letter, it indicates that the remaining characters are binary or hexadecimal. *("Base 2" or "Base 16.") And, if it is a digit, the entire value is "octal" – base 8.

If "binary," each digit represents one bit. If "hexadecimal," each digit/letter represents four. If "octal," three.