Question

The main problem I'm having is to read out values in binary in C++ (python had some really quick/easy functions to do this)

I just need the same. So at the moment I have:

ValWord< uint32_t> data1=//[SOME READ FUNCTION]

When I use cout << data1; It gives me a number e.g 2147581953

I want this to be in binary and eventually each "bit" needs to be in its own bin including all '0's e.g:

for (int i = 31; i >= 0; i--) {
      cout << binary[i];
    }  

Would give me this 32 bit long binary number. When I've had it as a straight forwward int, I've used:

    int data[32];
    bitset<32>(N) = data1;

    for(int i=31; i >=0; i--) {
      data[i]=(bitset<32>(N[i]).to_ulong());
    }

    for (int i = 31; i >= 0; i--) {
      cout << data[i];
    }  

But this just gives me error messages. Any ideas?

Was it helpful?

Solution

Maybe this:

#define CPlusPlus11 0

#if CPlusPlus11
int main()
{
    std::uint32_t value(42);
    std::bitset<32> bits(value);
    std::cout << bits.to_string() << std::endl;
    // Storing integral values in the string:
    for(auto i: bits.to_string(char(0), char(1))) {
        std::cout << (int)i;
    }
    std::cout << std::endl;
    return 0;
}
#else
int main()
{
    std::uint32_t value(42);
    std::bitset<32> bits(value);
    std::cout << bits.to_string() << std::endl;
    char data[32];
    for(unsigned i = 0; i < 32; ++i) {
        data[i] = bits[i];
    }
    for(unsigned i = 32; i; --i) {
        std::cout << int(data[i-1]);
    }
    std::cout << std::endl;
    return 0;
}
#endif

Note: Your expressions bitset<32>(N) = data1 and bitset<32>(N[i]) are code smell.

OTHER TIPS

In general, transforming a number into a string for a given base, is quite ubiquitous:

#include <cassert>
#include <string>

static char const Digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static size_t const DigitsSize = sizeof(Digits) - 1;

static size_t const BufferSize = 32;

std::string convert(unsigned number, unsigned base) {
    assert(base >= 2 and base <= DigitsSize);

    char buffer[BufferSize] = {};
    char* p = buffer + BufferSize;

    while (number != 0) {
        *(--p) = Digits[number % base];
        number /= base;
    }

    return std::string(p, (buffer + BufferSize) - p);
}

Note: BufferSize was computed for the minimum base of 2, base 1 and base 0 are non-sensical.

Note: if the number can be negative, the simplest is to test for it beforehand, and then use its opposite; a special caveat is that the opposite of the minimum value of a 32 bits integer cannot be represented by a 32 bits integer in base 2.

I have some simple functions i use for this, using stl, here is one for binary:

#include <iostream>
#include <algorithm>

using namespace std;

string binary( unsigned long n )
  {
  string result;

  do result.push_back( '0' + (n & 1) );
  while (n >>= 1);

  reverse( result.begin(), result.end() );
  return result;
  }

int main()
{
    cout << binary(1024) << endl;
   cout << "Hello World" << endl; 

   return 0;
}

Hope this is of use to you!

Let me know if you need something more performant and I can try to rustle up some Assembler code for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top