<> Storage of data in integers
1. Source code , Inverse code , Complement
First, let's look at the original code , Inverse code , The concept of complement :
There are three ways to express the signed number in computer , That is the original code , Inverse and complement .
The three methods have two parts: sign bit and numerical bit , The sign bit is used 0 express “ just ”, use 1 express “ negative ”, However, the three representation methods of numerical bit are different .
Original code :
The binary can be translated into binary directly according to the form of positive and negative numbers .
Inverse code :
Keep the sign bit of the original code unchanged , The other bits can be obtained by negating them in turn .
Complement :
Inverse code + 1 Get the complement
be careful : The highest bit is the sign bit , The primitive of positive numbers , back , All complements are the same .
For plastic surgery : The data stored in memory is actually the complement code .
int main() { int a = 20; //00000000000000000000000000010100- Source code , Inverse code , Complement // Change to hexadecimal sequence
//0x000014 int b = -10; //10000000000000000000000000000000- Source code
//11111111111111111111111111110101- Inverse code //11111111111111111111111111110110- Complement
//fffffff6 return 0; }
a Memory for
b Memory for
We can see that the complement is stored in the memory , We will talk about the order in memory later .
<> Byte order
Introduction to big and small end :
Large end byte order storage pattern , The low byte of data is stored in the high address of memory , And the high level of the data , Stored in the low address of memory ;
Small end byte order storage pattern , The low byte of data is stored in the low address of memory , And the high level of the data ,, Stored in a high address in memory .
Exercises
Determine the byte order of the computer
int main() { int a = 1; char *p = (char*)&a; if (*p == 1) { printf(" Small end "); }
else { printf(" Big end "); } return 0;
Print the following numbers as integers
int main() { char a = -1; //1000000000000000000000000000001- Source code
//1111111111111111111111111111110- Inverse code //1111111111111111111111111111111- Complement
//11111111-a // It is also printed as an integer , integral promotion , Complement by sign 1 //1111111111111111111111111111111
//1111111111111111111111111111110 //1000000000000000000000000000001 //-1 signed
char b = -1; unsigned char c = -1; //11111111-c // Lifting of unsigned bit integers , High compensation 0
//0000000000000000000000011111111 printf("%d %d %d", a, b, c); return 0; }
be careful : The same is true for the source complement of an unsigned integer .
algebraic addition
int main() { int i = -20; unsigned int j = 10;
//10000000000000000000000000010100- Complement //11111111111111111111111111101011- Inverse code
//11111111111111111111111111101100- Source code //00000000000000000000000000001010
//11111111111111111111111111110110//i+j //11111111111111111111111111110101// Inverse code
//10000000000000000000000000001010 printf("%d", i + j); return 0; } int main() {
char a = -128; //1000000000000000000000010000000
//1111111111111111111111101111111 //1111111111111111111111110000000 //10000000-a
//%u- Signed integer //1111111111111111111111110000000- Complement // //%u unsigned int printf("%u\n", a);
return 0; } #include<stdio.h> int main() { char a[1000]; int i; for(i=0; i<1000;
i++) { a[i] = -1-i; } printf("%d",strlen(a)); return 0; }
Technology