[英]How to convert decimal to binary in 64 bits?
so I have this code 所以我有这个代码
int main()
{
int n, c, k;
printf("Enter an integer\n");
scanf("%d", &n);
printf("%d in binary is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
It converts decimal into binary but only in 32 bits. 它将十进制转换为二进制,但只能转换为32位。 When I change it into 64 bits it doesn't work (it seems like it just doubles the result from 32 bits). 当我将其更改为64位时,它不起作用(似乎只是将结果从32位加倍)。 At the same time it works fine with 8 or 4 bits etc. What am I doing wrong? 同时使用8位或4位等可以正常工作。我在做什么错?
It converts decimal into binary but only in 32 bits. 它将十进制转换为二进制,但只能转换为32位。 When I change it into 64 bits it doesn't work (it seems like it just doubles the result from 32 bits). 当我将其更改为64位时,它不起作用(似乎只是将结果从32位加倍)。
The problem is here. 问题在这里。
int n, c, k;
printf("Enter an integer\n");
scanf("%d", &n);
n
is an int
which can be as small as 16 bits. n
是一个int
,可以小至16位。 It could be 64 bits, but it's probably 32 bits. 可能是64位,但可能是32位。 When you try to enter a 64 bit number you'll get garbage. 当您尝试输入64位数字时,您将得到垃圾。
#include <stdio.h>
int main() {
int n;
printf("sizeof(int) == %zu\n", sizeof(int));
printf("Enter an integer\n");
scanf("%d", &n);
printf("n = %d\n", n);
}
$ ./test
sizeof(int) == 4
Enter an integer
12345678901
n = -539222987
Instead, you can use a long long int
which has a minimum size of 64 bits or int64_t
from stdint.h
which is exactly 64 bits. 相反,您可以使用long long int
,其最小大小为64位,或者使用stdint.h
中的int64_t
(恰好为64位)。 I have a preference for using the explicit width types in code that requires a specific width to make it more obvious to the reader. 我偏爱在代码中使用显式宽度类型,该类型需要特定的宽度以使其对读者更加明显。
long long int
uses %lld
for scanf
and printf
while int64_t
uses macros from inttypes.h . long long int
将%lld
用于scanf
和printf
而int64_t
使用inttypes.h中的宏 。 The code below takes advantage of C automatically concatenating constant strings; 下面的代码利用C自动连接常量字符串的优势; "foo" "bar"
and "foobar"
are equivalent. "foo" "bar"
和"foobar"
是等效的。
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main() {
int64_t n;
printf("Enter an integer\n");
scanf("%"SCNd64, &n);
printf("n = %"PRId64"\n", n);
}
$ ./test
Enter an integer
12345678901
n = 12345678901
The problem is your n & k variables' data type. 问题是您的n&k变量的数据类型。 They are integer. 它们是整数。 Check your platform's data type size using sizeof(int). 使用sizeof(int)检查平台的数据类型大小。
When you change to 64-bits, these variables can't hold 64-bit values. 当您更改为64位时,这些变量将无法保存64位值。
HTH! HTH!
the following code is one way to handle the problem: 以下代码是解决问题的一种方法:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
long long unsigned int n;
long long int c;
long long unsigned int k;
printf("Enter an integer\n");
if( 1 != scanf("%llu", &n) )
{
perror( "scanf for 64 bit number failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
printf("%llu in binary is:\n", n);
for (c = 63; c >= 0; c--)
{
k = n >> c;
if (k & 1)
putc('1', stdout);
else
putc('0', stdout);
}
printf("\n");
return 0;
} // end function: main
and here is the output from a typical run of the program: 这是该程序典型运行的输出:
Enter an integer
6789097860397846
6789097860397846 in binary is:
0000000000011000000111101010011000000110010100000111101100010110
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.