Skip to main content
deleted 219 characters in body
Source Link
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);
}

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);
}

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;

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);
}
Rollback to Revision 2
Source Link
jdt
  • 2.4k
  • 5
  • 20

In Africa, there is a saying that goes something like this, “How do you eat an elephant?”, “One bite at a time!” I like the way you figured this out by yourself, and the following comments are not meant to discourage you but only to point out some ways to improve your code.

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);
}

In Africa, there is a saying that goes something like this, “How do you eat an elephant?”, “One bite at a time!” I like the way you figured this out by yourself, and the following comments are not meant to discourage you but only to point out some ways to improve your code.

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);
}

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);
}
added 279 characters in body
Source Link
jdt
  • 2.4k
  • 5
  • 20

In Africa, there is a saying that goes something like this, “How do you eat an elephant?”, “One bite at a time!” I like the way you figured this out by yourself, and the following comments are not meant to discourage you but only to point out some ways to improve your code.

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);
}

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);
}

In Africa, there is a saying that goes something like this, “How do you eat an elephant?”, “One bite at a time!” I like the way you figured this out by yourself, and the following comments are not meant to discourage you but only to point out some ways to improve your code.

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);
}
deleted 1 character in body
Source Link
jdt
  • 2.4k
  • 5
  • 20
Loading
Source Link
jdt
  • 2.4k
  • 5
  • 20
Loading