问题
I'm trying to print binary using bit mask of 32 bit in c but the binary representation is not getting printed in the if statement.
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: the program is now only working for 2
but still not printing the other values (255, 32, -1, -255)
回答1:
If the fixups suggested by BLUEPIXY and myself are made, the code will produce the answer with the bits in the correct sequence.
#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;
}
Output:
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
The output is not reversed, despite what BLUEPIXY says in the comments.
The body of the loop could be converted into a function such as void print_binary(unsigned value)
with minimal effort, and that would then be called from a loop. The edited question with more or less the same loop written out six(!) times is a travesty — do not write code like that. When you copy'n'paste code like that, there is a function waiting to be written.
#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;
}
}
This function can be used to print the binary representation of an unsigned int
without adding any decoration. It can be used reasonably generally. In this specific context, you might write a wrapper function
to handle the other formatting:
void fmt_binary(int value)
{
printf("Binary representation of %3d: ", value);
print_binary((unsigned)value);
putchar('\n');
}
The cast is not necessary as long as you have a prototype for print_binary()
in scope. From C99 onwards, you must have a function declaration present, but that doesn't have to be a prototype. However, compiling without prototypes present is silly. And you should find the options that ensure your compiler complains if you try to skimp. For GCC, you might use:
gcc -std=c11 -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes …
You might or might not add -Wold-style-declaration
or -Wold-style-definition
depending on the code base you're dealing with and the version of GCC you're using (and on how careless you are in writing code). There are other options you'd consider, like -Wshadow
, but if your code compiles cleanly with what's shown, it is unlikely to run into many problems that aren't logic problems.
With fmt_binary()
defined, you can write 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;
}
Example output might be:
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
来源:https://stackoverflow.com/questions/46780484/binary-print-not-working-in-c