位 : bit
字节 : byte
1byte = 8bit

符号数
image

若是正负数都用原码表示则会出现如下两个问题

image
image

故采用补码 以二字节为例

image

下为各数据类型所占字节和表示范围
image

在c++中 可以通过sizeof(类型)来确定类型所占字节

#include<iostream>
using namespace std;
int main()
{
	int a;
	cout << sizeof(int) << endl;
	cout << sizeof(a) << endl;
	return 0;
}

上下限依旧可以通过代码输出

#include<iostream> 
#include<climits>
using namespace std;
int main()
{
	cout << INT_MAX << endl;
	cout << UINT_MAX << endl;
	cout << LLONG_MAX << endl;
	cout << SHRT_MAX << endl;
	
	return 0;
} 

各种进制在c++代码中表示如下

#include<iostream> 
using namespace std;
int main()
{
	cout << 123 << endl;        //十进制 
	cout << 0b1111011 << endl;  //二进制 
	cout << 0173 << endl;       //八进制 
	cout << 0x7B << endl;       //十六进制 
	return 0;
} 

并且还可以通过前导进制转换改为其他进制

#include<iostream> 
using namespace std;
int main()
{
	int a = 123; 
	cout << hex << a << endl;       //十六进制 
	cout << oct << a << endl;       //八进制 
	cout << dec << a << endl;       //十进制 
	return 0;
} 

c++提供typeid运算符,可以查询数字类型

#include<iostream> 
using namespace std;
int main()
{
	int a = 123; 
	cout << typeid(123).name() << endl;
	cout << typeid(123L).name() << endl;
	cout << typeid(123U).name() << endl;
	cout << typeid(123UL).name() << endl;
	cout << typeid(123LU).name() << endl;
	return 0;
} 

可以根据下图对照
image

下面为浮点型常量在内存中的储存方式
浮点数在内存中的存储存在三部分,分别是符号位指数部分尾数部分
image
image

IEEE754规定, 指数位用于表示[-127, 128]范围内的指数

规定: 在32位单精度类型中, 这个偏移量是127 在64位双精度类型中, 偏移量是1023

计算方法

  1. 尾数由二进制小数转为十进制小数后加1

  2. 指数转为十进制再加上偏移量

  3. -1的指数次方 × 2^指数 × 尾数