Integer data store

int main(a)
{
    int a = 20;
    int b = - 10;
    return 0;
}
Copy the code

The first picture is how a is stored in memory, and the second picture is how B is stored in memory. How does a computer store it? By what rules is it stored?

Source code inverse code complement

Unsigned and signed integers for integer data in a computer. Unsigned integer: All binary bits are used to represent numeric values. Signed integer: The first binary bit represents the symbol and the following binary bits represent the value. 0 represents a positive number and 1 represents a negative number.

Unsigned integer: source, inverse, and complement are the same.

Signed integers: For positive numbers, source, inverse and complement are the same. For negative numbers, the source code, the inverse code and the complement code have their own ways of calculating.

Source code: normal conversion of decimal number to binary, positive number is 0, negative number is 1 set symbol bit, is the source code. Inverse code: the original code, except for the symbol bit, the rest of the inverse. Complement: Add 1 to the inverse.

int a = 20; //4 bytes of a source code: 00000000 00000000 0000000010100 A reverse code: 00000000 00000000000 0000000000010100 A complement: 00000000 00000000 0000010100 A's complement is converted to hexadecimal: 0X00000014 int B = -10; //4 bytes b source code: 1 0000000 00000000 00000000 00001010 B reverse code: 11111111 11111111 11111111 11110101 B complement: 11111111 11111111 11111111 11110110 B's complement code is converted to hexadecimal: 0XFFFFFFF6Copy the code

Conclusion: We can find that memory is the transformation form of the computer’s complement stored, so the value exists in the computer as a complement.

Why complement is used

Why complement? Computer only adder can add, complement can make addition and subtraction use addition to get results.

int sum = a+b; // 20-10 = 20 + (-10) = 10 // If the original code is used for operation a: 00000000 00000000 0000000000010100 B: 1 0000000 00000000 00000000 00001010 A + B source code: 1 0000000 00000000 0000000000011110 Converted to decimal: -30 Error A's complement: 00000000 00000000 0000010100 B's complement: 11111111 11111111 11111111 11110110 A + B's complement: 00000000 00000000 00000000 00001010 Converted to decimal: 10 CorrectCopy the code

Small big end end

Careful net friends will find a hexadecimal we converted out clearly is00 00 00 14But this is true in IDE presentations. The two are completely reversed, because data is stored in big-endian and small-endian modes.

Big-endian storage mode: The low bits of data are stored in the high bits of memory, and the high bits in the low bits of memory. Small-endian storage mode: The low bits of data are stored in the low address of memory, and the high bits are stored in the high address of memory.

Let’s take 00, 00, 14

// Write a piece of code that tells us whether the machine uses big-endian or small-endian
int check_sys(a)
{
    //a(0X00 00 00 01) is stored in memory.
    // if it is a small byte, it is 01, 00, 00, 00
    // Is the machine big or small?
    int a = 1;
    return* (char *)&a;
}
int main(a)
{
    int ret = check_sys();
    if(ret)
    {
        printf("Little end \ n");
    }
    else
    {
        printf("The big end \ n");
    }
    return 0;
}
Copy the code

Character data store

int main(a)
{
    char a = - 1;
    signed char = - 1;
    unsigned char = - 1;
    printf("a = %d, b = %d, c = %d\n",a,b,c);
    return 0;
}
Copy the code

What is the final output of this program?

Where does this result come from?

char a = -1; -1 is an integer, so it should be four bytes. We write the binary form of -1:10000000 00000000 00000000 0000000-1: 1111111111 1111111111 1111111110-1 complement: Char char is a single byte with eight bits, and char is the default signed character type. Put -1 in a char variable. This variable can only hold one byte, so it is a complement of A: 11111111% d is a signed decimal integer. To print an integer, upgrade the char a to an int. The 8-bit char is preceded by 24 zeros/ones to int. Char (1) char (1) char (1) char (1) char (1) 11111111 11111111 11111111 11111111 11111111 11111110 THE original code of A 10000000 00000000 00000000 00000001 If the original code of A is converted to decimal, it is -1. 1. Signed char b = -1; Char a = -1; Unsigned char c = -1; Here is the same - 1 type into a character variable, so finally in c is 11111111 print into integer, so will be integer type char c promoted to int, unsigned char is unsigned, front fill 0 c complement: 00000000 00000000 00000000 11111111 C reverse code: 00000000 00000000 00000000 11111111 C source code: 00000000 00000000 00000000 11111111 Converted to decimal 255Copy the code
int main(a)
{
    char a = - 128.;
    printf("%u\n", a);
    return 0;
}
Copy the code

The output of the program is

char a = -128; -128's original code: 10000000 00000000 00000000 10000000 -128's inverse code: 11111111 11111111 11111111 0111111111-128's complement: 11111111 11111111 11111111 10000000 If you put -128 into character variable A, you can only store one byte of data, so the value in a is the complement of A: 10000000 %u is an unsigned decimal integer that promotes a to int. Char is signed, so binary is the complement 1 of a integer promoted: 11111111 11111111 11111111 10000000 And then calculate the original code according to the complement code, the conversion is the final result. At this time, we should pay attention to the program to carry out unsigned printing, and the unsigned original inverse complement code is the same, so 1111111111111111 11111111 10000000 is the original code, converting it to base 10 is the final result.Copy the code

See the range of unsigned and signed char as enumerated

Signed char Range: -128 to 127 10000000->-128 10000000 The original code obtained by subtracting one cannot be converted to -128, but 10000000 is directly specified by the computer as -128

Unsigned char Ranges from 0 to 255