4

I want to make a simple program that will take number of bits from the input and as an output show binary numbers, written on given bits (example: I type 3: it shows 000, 001, 010, 011, 100, 101, 110, 111). The only problem I get is in the second for-loop, when I try to assign variable in bitset<bits>, but it wants constant number. If you could help me find the solution I would be really greatful. Here's the code:

#include <iostream>
#include <bitset>
#include <cmath>

using namespace std;

int main() {
    int maximum_value = 0,x_temp=10;
    //cin >> x_temp;
    int const bits = x_temp;

    for (int i = 1; i <= bits; i++) {
        maximum_value += pow(2, bits - i);
    }
    for (int i = maximum_value; i >= 0; i--)
        cout << bitset<bits>(maximum_value - i) << endl;
    return 0;
}
januszysko
  • 41
  • 1
  • 3
  • Possible duplicate of [Convert integer to bits](http://stackoverflow.com/questions/6038718/convert-integer-to-bits) – Jacob H Oct 19 '16 at 20:29
  • The answer to that question depends on the number being known at compile time. Not suitable for what the OP needs. – R Sahu Oct 19 '16 at 20:32
  • Unless you are required to use `std::bitset`, you can use the solution in http://stackoverflow.com/questions/23173261/int-to-binary-conversion-explanation. – R Sahu Oct 19 '16 at 20:34
  • I wanted to try doing it the easy way. I am able to do it other ways. I just wanted to know if there is a glitch I can use to make it work – januszysko Oct 19 '16 at 20:38

2 Answers2

1

A numeric ("non-type", as C++ calls it) template parameter must be a compile-time constant, so you cannot use a user-supplied number. Use a large constant number (e.g. 64) instead. You need another integer that will limit your output:

int x_temp = 10;
cin >> x_temp;
int const bits = 64;
...

Here 64 is some sort of a maximal value you can use, because bitset has a constructor with an unsigned long long argument, which has 64 bits (at least; may be more).

However, if you use int for your intermediate calculations, your code supports a maximum of 14 bits reliably (without overflow). If you want to support more than 14 bits (e.g. 64), use a larger type, like uint32_t or uint64_t.


A problem with holding more bits than needed is that the additional bits will be displayed. To cut them out, use substr:

cout << bitset<64>(...).to_string().substr(64 - x_temp);

Here to_string converts it to string with 64 characters, and substr cuts the last characters, whose number is x_temp.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • Thank you, I guess I understand, but can you tell me how can I limit my output when it comes to leading 0s? – januszysko Oct 19 '16 at 20:43
  • So you don't want to display leading zeros? You might want to ask a different question for that (unless you figure it out by yourself somehow). – anatolyg Oct 19 '16 at 20:47
  • I mean I do, but only for the limited value. I don't want to show the number on 64bits but on 10 for example – januszysko Oct 19 '16 at 20:51
  • My question concerns your statement "You need another integer that will limit your output". I don't know how to implement it – januszysko Oct 19 '16 at 20:51
  • OK! Didn't understand at first. Updated my answer. – anatolyg Oct 19 '16 at 20:59
0

You have to define const int bits=10; as a global constant :

#include <iostream>
#include <math.h>
#include <bitset>

using namespace std;
const unsigned bits=10;
int main() {
    int maximum_value = 0,x_temp=10;

    for (int i = 1; i <= bits; i++) {
        maximum_value += pow(2, bits - i);
    }
    for (int i = maximum_value; i >= 0; i--)
        cout << bitset<bits>(maximum_value - i) << endl;
    return 0;
}

enter image description here

Dali
  • 344
  • 1
  • 10