Truncation, plastic lifting, big end and small end

1. Truncation and shaping promotion

(1) Truncation (big ones to small ones)

在C语言中进行赋值的时候,如果将字节多的数据
类型赋给一个字节少的数据类型,会发生“截断”

The reason for this situation: In the process of assignment, only the low bit of the variable with a longer byte is assigned to the variable with a shorter byte

(2) Plastic surgery (small to large)

整形提升是C语言中的一项规定:在表达式计算时,
各种整形首先要提升为int类型,如果int类型不
足以表示则要提升为unsigned int类型;然后执
行表达式的运算

The rules of plastic promotion:
a. Unsigned number complement 0
b. Signed number complement sign bit

(3) Example 1: What is the output a, b, and c of the following program?

#include<stdio.h>
int main()
{
    
    
	char a = -1;
	signed char b = -1;
	unsigned char c = -1;
	printf("a=%d b=%d c=%d\n", a,b,c);
	return 0;
}

Insert picture description here
Insert picture description here

2. Big end little end

(1) What is the big and small end?

Big-endian (storage) mode: means that the low bits of data are stored in the high address of the memory, while the high bits of data are stored in the low address of the memory;
little- endian (storage) mode: means that the low bits of data are stored in the low address of the memory In the address, the high bit of the data is stored in the high address of the memory.
Insert picture description here
**Note: **Only the low address on the left and the high address on the right are stored in the memory of the little endian; the rest are the high addresses on the left and the low addresses on the right.

(2) Baidu written test questions (design a program to judge whether the current machine is big-endian or small-endian)

#include<stdio.h>
int JudgmentMachine()
{
    
    
	int a = 1;
	char val = a; //发生截断
	if (val = '0x01')
	{
    
    
		return 1;
	}
	return 0;
}
int main()
{
    
    
	if (JudgmentMachine())
	{
    
    
		printf("小端\n");
	}
	else
	{
    
    
		printf("大端\n");
	}
	return 0;
}

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_50886514/article/details/111478367