0

Part of a program I'm writing involves getting a list of integers (e.g. 15, 18, 25) and converting each one to binary. I'm iterating through the list and using the following line of code to convert each one:

std::string binary = std::bitset<8>(v).to_string();

(the (v) is the integer I'm converting)
but the problem with this line of code is that it defines the length of the outputted binary string, so 2 would become "00000010" and 31 would become "00011111" of course I cant it make too low or else im going to have some trouble with larger numbers, but I want the length of each binary string to be equal to the real binary number (2 is "10", 31 is "11111"). I have my reasons for this.
So I tried replacing the <8> with an int that changes based on the number I'm trying to convert based on the following code:

int length_of_binary;
        if (v <= 1) {
            length_of_binary = 1;
        }
        else if (v <= 3) {
            length_of_binary = 2;
        }
        else if (v <= 8) {
            length_of_binary = 4;
        }
        else if (v <= 16) {
            length_of_binary = 5;
        }
        else if (v <= 32) {
            length_of_binary = 6;
        }

std::string binary = std::bitset<length_of_binary>(v).to_string();

The problem is that i get the following error when hovering over the (now under-waved) variable length_of_binary:
"+5 overloads. expression must have a constant value."

and the program won't compile. I even tried tricking the compiler by assigning the value of length_of_binary to a const int but it still won't work.
Is there a way to fix this? if not is there a piece of code/function that will give me what I need?

  • It's just as the compiler tells you. The specified value must be know at compile time. Hence, you cannot make it dependent on some runtime conditions. You might want to rethink your solution. – Dusteh Dec 11 '17 at 11:33
  • 2
    Why not convert as you are currently doing and then use substr to get the part you want? – falopsy Dec 11 '17 at 11:33
  • is there another way to do what I want? –  Dec 11 '17 at 11:33
  • @falopsy how would i remove the unwanted 0's from the front? –  Dec 11 '17 at 11:34
  • See https://stackoverflow.com/questions/22746429/c-decimal-to-binary-convertin. – Oscar de Leeuw Dec 11 '17 at 11:36
  • std::string binary = std::bitset<8>(v).to_string(); binary.substr(8-length_of_binary); should work. – falopsy Dec 11 '17 at 11:38
  • An alternative approach may be to repeatedly divide your number by 2 (or right-shift it by 1) while incrementing a count each time. When the number has been reduced to zero, the count gives you your length. Note that this will not work for negative values. – jsheeran Dec 11 '17 at 11:38
  • @jsheeran: Won't fix it. And can't fix it, because the probleme here really is that you cannot produce a `constexpr` value from a runtime variable (you can create `const` copies of user input, but that's insufficient - you need a compile time constant) – MSalters Dec 11 '17 at 12:01

1 Answers1

1

As already mentioned in the comments: the issue you face is that the value needs to be known at compile time (not runtime dependent).

Hence, you can use a fixed representation, for example std::bitset<N> convert it into a string like you have already done and then trim the leading zeros. It can be achieved like this:

std::string text = std::bitset<8>(25).to_string(); // binary representation
text.erase(0, text.find_first_not_of('0')); // zeroes trimmed
std::cout << text; // prints out: 11001

Note that this is just an example. You would still have to handle the case of 0 and think whether your input data won't exceed an 8 bit representation.

Nevertheless, with this approach you have no need for the length_of_binary variable and the related if-else sections - which simplifies the code a lot.

Dusteh
  • 1,496
  • 16
  • 21