C++ Array of binary numbers to int value

Viewed 2168

everyone ! Now I am stuck here with a problem...

Problem:

Given a binary number represented as an array, write a function that takes the array and its size as a parameter, and returns the integer value. You may assume that there are at least 1 and no more than 30 numbers in the array and that all the values are either 0 or 1. The array is ordered with most significant binary digit at the start (index 0) and the least significant digit at the end.

Signature: int binary_to_number(int binary_digits[], int number_of_digits)

The function I have wrote is at the bottom. It works fine to return the int value for number_of_digits <= 10.

As you can see, that the question says "You may assume that there are at least 1 and no more than 30 numbers in the array"

My question is, how can I fix my function to return proper int value even if there is more than 10 numbers (perhaps 30 numbers)?

OR, Should I be approaching the problem different way? and if so, what should I do?

#include<iostream>
#include<string>

int binary_to_number(int binary_digits[], int number_of_digits){
    std::string bin_str;

    for (int i=0; i<number_of_digits; i++) {
         if (binary_digits[i] == 0) {
             bin_str = "0" + bin_str;
         } else if (binary_digits[i] == 1) {
             bin_str = "1" + bin_str;
           } 
    }
    int bin_int = std::stoi (bin_str);
return bin_int;
}
3 Answers

first of all u are using 0 and 1's in your string so assume your array is of 30 length then string will also be of 30 length but max size of long long even is 18 digits you will get overflows

int bin_int = std::stoi (bin_str);

above is completely wrong...

You must convert the binary to integer the old way in powers of 2 then if u want to convert it to int no problem but i would suggest you to do like this instead else your approach will be messy

num += pow(2,i); // watch out for overflows if u experience such; then 
                 // better use bit manipulation for 2's power eg.(1<<i)

here no problem just the old way.....But it's starting from LSB . From MSB you have to start from last of array as per your question

You may use this algorithm to do so:

int conversion(int array[], int len) {
    int output = 0;
    int power = 1;

    for (int i = 0; i < len; i++)
    {
        output += array[(len - 1) - i] * power;
        // output goes 1*2^0 + 0*2^1 + 0*2^2 + ...
        power *= 2;
    }

    return output;
}

A sample statement could be considered as:

int arr[16] = {1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1};
std::cout << conversion(arr, 16);

Then it should out:

39321

A beautiful representation for my code could be found here.

Hope it helps.

You are performing more operations than necessary.

int result = 0;
int index = number_of_digits - 1;
for (int i = 0U; i < number_of_digits; ++i)
{
  result = (result << 1) | binary_digits[index];
  --index;
}

The result is initialized to zero.
For each bit in the array:
Left shift the result by one bit to make room for the new bit.
Arithmetic OR the new bit.

Try out the algorithm by hand.

Related