当前位置:网站首页>初出茅庐的小李第108篇博客二进制打印
初出茅庐的小李第108篇博客二进制打印
2022-07-22 21:34:00 【初出茅庐的小李】
二进制打印介绍
C语言在格式化打印的时候有很多格式控制,比如十进制打印用%d输出,十六进制用%x输出,八进制用%o格式输出,但是当我们期望看一个数据的二进制的时候就必须借助计算器或者其他比较不方便于是考虑写一个简单的二进制打印函数。
二进制打印原理
计算机内部存储都是二进制的所以不管是有符号数还是无符号数都是二进制的,利用C语言的位操作就可以实现对每个位的访问,这里我仅仅是为了输出二进制,并不是要把这个数记录下来所以我打印的是2进制字符组成的字符串。
二进制打印代码
void printf_2(int data)
{
int i = 0;
for(i = 0;i<32;i++)
{
if(data&(0x80000000>>i))
{
printf("1");
}
else
{
printf("0");
}
}
printf("\r\n");
}
测试代码
printf("测试1:参数是十进制正数: %d\r\n",2147483647);
printf_2(2147483647);
printf("测试2:参数是十进制负数: %d\r\n",-1);
printf_2(-1);
printf("测试3:参数是十六进制数: 0x%X\r\n",0x80010000);
printf_2(0x80010000);
运行结果
结果解释说明
正数测试

有符号数的最高位是其符号位所以打印2147483647的结果最高位就是0;
十六进制数测试

负数测试
由于负数在计算机中是以补码的形式存在的所以-1打印结果是-1的补码

边栏推荐
- 【无标题】
- Talk about 12 business scenarios of concurrent programming
- 用Stanford Parse(智能语言处理)去实现分词器
- 驱动页面性能优化的3个有效策略
- LAN SDN hard core technology insider 18 beautiful new world
- 【翻译】Chaos Mesh移至CNCF孵化器
- 自定义flink es source
- Interpretation of URL structure
- 2022 employment season surprise! The genuine Adobe software can finally be used for nothing
- 树和二叉树
猜你喜欢

多传感器融合综述---FOV与BEV

Mysql A left(right) join B on A.id=B.id and A.age=1与A left(right) join B on A.id=B.id where A.age=1

Graduation project ----- Internet of things environment detection system based on stm32

记一次线上SQL死锁事故:如何避免死锁?

Here comes the genuine Adobe software! Adobe's only genuine family bucket subscription in the world costs only 0 yuan / year

景联文科技提供3D点云-图像标注服务

zabbix agent创建监控项

微信小程序项目实战

VMware 中搭建 SylixOS 环境

Wechat hotel reservation applet graduation project (7) Interim inspection report
随机推荐
如何保护 JDBC 应用程序免受 SQL 注入
Talk about 12 business scenarios of concurrent programming
成功解决:error: src refspec master does not match any
延伸联接边界,扩展业务范围,全面迈向智能云网2.0时代
Leetcode 757 set the intersection size to at least 2[sort greedy] the leetcode path of heroding
升级poi-tl版本1.12.0与旧版poi(4.1.2)、easyexcel之间的依赖冲突解决
Mysql无法访问,navicat提示:is not allowed to connect to this MySQL server
zabbix agent创建监控项
002_Kubernetes安装配置
Redis三种集群方案
Classes et objets (1)
Utools recommendation
局域网SDN技术硬核内幕 7 从二层到大二层
Inside the hard core of LAN SDN technology - evpn implementation of 16 three from thing to person user roaming in the park
Scala when used Performance problems of contains().Exists()
Wechat campus second-hand book trading applet graduation design finished product (6) opening defense ppt
Mysql A left(right) join B on A.id=B.id and A.age=1与A left(right) join B on A.id=B.id where A.age=1
测试用例设计方法合集
我在京东使用工厂模式,一文说清楚工厂模式
微信小程序项目实战