0

I've created a method to generate random binary numbers with n-digits up to 256 digits. In order to continue with my program I need to take the vector with my binary value and put it into a decimal array and then convert that number into a int decimal. Here's the following function that creates the random binary vector and my attempt to convert to decimal. I'm having trouble converting my binary vector to a decimal.

int random_binary(int n, int m){

   vector<int> binary;

   for(int i = 0; i < n; i++)
   {
      m = rand() % 2;

      binary.push_back(m);
   }

   binary.push_back(1);

   int j;

   for(j = 0; j < binary.size(); j++)
   {
       cout << binary[j];
   }
   cout <<"\n";


   int len = binary.size();
   int a = binary[len];   //having trouble right here

   int decimalValue = 0;

   for (int i = len-1; i>= 0; i--)
   {
       decimalValue = decimalValue + binary[i]*pow(2,len-i-1);
   }

   return decimalValue;
}

If anybody could help figure this out that would be much appreciated.

ClaytonTM9
  • 67
  • 8
  • Errr, numbers are numbers, number representations are number representations? I prefer base 20 BTW, having ten fingers and ten toes. – πάντα ῥεῖ Sep 23 '16 at 03:01
  • @ClaytonTM9 You have a major hypo it seems. – πάντα ῥεῖ Sep 23 '16 at 03:09
  • 1
    @ClaytonTM9 -- 1) `int len = binary.size(); int a = binary[len];` This is not valid C++. Arrays must be declared using a constant as the number of entries. 2) `pow(2,len-i-1)` -- Do not use `pow` if the [exponent will be an integer](http://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os) – PaulMcKenzie Sep 23 '16 at 03:10

1 Answers1

0

The code isn't all that bad, but it does have a few problems:

  • An int value can only hold 32 bits on most platforms, 31 bits if you only want non-negative values. So for 256 binary digits, you will need to use something else.

  • int a = binary[len]; //having trouble right here - I think @PaulMcKenzie interprets this as an attempt to declare an array of int with a variable number of elements (int a[len];). I see it as probably initializing an int variable to the element in binary one past the last entry. Either way is wrong -- but you never use a anyway, and I don't see any possible use for it. So just delete that line.

  • Not a bug, but you should understand that int decimalValue isn't decimal, it is an integer, which is most likely stored inside the computer as a set of bits, and only appears as a decimal number when converted and displayed a certain way, like when printed using std::cout.

  • In the sum loop, std::pow should not be used for integer values, but luckily the bit left shift operator can be used to get integer powers of 2:
    decimalValue = decimalValue + binary[i] * (1<<(len-i-1));

Christopher Oicles
  • 3,017
  • 16
  • 11
  • Thanks so much, question on the first bullet you pointed out. What would I need to use in order to hold values larger than 32 without it going to a negative value? – ClaytonTM9 Sep 23 '16 at 20:12
  • Well 31 bits gives you the numbers 0 to 2,147,483,647. The biggest common unsigned integer type is `unsigned long long`, with 64 bits on all platforms I'm aware of, so this gives you 0 to 18,446,744,073,709,551,615. For more bits, you might need to use an array or vector of values, and make your own math functions for it. `std::bitset` is a data type from the standard library which will hold any number of bits you want, limited by the capabilities of your platform. – Christopher Oicles Sep 23 '16 at 20:33