This article introduces C The storage form of integer variable in memory . Mainly analyzes the binary and hexadecimal form .

The sample code is as follows :

 
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int a
= -7; char string_2[35] = ""; char string_16[35] = ""; /* Print it out of the system int The number of bytes occupied by the type */
printf("sizeof(a) is %d\n", sizeof(a)); printf("sizeof(int) is %d\n",
sizeof(int)); /* string_2 Storing integer variables in a Binary representation of */ itoa(a, string_2, 2);
printf("integer a = %d\nbinary a = %s\n", a, string_2); printf("length of
string_2 a is %d.\n", strlen(string_2)); /* string_16 Storing integer variables in a The hexadecimal expression of */
itoa(a, string_16, 16); printf("integer a = %d\nHexadecimal a = %s\n", a,
string_16); printf("length of string_16 a is %d.\n", strlen(string_16));
system("pause"); return 0; }
The running results of the above code are as follows :

 

 
sizeof(a) is 4 sizeof(int) is 4 integer a = -7 binary a =
11111111111111111111111111111001 length of string_2 a is 32. integer a = -7
Hexadecimal a = fffffff9 length of string_16 a is 8.
It can be seen from the above code and its running results :

 

a)-7 The corresponding binary value in memory is 11111111111111111111111111111001; The corresponding hexadecimal value is fffffff9, Through the binary value complement operation :

1. Except sign bit , Reverse operation :11111111111111111111111111110110;

2. Plus 1 operation :1111 1111 1111 1111 1111 1111 1111 0111, This value is the original code , The corresponding value is -7.

Through the above calculation , Proved -7 It is stored in memory in the form of binary complement . And hexadecimal is only the complement of binary integration , It is also stored in the form of complement .

b)-7 Complement of  1111 1111 1111 1111 1111 1111 1111 1001
in , front 28 Bits are sign bits , Only in the end 4 Bits are numeric bits . When performing an operation , Marking out the sign position is helpful to carry out the operation more clearly and understand the relationship between the original code and the complement code .

Technology
©2019-2020 Toolsou All rights reserved,
2020 The 11th National Blue Bridge Cup C/C++b Group summary ( Completion ) Review of the most complete computer network principles in history vue-cli 3 VUE Scaffold project construction ( Detailed explanation ) solve Vue+TypeScript Under development TS Don't recognize this.$refs The question of Vue Using the function of anti chattering and throttling How to use division operation in relational algebra SQL Statement representation ?copy-webpack-plugin Copy and compress files avue The use of dictionaries in English Teaching girls to learn Java: What is? Java?python Code painting Cherry Blossom -python Draw cherry tree code Specific code introduction