In learning C language, digital circuit, microcomputer principle, FPGA and so on many courses, the first is about the base system and its transformation, learning so many times, we really understand the base system?

1. What is carry system (base system)

Base system, full name is carry counting system, is the same quantity of different ways of counting.

A sequence of numbers ascending from right to left, the same number in different positions, representing different sizes, depending on the base system.

In popular terms, the base system determines how many times to enter one.

2. Commonly used base system

The decimal system

  • Cardinality: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

The decimal system has a long history and I can’t really say why

binary

  • Cardinality: 0, 1

Binary, which has only two states, 0 and 1, can be represented as either with/without (magnetism) or high/low (voltage), and is widely used in the world of digital circuits, and thus in the world of computers.

hexadecimal

  • Cardinality: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

When using computers, hexadecimal was established between decimal and binary for ease of coding and understanding, i.e., four binary numbers are represented by one hexadecimal number.

octal

  • Cardinality: 0, 1, 2, 3, 4, 5, 6, 7

Octal has never been used before, so I don’t know why it was invented

3. Representation of different bases in the program

3.1. Assembly language

  • Decimal: suffix D (eg.1234d)
  • Binary: suffix B (eg. 1011B)
  • Hex: suffix H (eg.82ceh)
  • Octal: suffix O(eg. 7162O)

3.2. High-level languages

  • Decimal: Direct writing
  • Binary: cannot write, use hexadecimal instead
  • Hexadecimal: prefix 0X or 0X
  • Octal: prefix 0 (zero, not capital O)

4. Conversion between common bases (integers)

The decimal system binary hexadecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 a/A
11 1011 b/B
12 1100 c/C
13 1101 d/D
14 1110 e/E
15 1111 f/F

N base -> decimal

  • Unsigned: the cardinality of the position X the weight of the position, and then the sum of all positions.
  • Signed: the highest bit is the sign bit, and the rest of the bits operate according to the rules above.

Decimal -> n base

  • Mod n to zero, remainder in reverse order

5. Conversion between commonly used bases (floating point numbers)

Decimal -> binary floating – point number

  • Integer part: converted as a decimal integer (mod n to zero, remainder in reverse order)
  • Decimal part: multiply by 2 until the decimal part is zero, and then arrange the integers in positive order (if the decimal part is not zero, reserve enough decimal places as required, and round the last 0 to 1).

Binary floating point -> decimal decimal

  • All the bits are added by weight.