采用移位和相与方式求整数在内存中的二进制形式。
#include<stdio.h>
typedef int DataType;
int num_covert_binary(DataType num);
void main()
{
DataType num;
num = -1;
num_covert_binary(num);
num = 12;
num_covert_binary(num);
getchar();//让console 等待一下
}
int num_covert_binary(DataType num)
{
DataType base_data;
base_data = 1;
base_data = base_data << (sizeof(DataType)* 8 - 1);//相当最高位是1其它位为0
printf("\n%d在内存中二进制形式为:\n", num);
for (int i = 1; i <= sizeof(DataType)* 8; i++)
{
DataType temp = base_data#
if (temp == 0) //相与的结果为 0
{
putchar('0');
}
else
{
putchar('1');
}
if (i % 4 == 0)
{
printf(" ");
}
num = num << 1; //左移一位
}
return 0;
}