【发布时间】:2018-06-24 10:40:43
【问题描述】:
// C++ program to convert a decimal
// number to binary number
#include <iostream>
using namespace std;
// function to convert decimal to binary
void decToBinary(int n)
{
// array to store binary number
int binaryNum[1000];
// counter for binary array
int i = 0;
while (n > 0) {
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
}
// Driver program to test above function
int main()
{
int n = 2;
decToBinary(n);
return 0;
}
我想知道如何覆盖 8 位。因为如果你输入 2,答案将是 10,但我想实现它,所以它可以变成 00000010
【问题讨论】:
-
哎呀,抱歉,这是个意外。
-
我想知道如何覆盖 8 位。您对 8 位或 8 位的倍数感兴趣吗?
-
是的,因为所有十进制数都必须转换为 8 位。
-
顺便提一下,您不需要 1000 个整数来存储转换结果。只要您的参数是一个 int,您永远不需要超过
CHAR_BIT * sizeof(int),它最终在 x86 上等于 32。