Portfolio

Binary_Representation_Of_Integer


/*Write a function that returns a vector (list in Python) with each element representing one bit of a 32bit
unsigned/signed integer in such a way that if printed it would appear as the binary representation of the
integer (Least Significant Bit on the right).

e.g. 1 = 00000000000000000000000000000001

Assign either a 1 or a 0 to the vector element depending on whether the bit at the corresponding position is
a 1 or 0.

For example:

showBits(1);
would return the following:

vector<int> bits = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
-1 on the other hand would contain all 1s:

showBits(-1);
vector<int> bits = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
*/


#include <vector>
using namespace std;

vector<int> showBits(int n){

std::vector<int> bits;

for (int i =31; i>=0; i--)
bits.push_back((n & (1<<i)) ? 1 : 0);

return bits;
}