Skip to main content
4 of 5
Rollback to Revision 2
jdt
  • 2.4k
  • 5
  • 20

Formatting conventions

It is your program and you are free to format it any way you want to, but sticking to conventions makes it easier for others to read.

I personally prefer my function definitions and for loops on one line unless they are very long. Most people use either camelCase or snake-case with variables starting with a lower case letter.

ZeroOrOne

If you used an unsigned int for Number then you would not have to worry about it being negative and simplified it to the following:

*(Array + I) = Number % 2;

Showing intention

I would suggest that you rather use the following:

Number /= 2;

Instead of shifting the bits:

Number = (Number >> 1);

The compiler is smart enough to optimize this for you.

std::bitset

Since C++11 we can use std::bitset do all of this for us very neatly like the following:

#include <bitset>

int main()
{
    std::bitset<32> bs(5);
}
jdt
  • 2.4k
  • 5
  • 20