当前位置:网站首页>Introduction to C integer data storage
Introduction to C integer data storage
2022-08-10 06:54:00 【[email protected]】
前言
Learning computer knowledge is not only about programming and writing code,At the same time, it is necessary to be clear about the underlying logic of the computer..Briefly introduce todayCHow integer data is stored in the language,A brief look at the data storage process.
一、Integer data types and storage formats
1.Integer data types are as follows
char (because the characters can be usedASCIIcode value to represent,所以charAlso classified as integer)
unsigned char
signed char
short
unsigned short [int]
signed short [int]
int unsigned int
signed int
long
unsigned long [int]
signed long [int]
变量的创建是要在内存中开辟空间的,空间的大小是根据不同的类型而决定的.现在定义一个 int a,So how is the integer stored in the memory space??
In the computer world data is stored in binary form,How is an integer represented in binary in a computer??We import the original code 补码 反码的概念.
计算机中的整数有三种2进制表示方法,即原码、反码和补码.
三种表示方法均有符号位和数值位两部分,符号位都是用0表示“正”,用1表示“负”.
正整数的原、反、补码都相同,Numerical bits are their binary,符号位为0;
The sign bit of the original code of the negative integer is unchanged and the inverse is the one's complement,反码加1即为补码;
The original code value of a negative integer is the binary representation of its absolute value,符号位为1.
数据存放内存中其实存放的是补码.
为什么呢?
在计算机系统中,数值一律用补码来表示和存储.原因在于,使用补码,可以将符号位和数值域统一处理;同时,加法和减法也可以统一处理(CPU只有加法器)此外,补码与原码相互转换,其运算过程是相同的,不需要额外的硬件电路.
二、数据存储顺序
Data is stored in bytes in memory,8个比特位为一个字节.
So what is the byte order??
先定义一个int a =20为例,通过vs2019查询a的地址得到aspecific storage format,为了便于观察,Resize the window beforehand to4bytes are displayed on a line,that is, an integer.
A byte is two16进制位,the above is just right8个16进制位,14Converting to decimal is20;
从14 00 00 00,It can be seen that the value bits stored in the byte are from low to high,**即 14Is the lowest value,So it can be guessed whether the negative integers are also arranged in this way??
Then we define a negative integer to observe.
通过vs2019对bTake the address and observebThe corresponding hexadecimal is f6 ff ff ff
为了便于理解,可以将-10的原码 反码 补码 All said to come out
So it is not difficult to find that negative integers are also stored in the same way as positive integers,The data stored in the byte also starts from the low order..Then draw out the concept on byte size
字节大小端介绍
什么大端小端:
大端(存储)模式,是指数据的低位保存在内存的高地址中,而数据的高位,保存在内存的低地址中;
小端(存储)模式,是指数据的低位保存在内存的低地址中,而数据的高位,保存在内存的高地址;
为什么有大端和小端:
这是因为在计算机系统中,以字节为单位的,每个地址单元都对应着一个字节,一个字节为8 bit.但是在C语言中除了8 bit的char之外,还有16 bit的short型,32 bit的long型(要看具体的编译器),另外,对于位数大于8位的处理器,例如16位或者32位的处理器,由于寄存器宽度大于一个字节,那么必然存在着一个如何将多个字节安排的问题.因此就导致了大端存储模式和小端存储模式.
例如:一个 16bit 的 short 型 x ,在内存中的地址为 0x0010 , x 的值为 0x1122 ,那么 0x11 为高字节, 0x22 为低字节.对于大端模式,就将 0x11 放在低地址中,即 0x0010 中, 0x22 放在高地址中,即 0x0011 中.小端模式,刚好相反.我们常用的 X86 结构是小端模式,而 KEIL C51 则为大端模式.很多的ARM,DSP都为小端模式.有些ARM处理器还可以由硬件来选择是大端模式还是小端模式.
Below is an example
百度2015年系统工程师笔试题:
请简述大端字节序和小端字节序的概念,设计一个小程序来判断当前机器的字节序.
The concept of byte size end is no longer explained.,Mainly introduce how to determine the byte order
大致思路
The endianness of the machine can be known by simply examining how an integer is endian-ordered in that machine
So we just need to access the first byte of the integer,Figure out whether it stores the high-order or low-order bits of the integer value.
因为char是一个字节的大小,所以可以用chartype pointer to access integer type
With a general idea, you can write the following code
#include<stdio.h>
int check_sys()
{
int a = 1;
return *(char*)&a;
}
int main()
{
if(check_sys() == 1)
printf("小端\n");
else
printf("大端\n");
return 0;
}
因为a的补码表示为 00000000 00000000 00000000 00000001
If little endian is stored 第一个字节 放的就是 1;
If stored in big-endian byte order 第一个字节 放的就是 0;
The endianness of the machine can be determined by the return value of the function.
总结
The above is my brief introduction to integer storage ,如有错误欢迎指正,谢谢.
A final exercise.interested in doing it,猜猜输出结果是什么?Next blog will explain in detail.
#include <stdio.h>
int main()
{
char a = -1;
signed char b = -1;
unsigned char c = -1;
printf("a=%d,b=%d,c=%d", a, b, c);
return 0;
}
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208100619596915.html
边栏推荐
- 34. 谈谈为什么要拆分数据库?有哪些方法?
- I would like to ask you guys, when FLink SQL reads the source, specify the time field of the watermark. If the specified field is in the grid
- 人工神经网络模型的特点,人工神经网络模型定义
- 人工神经网络工作原理,神经网络的工作原理
- A few lines of code can crash the system;
- u-boot ERROR: Failed to allocate 0x5c6f bytes below 0x17ffffff.Failed using fdt_high value
- Chapter 12 Other Database Tuning Strategies [2. Index and Tuning] [MySQL Advanced]
- 椭圆曲线离散对数问题以及求解
- WooCommerce installation and rest api usage
- 自动化测试框架Pytest(二)——前后置处理
猜你喜欢
Grammar Basics (Judgment Statements)
High quality WordPress download station 5 play theme template
如何治理资源浪费?百度云原生成本优化最佳实践
BUUCTF Notes (web)
【MySQL】使用MySQL Workbench软件新建表
【Event Preview on August 9】Prometheus Summit
【电商业务】外行为何难区别 商品属性与商品规格
ES13 - ES2022 - 第 123 届 ECMA 大会批准了 ECMAScript 2022 语言规范
语法基础(判断语句)
MySQL之InnoDB引擎(六)
随机推荐
MySQL设置初始密码—注意版本mysql-8.0.30
Ladies and gentlemen, oracle11g, cdc2.2, flink1.13.6, single-table incremental synchronization.Without adding data
椭圆曲线离散对数问题以及求解
CuteOneP 一款php的OneDrive多网盘挂载程序 带会员 同步等功能
BUUCTF Notes (web)
ctfshow SSTI 知识点总结
DGIOT支持工业设备租赁以及远程管控
.NET-8. My Thought Notes
Discussion on Chinese Fuzzy Retrieval in Databases
Bigder:42/100 showCase多少bug可以打回去
语法基础(判断语句)
1413. Stepwise Summation to Get Minimum Positive Numbers
PLSQL学习第一天
裸辞—躺平—刷题—大厂(Android面试的几大技巧)
[网络安全]实操AWVS靶场复现CSRF漏洞
WooCommerce installation and rest api usage
简单业务类
【强化学习】《Easy RL》- Q-learning - CliffWalking(悬崖行走)代码解读
【MySQL】使用MySQL Workbench软件新建表
mysql之两阶段提交