类型转换 double的存储

类型转换

  • c语言风格

    1. (type) expression
    2. type (expression)
  • c++风格

    1. static_cast
    2. dynamic_cast
    3. reinterpret_cast
    4. const_cast
       使用: xx_cast <type>(expression)

const_cast

#include <iostream>
using namespace std;

class Person {};

int main() {
	const Person* p = new Person();
	//两种写法没有差别
	// const_cast 能说明之前的是const
	Person* p1 = const_cast<Person*>(p);
	Person* p2 = (Person*)p;
	return 0;
}

dynamic_cast

#include <iostream>
using namespace std;

class Person {
	virtual void run() {}
};

class Student:public Person{};

class Car{}int main() {
	Person* p1 = new Person();
	Person* p2 = new Student();

	cout << "p1 :" << p1 << endl;
	cout << "p2 :" << p2 << endl;
//dynamic 只能用在多态中(有虚函数);用在指针中;会做安全检测,不安全的为0
//c风格不做安全检测
	Student* stu1 = dynamic_cast<Student *>(p1); //不安全
	Student* stu2 = dynamic_cast<Student *>(p2); //安全

	cout << "stu1 :" << stu1 << endl;
	cout << "stu2 :" << stu2 << endl;
//支持交叉转换,直接赋值给NULL
	Car *car = dynamic_cast<Car *>(p2);
	return 0;
}

static_cast

  • 对比dynamic_cast,缺乏运行安全检测
  • 不能交叉转换(不是同一继承体系的,无法转换)
  • 常用于基本数据类型转换、非const转成const
  • 适用范围较广

reinterpret_cast

  • 没有任何类型检查和格式转换,仅仅是简单的二进制数拷贝
  • 可以交叉转换
  • 可以将指针和整数相互转换
#include <iostream>
using namespace std;

int main() {
	int a = 10;
	double b = reinterpret_cast<double&>(a);
	int* p = reinterpret_cast<int*>(0x100);

	return 0;
}

double的存储

符号位阶位位数
float1823
double11152

10 --> 1010 --> 1.010*2^3
符号位:0
阶位:3 --> 3+1023 --> 0100 0000 0010(前十二位) 因为指数可以为负,为了便于计算,规定都先加上1023
尾数:010
10(double) --> 0100 0000 0010 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
小端存储:0x00 00 00 00 00 00 24 40

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值