我想打印C 32位二进制使用位掩码但二进制表示未在if语句得到打印。
unsigned int bit_mask = 2147483648;
int decimal = 2;
printf("\nBinary representation of 2: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}
decimal = 255;
printf("\n\nBinary representation of 255: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}
decimal = 32;
printf("\n\nBinary representation of 32: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}
decimal = -1;
printf("\n\nBinary representation of -1: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}
decimal = -255;
printf("\n\nBinary representation of -255: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}
int random_number = (rand() % INT_MAX) + (rand() % INT_MIN);
printf("\n\nBinary representation of %d: \n", random_number);
while(bit_mask > 0){
if((random_number & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}
PS:程序现在只工作2
,但仍然不打印其它值(255,32,-1,-255)
如果BLUEPIXY和我建议的修正是由,代码将产生正确的顺序位的答案。
#include <stdio.h>
int main(void)
{
for (unsigned value = 2; value < 1024; value = value * 3 + 1)
{
unsigned bit_mask = 0x80000000; // 2147483648
printf("Binary representation of %3u: ", value);
while (bit_mask > 0)
{
if ((value & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask >>= 1;
}
putchar('\n');
}
return 0;
}
输出:
Binary representation of 2: 00000000000000000000000000000010
Binary representation of 7: 00000000000000000000000000000111
Binary representation of 22: 00000000000000000000000000010110
Binary representation of 67: 00000000000000000000000001000011
Binary representation of 202: 00000000000000000000000011001010
Binary representation of 607: 00000000000000000000001001011111
输出不反转,尽管什么BLUEPIXY说在注释 。
所述循环体可以被转换成一个功能,诸如void print_binary(unsigned value)
以最小的努力,并且然后将被从一个循环调用。 或多或少同一回路的编辑问题写出来六次是很滑稽(!) - 不写这样的代码。 当你copy'n'paste这样的代码,有要写入的功能等待。
#include <stdio.h>
void print_binary(unsigned value)
{
unsigned bit_mask = 0x80000000; // 2147483648
while (bit_mask > 0)
{
if ((value & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask >>= 1;
}
}
此功能可用于打印的二进制表示unsigned int
而不添加任何装饰。 它可以合理地普遍使用。 在这个特定背景,你可以写一个包装函数来处理其他格式:
void fmt_binary(int value)
{
printf("Binary representation of %3d: ", value);
print_binary((unsigned)value);
putchar('\n');
}
因为你有一个原型演员是没有必要的,只要print_binary()
的范围。 从C99开始,你必须有一个当前功能的声明,但是,这并不一定是一个原型。 然而,编译没有原型现在是愚蠢的。 而且你会发现,确保如果你试图吝啬你的编译器抱怨的选项。 对于GCC,您可以使用:
gcc -std=c11 -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes …
您可能会或可能不会增加-Wold-style-declaration
或-Wold-style-definition
取决于你正在处理的代码库和GCC的版本,您正在使用(和你是在写代码怎么不小心)。 还有你会考虑其他的选择,比如-Wshadow
,但如果你的代码清单所示完全编译,这是不可能碰到不属于逻辑问题的许多问题。
随着fmt_binary()
定义的,你可以写main()
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// … declarations or definitions of fmt_binary and print_binary
int main(void)
{
for (int value = 2; value < 1024; value = value * 3 + 1)
fmt_binary(value);
fmt_binary(2);
fmt_binary(255);
fmt_binary(32);
fmt_binary(-1);
fmt_binary(-255);
srand(time(0)); // Better than no call to srand()
int random_number = (rand() % INT_MAX) + (rand() % INT_MIN);
fmt_binary(random_number);
return 0;
}
示例输出可能是:
Binary representation of 2: 00000000000000000000000000000010
Binary representation of 7: 00000000000000000000000000000111
Binary representation of 22: 00000000000000000000000000010110
Binary representation of 67: 00000000000000000000000001000011
Binary representation of 202: 00000000000000000000000011001010
Binary representation of 607: 00000000000000000000001001011111
Binary representation of 2: 00000000000000000000000000000010
Binary representation of 255: 00000000000000000000000011111111
Binary representation of 32: 00000000000000000000000000100000
Binary representation of -1: 11111111111111111111111111111111
Binary representation of -255: 11111111111111111111111100000001
Binary representation of -1758826555: 10010111001010100110111111000101