c ++为什么下面的代码不能像我预期的那样工作

时间:2022-04-05 12:57:35

** #1 i really appreciate any help you can provide. :) **

**#1我非常感谢您提供的任何帮助。 :) **

//Decimal_to_Binary

#include<iostream>
#include<Windows.h>

using namespace std;

int main() {

    //Variables:
    int num_b2[8];
    unsigned int delay_x = 0, num_b10 = 0, x = 0;

    //Input:
    cout << "\n Enter a decimal(integer) number (0~31) \n To get its equivalent binary number : "; cin >> num_b10; cout << "\a";
    x = num_b10;
    system("cls");

    //Function:
    while (x > 0) {
        for (unsigned short int i = 7; i > 0; i -= 1) {
            num_b2[i] = x % 2;
            x /= 2;
        }
    }

    //Output:
    Sleep(delay_x);
    for (unsigned short int i = 0; i<sizeof(num_b2) / 4; i += 1) {
        cout << num_b2[i];
    }
    cout << "\n\n\n";


    system("pause");
    return 0;
}

** #1 i really appreciate any help you can provide. :) **

**#1我非常感谢您提供的任何帮助。 :) **

1 个解决方案

#1


0  

Here is a simple code for decimal to binary conversion

这是一个简单的十进制到二进制转换代码

    <code>int num = 5;
    for (int i = 31; i>=0 ; i--)
    {
        if((1 << i) & num)
           cout << 1;
        else
           cout << 0;
    }</code>

#1


0  

Here is a simple code for decimal to binary conversion

这是一个简单的十进制到二进制转换代码

    <code>int num = 5;
    for (int i = 31; i>=0 ; i--)
    {
        if((1 << i) & num)
           cout << 1;
        else
           cout << 0;
    }</code>