The storage of data in the computer and the introduction of two, eight and hexadecimal


foreword

This article will introduce you to various bases, including: binary, octal, and hexadecimal; how data will be stored in memory.

1. Binary:

Binary is actually a single digit that is less than 2 (for example: 01111 represents 15 in decimal), so how did it get it? Example:
Binary: 0+1 = 1 +1= 10 +1= 11 +1= 100 , where 10, 11 and 100 should be read separately as 1 0, 1 1, 1 0 0 instead of ten, eleven, one Hundreds;
decimal: 0+1 = 1 +1= 2 +1= 3 +1= 4 ;
the law has already appeared, in fact, the binary is the same as the decimal system, the decimal system is decimal one, and the binary is binary one.

2. Octal:

Octal is to meet octal one
. For example:
Octal: 0+3 = 3 +3= 6 +1= 7 +1= 10 ;
Decimal: 0+3 = 3 + 3 = 6 +1= 7 +1= 8 ;

3. Hexadecimal:

The hexadecimal system is also similar, in the case of hexadecimal one, but there is no single number in Arabic numerals representing ten, eleven... So in hexadecimal, use A to represent ten , B to represent eleven , C to represent twelve , D means thirteen , E means fourteen , F means fifteen Example:
hexadecimal: 7, 8, 9, A, B, C, D, E, F;
decimal: 7, 8, 9, 10, 11, 12, 13, 14, 15;

4.X base is converted to decimal:

Decimal: 1234
1 * 10 3 + 2 * 10 2 + 3 * 10 1 + 4 * 10 0 = 1234
Binary: 010011
1 * 2 4 + 1 * 2 1 + 1 * 2 0 = 19
Octal: 3741
3 * 8 3 + 7 * 8 2 + 4 * 8 1 + 1 * 8 0 = 2017
Hexadecimal: 2BE
2 * 16 2 + B * 16 1 + E * 16 0 = 702


1. The storage of integers in the computer:

Integer types include: char , int , long , short (char type is also considered an integer type because it is stored in ASCII code in memory). Integers are stored in two's complement
form in memory . Example: Unsigned int 10 Original code: 000000000000000000000000001010 Complement code: 111111111111111111111111111110110 Because it is an unsigned int type, it is 32 bits, and the data 10 is stored in memory as 111111111111111111 Stored in the form of 11111111111110110. Signed int 10 original code: 000000000000000000000000001010 complement: 01111111111111111111111111110110 signed int -10 original code: 1000000000000000000000000001010 Complement: 1111111111111111111111111111110110









Second, the storage of floating-point numbers in the data:

Floating-point types include: float, double
The storage of floating-point types is very different from integers, because floating-point numbers are stored in scientific notation.

#include <stdio.h>
 
int main()
{
    
    
    int n = 9;
    float* p = &n;
    printf("%d",*p);
    return 0;
}
输出结果是 0
#include <stdio.h>
 
int main()
{
    
    
    float n = 9;
    int* p = &n;
    printf("%d",*p);
    return 0;
}
输出结果是 1091567616

The storage calculation formula of floating-point numbers is:
(-1)^S * M * 2^E
For 64-bit machines
insert image description here
For 32-bit machines
Please add a picture description
S is the sign bit, E is the exponent, and the remaining M is a valid number.
Because floating-point numbers are stored in scientific notation, the range of numbers in M ​​is 0<M<10 , and because the computer memory is stored in binary, 0<M<2 .

IEEE 754 stipulates that when M is saved inside the computer, the first digit of this number is always 1 by default, so it can be discarded and only the latter part is saved. For example, when saving 1.01, only 01 is saved.

E is an unsigned integer, however, due to the exponent in scientific notation, negative numbers can appear, such as 2 -2 . Therefore, IEEE 754 stipulates that an intermediate number must be added to the real value of E when stored in memory. For 8-bit E, the intermediate number is 127; for 11-bit E, the intermediate number is 1023. For example, the E of 2^10 is 10, so when saving it as a 32-bit floating point number, it must be saved as 10+127=137, which is 10001001.

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/130993446