当前位置:网站首页>storage of data in memory

storage of data in memory

2022-08-10 15:21:00 7 on 7.

一、数据类型

数据类型有7种:

   char        字符型  
   short       短整型 
   int         整型
   long        长整型
   long long  更长整型
   float      单精度浮点数 
   double     双精度浮点数

二、原码,反码,补码

计算机中的整数有三种2进制表示方法,即原码、反码和补码.
三种表示方法均有符号位和数值位两部分,符号位都是用0表示’正”,用1表示"负”,而数值位正数的原、反、补码都相同.
负整数的三种表示方法各不相同.
原码:It is to directly translate the value into binary in the form of positive and negative numbers to get the original code.
反码:原码的符号位不变,The other bits are reversed bit by bit to get the inverse code.
补码:反码加1,得到补码.
计算例子:如图在这里插入图片描述
计算a+b:
a是正数,原码等于补码:00000000 00000000 00000000 00000111
b是负数,原码:10000000 00000000 00000000 00001010
反码:111111111 11111111 11111111 11110101
补码:11111111 11111111 11111111 11110110
a+bThe complements of , are added separately:11111111 11111111 11111111 11111101
而打印的是%d即有符号整型,to convert it to the original code,Subtract one and negate it to the original code:
10000000 00000000 00000000 00000011 再化为10进制就是-3
看下运行结果:
在这里插入图片描述

三、大小端

There are two ways to store data in memory, one is big endian and the other is little endian.
在计算机系统中,以字节为单位的,每个地址单元对应着一个字节,一个字节为8bit.但在C语言中除了8 bit的char之外,还有16 bit的short型,32bit的long型(要看具体的编译器),另外,对于位数大于8位的处理器,例如16位或者32位的处理器,Because the odd memory width is greater than one byte,Then there is a problem of how to arrange multiple bytes.This leads to a big-endian storage model and a little-endian storage model.
How can we tell the endianness of the current machine:

#include<stdio.h>
int is_sys()
{
    
	int a = 1;
	return (*(char *)&a);
}
int main()
{
    
	int ret = is_sys();
	if (ret == 1)
	{
    
		printf("小端\n");
	}
	else
	{
    
		printf("大端\n");
	}
	return 0;
}

Take an integer1,补码:0000000 0000000 00000000 00000001,To determine which storage mode it is,Just take out the first byte,Because low bits are big endian when placed at high addresses,Little endian when low bits are placed at low addresses,The address is first converted into a character pointer to ensure that a byte is taken ,Then dereference the content,如果是1就是小端,反之是大端.

整型提升

C的整型算术运算总是至少以缺省整型类型的精度来进行的.为了获得这个精度,表达式中的字符和短整型操作数在使用之前被转换为普通整型,这种转换称为整型.
Truncation occurs when we store data with character types,Because a number is four bytes,A character type can only store one byte,Look at the current sign bit after truncation:
对于有符号类型,如果是1就把1The previous bit completion1进行整型提升,如果是0就把0The previous bit completion0进行整型提升.
对于无符号类型,直接补全0.
(1)We can understand it well by looking at the following example:

#include<stdio.h>
int main()
{
    
	char a = -2;
	unsigned char b = -10;
	printf("%d %d", a,b);
}

a=-2,原码:10000000 0000000 00000000 00000010
反码: 11111111 11111111 11111111 11111101
补码: 11111111 11111111 11111111 11111110
但是aIt is a character type that can only store one byte,A truncation will occur and only take the low-order one byte ie:11111110
And we print a signed integer%d,会发生整型提升,因为它是负的,所以在前面补1
11111111 11111111 11111111 11111110,The original code is printed,So convert to the original code.减一取反:10000000 00000000 00000000 00000010 结果是-2
b=-10,原码:10000000 00000000 00000000 00001010
反码: 11111111 11111111 11111111 11110101
补码 :11111111 11111111 11111111 11110110
Ditto after truncation:11110110 Because he is an unsigned integer in front0:
00000000 00000000 00000000 11110110.The direct print result of the original code is246.
Verify the result again:
在这里插入图片描述

(2)另外%u是打印无符号整型.It is also truncated to see if the original number has a sign,If there is a sign, it is not complemented1或补0,unsigned direct complement0.Then print it directly as the original code after the completion
例如 char=-128
原码:10000000 00000000 00000000 10000000
反码: 11111111 11111111 11111111 01111111
补码: 11111111 11111111 11111111 10000000
Because it is a character type, take the lower byte 1000000,And because he is a negative complement1:
11111111 11111111 11111111 10000000,Print directly as the original code If the factor is too large, see the result directly:
在这里插入图片描述
(3)One more analysis:

#include<stdio.h>
int main()
{
    
	
	int i = -20;
	unsigned char j = 10;
	unsigned char b = i + j;
	printf("%u", b);
}

i的原码:10000000 00000000 00000000 00010100;
反码: 11111111 1111111 11111111 11101011
补码: 11111111 11111111 11111111 11101100
而jis an integer-less character value 因为它是正数,原码等于补码:
00000000 00000000 00000000 00001010
两者相加是:11111111 11111111 11111111 11110110
And there is an unsigned char type,发生截断:11110110
而打印的是%uDirect integer boost complement0 补码等于原码 :
00000000 00000000 00000000 11110110结果是246
看结果:
在这里插入图片描述

原网站

版权声明
本文为[7 on 7.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101445271361.html