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
Daily Recommendation
views 4