/******************************
作者:cncoderalex
博客:http://blog.youkuaiyun.com/cncoderalex
*******************************/
#include<iostream>
using namespace std;
template <typename T>
void PrintBinary(T value)
{
int BytesCount = sizeof(T);
unsigned long long Bit = 1;
string str;
for (int i = 0; i < BytesCount * 8; i++)
{
if (value & Bit)
{
str.append("1");
}
else
{
str.append("0");
}
Bit <<= 1;
}
int len = str.length();
for (int i = len - 1; i >= 0; i--)
{
printf("%c", str[i]);
}
printf("\n");
}
int main()
{
printf("http://blog.youkuaiyun.com/cncoderalex");
printf("\n");
int a = -1;
unsigned char b = a;
int c = b;
char d = a;
int e = b;
short f = a;
int g = b;
PrintBinary(a);
PrintBinary(b);
PrintBinary(c);
PrintBinary(d);
PrintBinary(e);
PrintBinary(f);
PrintBinary(g);
system("pause");
return 0;
}打印二进制位
最新推荐文章于 2024-07-28 00:40:42 发布
本文介绍了一个通用的C++模板函数,用于将任意类型的数据转换为二进制形式并打印出来。该函数可以处理多种数据类型,如整型、字符型等,并展示了如何使用这个函数来查看不同类型变量的二进制表示。
2832

被折叠的 条评论
为什么被折叠?



