[storage of data in memory] Introduction to original code, inverse code, complement and size end

#C language

preface

This article introduces how to save data in memory and some simple uses

1, Original code, inverse code and complement code

  • Data is stored in binary form in memory.
  • There are three binary representations of integers in computers, i.e. original code, inverse code and complement code.
  • The three representation methods include sign bit and value bit. The sign bit is 0 for "positive" and 1 for "negative"

The original, inverse and complement of positive numbers are the same.

int a = 10;
//4 bytes = 32 bits
//00000000 00000000 00000000 00001010 original code
//00000000 00000000 00000000 00001010 inverse code
//00000000 00000000 00000000 00001010 complement

For negative numbers, the complement is obtained by "adding one to negative", and the complement is stored in memory.

int b = -10;
//10000000 00000000 00000000 00001010 original code
//11111111 11111111 11111111 11110101 complement
//11111111 11111111 11111111 11110110 inverse code
  1. Original code
    The original code can be obtained by directly translating the numerical value into binary in the form of positive and negative numbers.
  2. Inverse code
    The sign bit of the original code is unchanged, and the other bits are inverted in order to obtain the inverse code.
  3. Complement
    The inverse code + 1 gives the complement.

The CPU of the computer only has an adder and can only perform addition, for example, 1-1 = 1 + (- 1).
The data is calculated not by the original code but by the complement.

  • The original code can be converted into a complement by "adding one to the reverse";
  • The complement can be converted into the original code by "subtracting one to get the inverse" or "subtracting one to add one"

2, Number of original code, inverse code, complement and sign (no sign)

	char a = -1;
    signed char b = -1;
    unsigned char c = -1;
    printf("a=%d,b=%d,c=%d", a, b, c);

What is the output result above?

Why is the output result of unsigned char c 255?

    char a = -1; //(- 1 truncated and stored in a)
    //10000000 00000000 00000000 00000001 original code
    //11111111 11111111 11111111 11111110 inverse code
    //11111111 11111111 11111111 11111111 complement
    //11111111 (complement stored in a)
    signed char b = -1;
    //10000000 00000000 00000000 00000001 original code
    //11111111 (complement stored in B)
    unsigned char c = -1;
    //10000000 00000000 00000000 00000001 original code
    //11111111 (complement stored in C)
    printf("a=%d,b=%d,c=%d", a, b, c);

As can be seen from the above figure, since the char type can only store one byte (8 bits), the data is stored in the variable after truncation.

  1. A is a char type, and to print in the form of signed% d, it needs to be shaped and promoted;
  2. And char represents a sign, so it should be promoted according to the sign bit. It can be seen that the sign bit is 1, so all the high bits are filled with 1;
	a
    11111111 11111111 111111111 11111111(Complement) (After shaping and lifting) 
    11111111 11111111 111111111 11111110 (Inverse code)
    10000000 00000000 000000000 00000001 (Original code)

signed char b is the same as char

For unsigned char c

  1. c is an unsigned char type, and to print in the form of signed% d, it needs to be shaped and promoted;
  2. And unsigned represents unsigned, so the high bit is directly filled with 0 when lifting;
	c
    00000000 00000000 000000000 11111111 (After shaping and lifting) 

Since the high bit after the lifting is 0, it is a positive number, and the original code, inverse code and complement of the positive number are the same, so the output result of c is 255;

Summary: the signed number is filled with 1 or 0 according to the sign bit during the shaping and lifting, and the unsigned number is directly filled with 0 during the shaping and lifting;

3, Value range of signed number and unsigned number

We list the possible values of all complements of char type signed and unsigned numbers

It can be seen from the figure that there is a value range for the signed number and unsigned number of char type;

We can even find that it is a samsara;

4, Introduction to large and small end

We set a variable here

int a = 0x12345678;

We find that its contents are stored in memory in reverse order. Why?

1. What is big end and small end

Big end (storage) mode: refers to that the low bit of data is stored in the high address of memory, while the high bit of data is stored in the low address of memory
Medium;
Small end (storage) mode: refers to that the low bit of data is stored in the low address of memory, while the high bit of data is stored in the high address of memory.

It should be noted that the storage of large end and small end is in reverse order in bytes instead of the contents in bytes. As shown in the figure above, "12" is not inverted into "21"

summary

The above is what I want to talk about today. This article will revise the deficiencies in the follow-up

Tags: C

Posted by tboneuls on Thu, 25 Aug 2022 15:11:53 +0930