[英]convert negative decimal number to binary number
I tried to convert a negative decimal number into a binary number and this code perfectly works on my computer, but the code doesn't work another computer. 我试图将一个负的十进制数转换为二进制数,并且此代码在我的计算机上可以正常使用,但是该代码在另一台计算机上不起作用。
I didn't get how it is possible. 我不知道怎么可能。 What is wrong in my code? 我的代码有什么问题?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void decTobin(int dec, int s)
{
int b[s], i = 0;
while (dec >= 0 && i != s - 1) {
b[i] = dec % 2;
i++;
dec /= 2;
}
int j = i;
printf("%d", dec);
for (j = i - 1; j >= 0; j--) {
if (b[j] == NULL)
b[j] = 0;
printf("%d",b[j]);
}
}
void ndecTobin(int dec, int s)
{
int b[s], i = 0, a[s], decimal, decimalvalue = 0, g;
while (dec >= 0 && i != s-1) {
b[i] = dec % 2;
i++;
dec /= 2;
}
int j = i;
printf("%d",dec);
for (j = i - 1; j >= 0; j--) {
if (b[j] == NULL)
b[j] = 0;
printf("%d",b[j]);
}
printf("\n");
a[s - 1] = dec;
for (j = s - 2; j >= 0; j--) {
a[j] = b[j];
}
for (j = s - 1; j >= 0; j--) {
if (a[j] == 0)
a[j] = 1;
else
a[j] = 0;
printf("%d",a[j]);
}
for (g = 0; g < s; g++) {
decimalvalue = pow(2, g) * a[g];
decimal += decimalvalue;
}
decimal = decimal + 1;
printf("\n%d\n", decimal);
decTobin(decimal, s);
}
int main()
{
int a, b;
printf("enter a number: ");
scanf(" %d", &a);
printf("enter the base: ");
scanf("%d", &b);
ndecTobin(a, b);
}
decimal
and int b[s]
not initialized. decimal
和int b[s]
未初始化。
By not initializing decimal
to 0, it might have the value of 0 on a machine one day and quite different results otherwise. 如果不将decimal
初始化为0,则一天中一台机器上的值可能为0,否则结果完全不同。
void decTobin(int dec, int s) {
// while loop does not set all `b`,but following for loop uses all `b`
// int b[s], i = 0;
int b[s] = { 0 }; // or int b[s]; memset(b, 0, sizeof b);
int i = 0;
}
void ndecTobin(int dec, int s) {
int b[s], i = 0, a[s], decimal, decimalvalue = 0, g;
decimal = 0;
...
decimal += decimalvalue;
}
Minor points: 次要点:
1) if (b[j] == NULL) b[j] = 0;
1) if (b[j] == NULL) b[j] = 0;
is strange. 很奇怪 NULL
is best used as a pointer, yet code is comparing b[j]
, an int
to a pointer. 最好将NULL
用作指针,但是代码会将b[j]
(一个int
与一个指针)进行比较。 Further, since NULL
typically has the arithmetic value of 0, code looks like if (b[j] == 0) b[j] = 0;
此外,由于NULL
通常具有算术值0,因此代码看起来好像if (b[j] == 0) b[j] = 0;
. 。
2) decTobin()
is challenging to follow. 2) decTobin()
具有挑战性。 It certainly is only meant for non-negative dec
and s
. 当然,它仅适用于非负dec
和s
。 Candidate simplification: 候选人简化:
void decTobin(unsigned number, unsigned width) {
int digit[width];
for (unsigned i = width; i-- > 0; ) {
digit[i] = number % 2;
number /= 2;
}
printf("%u ", number); // assume this is for debug
for (unsigned i = 0; i<width; i++) {
printf("%u", digit[i]);
}
}
It looks like you are just printing the number as a binary representation. 看来您只是将数字打印为二进制表示形式。 If so this version would work. 如果是这样,这个版本将工作。
void print_binary(size_t n) {
/* buffer large enough to hold number to print */
unsigned buf[CHAR_BIT * sizeof n] = {0};
unsigned i = 0;
/* handle special case user calls with n = 0 */
if(n == 0) {
puts("0");
return;
}
while(n) {
buf[i++] = n % 2;
n/= 2;
}
/* print buffer backwards for binary representation */
do {
printf("%u", buf[--i]);
} while(i != 0);
}
If you don't like the buffer, you can also do it using recursion like this: 如果您不喜欢缓冲区,也可以使用递归来实现,如下所示:
void using_recursion(size_t n)
{
if (n > 1)
using_recursion(n/2);
printf("%u", n % 2);
}
Yet another way is to print evaluating most significant bits first. 另一种方法是首先打印评估最高有效位。 This however introduces issue of leading zeros which in code below are skipped. 但是,这会导致前导零的问题,下面的代码将被跳过。
void print_binary2(size_t n) {
/* do not print leading zeros */
int i = (sizeof(n) * 8)-1;
while(i >= 0) {
if((n >> i) & 1)
break;
--i;
}
for(; i >= 0; --i)
printf("%u", (n >> i) & 1);
}
Different OS/processor combinations may result in C compilers that store various kinds of numeric variables in different numbers of bytes. 不同的OS /处理器组合可能会导致C编译器将各种数值变量存储在不同数量的字节中。 For instance, when I first learned C (Turbo C on a 80368, DOS 5) an int
was two bytes, but now, with gcc on 64-bit Linux, my int
is apparently four bytes. 例如,当我第一次学习C(在80368,Turbo C在80368,DOS 5上)时,一个int
是两个字节,但是现在,在64位Linux上使用gcc时,我的int
显然是四个字节。 You need to include some way to account for the actual byte length of the variable type: unary operator sizeof(foo)
(where foo is a type, in your case, int
) returns an unsigned integer value you can use to ensure you do the right number of bit shifts. 您需要提供某种方法来解释变量类型的实际字节长度:一元运算符sizeof(foo)
(其中foo是一种类型,在您的情况下为int
)返回一个无符号整数值,您可以使用该整数值来确保执行正确的位数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.