# C / C + + Programming Notes: C language base details, binary, octal and hexadecimal!

Time：2020-7-22

The numbers we usually use are composed of 10 numbers from 0 to 9, such as 1, 9, 10, 297, 952, etc. a number can represent nine at most. If you want to represent 10, 11, 29, 100, etc., you need to combine multiple numbers.

For example, for the result of 5 + 8, if a number is not enough, it can only be represented by “carry”, and then “one digit” is equivalent to ten and two digits to twenty.

Because every decimal one (full decimal one), also because there are only 0-9, a total of 10 numbers, so called decimal (decimal). Decimal system is formed naturally in the development of human society. It conforms to people’s thinking habits. For example, human beings have ten fingers and ten toes. The decimal system is called carry system. In addition operation, every x into one (full x into one), while subtraction operation, borrow one when x, this is the x-base, this system also contains x digits, the base is X. Decimal system has 0 ~ 9, a total of 10 digits, the radix is 10, in addition and subtraction operation, every decimal one, borrow one when ten.

## Binary

We might as well expand our thinking. Since we can use 0 ~ 9 to represent numerical values, we can also use 0 and 1 to represent numerical values, which is binary. For example, the numbers 0, 1, 10, 111, 100, 1000001 are valid binary.

In the computer, data are stored in the form of binary, which is the basis of learning programming. In this section, we will first explain the concept of binary, and the next section will explain the storage of data in memory, so that we can apply what we have learned.

The idea of binary addition and subtraction is similar to that of decimal addition and subtraction

For decimal system, every decimal one is added and one is borrowed as ten when subtracting;

For binary system, add every two into one, subtraction operation borrow one when two.

The following two diagrams demonstrate in detail the operation process of binary addition and subtraction.

1) Binary addition: 1 + 0 = 1, 1 + 1 = 10, 11 + 10 = 101, 111 + 111 = 1110 2) Binary subtraction: 1-0 = 1, 10-1 = 1, 101-11 = 10, 1100-111 = 101 ## octal number system

In addition to binary, C also uses octal.

Octal has 0 ~ 7, a total of 8 digits, the cardinal number is 8, the addition operation every eight into one, subtraction operation borrow one when eight. For example, the numbers 0, 1, 5, 7, 14, 733, 67001, 25430 are all valid octal.

The following two figures demonstrate the operation process of octal addition and subtraction in detail.

1) Octal addition: 3 + 4 = 7, 5 + 6 = 13, 75 + 42 = 137, 2427 + 567 = 3216 2) Octal subtraction: 6-4 = 2, 52-27 = 23, 307-141 = 146, 7430-1451 = 5757 In addition to binary and octal, hexadecimal is often used, even more frequently than octal.

In hexadecimal system, a is used to represent 10, B is 11, C is 12, D is 13, e is 14, and F is 15. Therefore, there are 16 numbers from 0 to F, and the cardinal number is 16. In addition operation, every 16 will enter 1, and subtraction operation will borrow 1 when 16. For example, the digits 0, 1, 6, 9, a, D, F, 419, ea32, 80a3, bc00 are all valid hexadecimal.

Note that the letters in hexadecimal are not case sensitive, and ABCDEF can also write ABCDEF.

The following two figures demonstrate the operation process of hexadecimal addition and subtraction in detail.

1) Hexadecimal addition: 6 + 7 = D, 18 + Ba = D2, 595 + 792 = d27, 2f87 + f8a = 3f11 2) Hexadecimal subtraction: D-3 = a, 52-2f = 23, e07-141 = cc6, 7ca0-1cb1 = 5fef 