我正在编写一个程序来将整数转换为32位二进制。问题在于输出 - 它倒退了。
#include <stdio.h>
int main(){
long number, binary, num2;
printf("Enter an integer: ");
scanf("%ld", &number);
for (num2 = (number * 2) / 2; num2 > 0; num2 /= 2){
binary = num2 % 2;
printf("%ld", binary);
}
putchar('\n');
return 0;
}
所以如果我把&#39; 6&#39;它显示为011,它必须是110
另外,如何输出其余的&#39; 0?因此,在这种情况下,整个输出将是:
00000000 00000000 00000000 00000110
由于
答案 0 :(得分:1)
您从右侧开始计算数字,这就是您的输出首先显示最右侧数字的原因。这是一种从左侧开始的方式,使用位掩码,并且不会将您的值转换为无符号,这可能会改变位:
#include <stdio.h>
#include <limits.h>
int main()
{
long number;
if ( 1 != scanf("%ld", &number) )
return 1;
// sign bit (cannot use 1L left-shift as that causes UB)
putchar( '0' + (number < 0) );
// value bits
for (long bit = 1L << (CHAR_BIT * sizeof number - 2); bit; bit >>= 1)
putchar( '0' + !!(number & bit) );
putchar('\n');
}
答案 1 :(得分:0)
如果要打印二进制数,则应向后打印这些位。看这个:
6(10):
6/2 = 3,rem =&gt; 0
3/2 = 1,rem =&gt; 1
1/2 = 0,rem =&gt; 1
因此6(2)= 110
您正在打印正向订单,它将为您提供011.所以您应该将二进制位保留在一个变量中并最终打印回来。
试试这个
#include <stdio.h>
int main(){
long number, binary, num2;
int i = 0, j;
char num[100];
printf("Enter an integer: ");
scanf("%ld", &number);
while (number != 0){
num[i] = num2 % 2;
number /= 2;
i++;
}
for (j = 0; j < 32; j++) {
if (j > i) {
printf("0");
}
else {
printf("%c", num[i]);
i--
}
}
printf("\n");
return 0;
}
答案 2 :(得分:0)
只需按照您希望输出的顺序检查位。我已经使用unsigned long
转换来进行移位,因为将有符号值的位移位到符号位的结果是未定义的。
#include <stdio.h>
#include <limits.h>
int main() {
long number = 0;
int i;
printf("Enter an integer: ");
scanf("%ld", &number);
for(i=0; i<sizeof(number)*CHAR_BIT; i++) {
if (number < 0)
printf ("1");
else
printf ("0");
if ((i % CHAR_BIT) == CHAR_BIT - 1)
printf (" ");
number = (long)((unsigned long)(number) << 1);
}
printf("\n");
return 0;
}
节目输出:
Enter an integer: 6
00000000 00000000 00000000 00000110
答案 3 :(得分:0)
使用递归函数要容易得多:
#include <stdio.h>
#include <stdint.h>
void printInBinary(long num, int bit)
{
if ( bit >= 8*sizeof(num) )
{
return;
}
printInBinary(num/2, bit+1);
printf("%ld", num%2);
if ( bit%8 == 0 )
{
printf(" ");
}
if ( bit == 0 )
{
printf("\n");
}
}
int main()
{
int y = 31;
uint32_t x1 = (1 << y );
uint32_t x2 = (1u << y );
printf("x1: %u\n", x1);
printInBinary(x1, 0);
printf("x2: %u\n", x2);
printInBinary(x2, 0);
}
输出:
x1: 2147483648 00000000 00000000 00000000 00000000 10000000 00000000 00000000 00000000 x2: 2147483648 00000000 00000000 00000000 00000000 10000000 00000000 00000000 00000000
PS 如果您使用uint32_t
代替num
代替long
,您将获得32位输出。
答案 4 :(得分:0)
#include <stdio.h>
int main(){
// Assuming 32 bit architecture.
unsigned long number;
// Initialize a null-terminated char-array
// of zeros (ASCII value 48).
char binary[33] = {[0 ... 31] = 48, [32] = 0};
printf("Enter an integer: ");
scanf("%lu", &number);
for(int i = 0; number > 0; ++i) {
binary[31 - i] = number % 2;
number /= 2;
}
printf("%s\n", binary);
return 0;
}
答案 5 :(得分:0)
这是一个简单的可移植的32位数字实现:
#include <stdio.h>
#include <string.h>
int main() {
long input; // at least 32 bits
unsigned long number;
int i;
printf("Enter an integer: ");
if (scanf("%ld", &input) != 1)
return 1;
/* copy the bit pattern to an unsigned long */
memcpy(&number, &input, sizeof number);
for (i = 32; i-- > 0;) {
if (i > 0 && (i & 7) == 0)
putchar(' ');
putchar('0' + (int)((number >> i) & 1));
}
putchar('\n');
return 0;
}