31 days of advanced C language - 1, data storage: integer

Preliminary: VS Debugging Tips

(1), there is a piece of code to be debugged.

#include<stdio.h>
int main() {
    
    
	int a = 10;
	float b = 10.0f;

	return 0;
}

(2), there is a breakpoint.

Just click there.

insert image description here
(3), enter debugging.

insert image description here
Effect: run, but not displayed.

insert image description here
(4), bring up the memory interface.

Debug - Window - Memory - choose any one.

insert image description here

Effect:

insert image description here
(5), check the value of a variable.

Enter &xxx in the address and press Enter. xxx is the variable name.

insert image description here
In the case, the value of a is 10, the memory is 0a, hexadecimal. a is 1 greater than 9, which is 10.

1, 1 and -1 memory

Code for debugging:

#include<stdio.h>
int main() {
    
    
	int a = 1;
	int b = -1;

	return 0;
}

Memory result: 1

insert image description here

Memory result: -1

insert image description here
No doubt, it is the four bytes in the upper left corner.

2, signed, unsigned

Values ​​use the highest binary bit to represent the sign. 0 is positive, 1 is negative.

An unsigned modified type has no sign bit.

For example, unsigned int, four bytes, are all numeric content.
In int, the highest bit represents the sign, and only 31 bits are numeric.

Unsigned max:
n-1 of 2 signed max: n-1 of 2-1

3, original code, inverse code, complement code

Unsigned number: The three codes are the same.

Signed positive number: The three codes are the same.

Signed Negative Numbers: Pay attention.
Source code: The binary representation of a numerical value.
One's complement: the sign remains unchanged, and the other bits are flipped. 1 becomes 0, 0 becomes 1.
Two's complement: add 1 to the result of one's complement.

Example: -1, one byte.
Original code: 1 000 0001
Inverse code: 1 111 1110
Complement code: 1 111 1111

,4, integers are stored in two's complement

The three codes of 1 are the same, so the byte order of 01 00 00 00 in memory
is reversed.

The complement of -1 is 1111 1111
to hexadecimal, which is ff.

5, complement, positive addition

Just add it directly.

1+1=2

0000 0001
0000 0001
=》
0000 0010

6, complement, subtraction of positive numbers

Convert to positive and negative numbers, and add directly.

-1
Original: 1000 0001
Anti: 1111 1110
Complement: 1111 1111

1±1=0

0000 0001 + 1111 1111

Sign bit: 1
Value: 000 0000, overflow.

On overflow, the sign bit is flipped, the result:
0000 0000

7, big endian, little endian

There are two strategies for storing data:

  1. Play from left to right. 1 is 00 00 00 01
  2. Play from right to left. 1 is 01 00 00 00

Use the pointer to determine the current policy:

int isSmall() {
    
    
	int test = 1;
	char* p = (char*)&test;
	if (*p == 1) {
    
    
		//大端
		return 0;
	}
	else {
    
    
		//小端
		return 1;
	}
}

To simplify:

int isBig() {
    
    
	int test = 1;
	return *(char*)&test;
}

Extra: Realize Modify Money

Change an address value to ff.

Code:

#include<stdio.h>
int main() {
    
    
	int money = 10;
	printf("金币数:%d\n", money);

	printf("金币数:%d\n",money);
	return 0;
}

Find the memory address of money:

insert image description here
Right-click on the value to edit the value.

insert image description here
Effect:

insert image description here
Final display:

insert image description here

Guess you like

Origin blog.csdn.net/qq_37284843/article/details/124418902