当前位置:网站首页>Fledgling Xiao Li's 108th blog binary print
Fledgling Xiao Li's 108th blog binary print
2022-07-23 07:54:00 【Fledgling Xiao Li】
Introduction to binary printing
C Language has a lot of format control when formatting and printing , For example, decimal printing %d Output , Hexadecimal use %x Output , Octal uses %o Format output , But when we expect to see the binary of a data, we must use a calculator or other inconvenient tools, so we consider writing a simple binary printing function .
Principle of binary printing
The internal storage of the computer is binary, so whether it is signed or unsigned, it is binary , utilize C The bit operation of language can realize the access to each bit , Here I just want to output binary , I don't want to record this number, so what I print is 2 String composed of hexadecimal characters .
Binary print code
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");
}
Test code
printf(" test 1: The argument is a positive decimal number : %d\r\n",2147483647);
printf_2(2147483647);
printf(" test 2: The argument is a decimal negative number : %d\r\n",-1);
printf_2(-1);
printf(" test 3: The parameter is a hexadecimal number : 0x%X\r\n",0x80010000);
printf_2(0x80010000);
Running results 
Explanation of results
Positive test

The highest bit of a signed number is its Sign bit So print 2147483647 The highest result is 0;
Hexadecimal number test

Negative test
Because negative numbers exist in the form of complements in computers -1 The printout is -1 Complement

边栏推荐
- 真人踩过的坑,告诉你避免自动化测试常犯的10个错误
- “蔚来杯“2022牛客暑期多校训练营1 (部分题目总结)
- 网络安全之ARP欺骗防护
- 局域网SDN技术硬核内幕 4 从计算虚拟化到网络虚拟化
- Flick batch reading es
- File upload, server file name Chinese garbled file upload, server file name Chinese garbled
- 局域网SDN技术硬核内幕 - 16 三 从物到人 园区用户漫游的EVPN实现
- yolov5 test.py BrokenPipeError: [Errno 32] Broken pipe问题解决
- 弥散张量分析开源软件 DSI Studio 简体中文汉化版可以下载了
- 局域网SDN硬核技术内幕 19 团结一切可以团结的力量
猜你喜欢
随机推荐
ROS2常用命令行工具整理ROS2CLI
【翻译】Chaos Mesh移至CNCF孵化器
Leetcode 20有效的括号、33搜索旋转排序数组、88合并两个有序数组(nums1长度为m+n)、160相交链表、54螺旋矩阵、415字符相加(不能直接转Int)、reverse()函数
亚马逊旗下Zoox通过安全测试 并在加州申请试驾
Customize flick es source
LAN SDN technology hard core insider 5 implementation of virtualized network
I use the factory mode in jd.com and explain the factory mode clearly
With 130 new services and functions a year, this storage "family bucket" has grown again
Why does MySQL index use b+ tree instead of jump table?
JVM监控工具介绍jstack, jconsole, jinfo, jmap, jdb, jstat
ROS based navigation framework
URL的结构解读
類和對象(1)
Scala 获取指定目录下的所有文件
93. (leaflet chapter) leaflet situation plotting - modification of attack direction
工作流引擎在vivo营销自动化中的应用实践
Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
【09】程序装载:“640K内存”真的不够用么?
真人踩过的坑,告诉你避免自动化测试常犯的10个错误
局域网SDN硬核技术内幕 20 亢龙有悔——规格与限制(上)









