0

I'm trying to code an algorithm that will save to file as binary strings every integer in a range. Eg, for the range 0 to 7:

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

Note that the leading zeros and spaces between digits are essential.

What I cant figure out how to do in a simple way is to convert the integers to binary numbers represented by bool []s (or some alternate approach).

EDIT

As requested, my solution so far is:

const int NUM_INPUTS = 6;
bool digits[NUM_INPUTS] = {0};
int NUM_PATTERNS = pow(2, NUM_INPUTS);

for(int q = 0; q < NUM_PATTERNS; q++)
{
    for(int w = NUM_INPUTS -1 ; w > -1 ; w--)
    {

        if( ! ((q+1) % ( (int) pow(2, w)))  )
            digits[w] = !digits[w];

        outf << digits[w] << " ";
    }

    outf << "\n";
}

Unfortunately, this is a bit screwy as the first pattern it gives me is 000001 instead of 000000.

This is not homework. I'm just coding a simple algorithm to give me an input file for training a neural network.

5
  • Is this homework? If so, tag it as such Commented Apr 4, 2012 at 15:01
  • I think you want to do some base conversion instead. Google C++ base 2 Commented Apr 4, 2012 at 15:01
  • Binary data is not the same as an array of bools... are you supposed to create your own binary representation of numbers too? Commented Apr 4, 2012 at 15:02
  • @Lirik the array of bools is what I'm after. Commented Apr 4, 2012 at 15:34
  • @Shahbaz this isn't homework. I just need a program to automatically generate an input file for a neural network. Commented Apr 4, 2012 at 15:34

3 Answers 3

4

Don't use pow. Just use binary math:

const int NUM_INPUTS = 6;
int NUM_PATTERNS = 1 << NUM_INPUTS;

for(int q = 0; q < NUM_PATTERNS; q++)
{
    for(int w = NUM_INPUTS -1 ; w > -1; w--)
    {
        outf << ((q>>w) & 1) << " ";
    }
    outf << "\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

How does 1 << 6 = 64? I'm not familiar with the << or >> operators.
x << n shifts the value x to the left by n bits. If you prefer, it doubles the value x, n times. 1, in binary, is "0000_0001". 1 Shifted left 6 times is "0100_0000". In base 10, that is 64. Similarly x >> n shifts the value x to the right by n bits (or halves x, n times). en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts
3

Note: I'm not providing code, but merely a hint because the question sounds like homework

This is quite easy. See this example:

number = 23
binary representation = 10111
first  digit = (number   )&1 = 1
second digit = (number>>1)&1 = 1
third  digit = (number>>2)&1 = 1
fourth digit = (number>>3)&1 = 1
fifth  digit = (number>>4)&1 = 1

Alternatively written:

temp = number
for i from 0 to digits_count
    digit i = temp&1
    temp >>= 1

Note that the order of digits taken by this algorithm is the reverse of what you want to print.

3 Comments

int compare = 1L << (digits_count - 1); digit i = (temp & compare ? 1 : 0); temp <<= 1;
In case you're wondering, that'll let you take the digits in the order you wanted...
@OrgnlDave, I wasn't wondering, it was obvious. I only intend to give the OP a hint though, and let him tackle with the problem himself.
1

The lazy way would be to use std::bitset.

Example:

#include <bitset> 
#include <iostream>

int main()
{
  for (unsigned int i = 0; i != 8; ++i){
    std::bitset<3> b(i);
    std::cout << b << std::endl;
  }
}

If you want to output the bits individually, space-separated, replace std::cout << b << std::endl; with a call to something like Write(b), with Write defined as:

template<std::size_t S>
void Write(const std::bitset<S>& B)
{
  for (int i = S - 1; i >= 0; --i){
    std::cout << std::noboolalpha << B[i] << " ";
  }
  std::cout << std::endl;
}

1 Comment

@MattMunson: Now that I have access to a real computer with a real screen and a real keyboard, I added an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.