写一个函数,用来输出整数在内存中的二进制形式(位于运算题)
/*
写一个函数,用来输出整数在内存中的二进制形式
*/
#include <stdio.h>
void printBinary(int number);
int main()
{
/*
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 1111
9 : 0000 0000 0000 0000 0000 0000 0000 1001
-10 : 1111 1111 1111 1111 1111 1111 1111 0110
*/
//printf("%d\n", ~9);
printBinary(-10);
return 0;
}
void printBinary(int number)
{
// 记录现在挪到第几位
// (sizeof(number)*8) - 1 == 31
int temp = ( sizeof(number)<<3 ) - 1;
while ( temp >= 0 )
{
// 先挪位,再&1,取出对应位的值
int value = (number>>temp) & 1;
printf("%d", value);
//
temp--;
// 每输出4位,就输出一个空格
if ( (temp + 1) % 4 == 0 )
{
printf(" ");
}
}
printf("\n");
}

浙公网安备 33010602011771号