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

时间:2017-10-22 16:15:50

标签: c++11 visual-c++

**#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我非常感谢您提供的任何帮助。 :) **

1 个答案:

答案 0 :(得分:0)

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

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