2016-09-30 17 views
1

私は初心者のC++で始まり、10進バイトを2進数に変換しようとしています。しかし、構文やロジックに問題がありますが、わかりません。デバッグしようとすると、エラーはuserValue5の周りにあると信じていますが、私はなぜそれがわかりません。どのようなヒントも高く評価されており、VS2015を使用しています。バイナリへの十進数のC++

#include "stdafx.h" 
    #include <iostream> 
    #include <stdint.h> 
    #include <cmath> 

    //finds where each column is a 0 or a 1 
    int binaryDigit(uint16_t y, uint16_t power) 
    { 
    if ((y/(pow(2, power))) > 1) 
    return 1; 
    else 
    return 0; 
    } 

    int difference(uint16_t y, int x, uint16_t power) 
    { 
    if (x == 1) 
    return y - pow(2, power); 
    else 
    return y; 
    } 

    //takes a decimal byte and turns it into binary 
    int main() 
    { 
    using namespace std; 
    cout << "Please insert a number between 0 and 255 so that I can convert it to binary: "; 
    uint16_t userValue(0), power(7); 
    cin >> userValue; 

    int firstDigit = binaryDigit(userValue, power); 
    uint16_t userValue2 = difference(userValue, firstDigit, power); 
    --power; 

    int secondDigit = binaryDigit(userValue2, power); 
    uint16_t userValue3 = difference(userValue2, secondDigit, power); 
    --power; 

    int thirdDigit = binaryDigit(userValue3, power); 
    uint16_t userValue4 = difference(userValue3, thirdDigit, power); 
    --power; 

    int fourthDigit = binaryDigit(userValue4, power); 
    uint16_t userValue5 = difference(userValue4, thirdDigit, power); 
    --power; 

    int fifthDigit = binaryDigit(userValue5, power); 
    uint16_t userValue6 = difference(userValue5, thirdDigit, power); 
    --power; 

    int sixthDigit = binaryDigit(userValue6, power); 
    uint16_t userValue7 = difference(userValue6, thirdDigit, power); 
    --power; 

    int seventhDigit = binaryDigit(userValue7, power); 
    uint16_t userValue8 = difference(userValue7, thirdDigit, power); 
    --power; 

    int eigthDigit = binaryDigit(userValue8, power); 


    cout << "The number " << userValue << " in binary is "; 
    cout << firstDigit << secondDigit << thirdDigit << fourthDigit << " " << fifthDigit << sixthDigit << seventhDigit << eigthDigit << endl; 


    return 0; 
    } 
+0

エラーメッセージはありますか? – HazemGomaa

+1

'<<' and '>>'演算子を読むことをお勧めします。あなたの人生をはるかに簡単にしてください。 – user4581301

+0

これは実際にエラーコードではありません。それはちょうど第5列の周りに間違ったバイナリの言葉を出力します – Electickid2020

答えて

2

bitsetあなたの友人です。

#include <bitset> 
#include <iostream> 

using namespace std; 

int main() 
{ 
    int userValue = 0; 

    cout << "Please insert a number between " << INT_MIN << " and " << INT_MAX << " so that I can convert it to binary: "; 
    cin >> userValue; 

    cout << bitset<32>(userValue) << endl; 

    return 0; 
} 
0

std::string intToBinaryString(int x) 
{ 
    // a useful class that allows easy concatenation 
    std::stringstream ss; 


    // create an integer that only has a 1 bit in its top position. 
    unsigned int mask = 1 << (sizeof(int) * 8 - 1); 

    // for each bit in x, starting at the top, check to see if its non zero... 

    for(int i = 0; i <sizeof(int) * 8; ++i) 
    { 
     // see if x's (top - i)'th bit is non zero 
     bool nonZeroBit = x & mask; 

     // assign the (top - i)'th "bit" of ss to be '1' if non zero and '0' otherwise 
     ss << (nonZeroBit ? '1' : '0'); 

     // shift the mask down by 1. 
     mask >>= 1; 
    } 

    // What we get on the first iteration is 
    // 
    // mask = 10000...0000 
    // & x = 10101...0010 
    // ----------------------------- 
    // nonZero = 10000...0000 = true 
    // 
    // the second iteration is 
    // 
    // mask = 01000...0000 
    // & x = 10101...0010 
    // ----------------------------- 
    // nonZero = 00000...0000 = false 
    // 
    // the third iteration is 
    // 
    // mask = 00100...0000 
    // & x = 10101...0010 
    // ----------------------------- 
    // nonZero = 00100...0000 = true 
    // 
    // ... 
    // 
    // then we just add these results into ss 

    // finally, convert it into a normal string 
    return ss.str(); 
} 
1

が、私はそれを考え出した...ビットセットを移動するための方法であるが、一般的には、多分これはより便利です。同じコードをコピーして貼り付けて時間を節約したとき、引数を編集するのを忘れていたので、それが正しく機能していなかったのです。私はそれを修正し、それは素晴らしい動作します。指数表やビットマスキングのようなものをコメントし投稿した皆さん、ありがとう。私はたくさんのことを学んだし、もっと多くのプログラムを書くのを待つことはできません。

+0

前回の答えで触れたように、 ' 'を使うのがずっと速くて簡単です。 – FluorescentGreen5

関連する問題