Many ways you can do this. I tried to make it simple so that it's easier for you to understand what's going on.
(1 << i) basically sets the i'th bit to 1 (assuming, the rightmost bit index starts at 0). Same as calculating 2 raised to i.
Code:
int main ()
{
unsigned int Byte;
for (int i = 0; i < 32; i++) {
Byte = (1 << i);
for (int j = 31; j >= 0; j--) {
printf ("%d", (Byte & (1 << j) ? 1 : 0));
}
printf ("\n");
}
return 0;
}
Result:
Code:
00000000000000000000000000000001 - 1
00000000000000000000000000000010 - 2
00000000000000000000000000000100 - 4
00000000000000000000000000001000 - 8
00000000000000000000000000010000 - 16
00000000000000000000000000100000 - 32
00000000000000000000000001000000 - 64
00000000000000000000000010000000 - 128
00000000000000000000000100000000 - 256
00000000000000000000001000000000 - 512
00000000000000000000010000000000 - 1024
00000000000000000000100000000000 - 2048
00000000000000000001000000000000 - 4096
00000000000000000010000000000000 - 8192
00000000000000000100000000000000 - 16384
00000000000000001000000000000000 - 32768
00000000000000010000000000000000 - 65536
00000000000000100000000000000000 - 131072
00000000000001000000000000000000 - 262144
00000000000010000000000000000000 - 524288
00000000000100000000000000000000 - 1048576
00000000001000000000000000000000 - 2097152
00000000010000000000000000000000 - 4194304
00000000100000000000000000000000 - 8388608
00000001000000000000000000000000 - 16777216
00000010000000000000000000000000 - 33554432
00000100000000000000000000000000 - 67108864
00001000000000000000000000000000 - 134217728
00010000000000000000000000000000 - 268435456
00100000000000000000000000000000 - 536870912
01000000000000000000000000000000 - 1073741824
10000000000000000000000000000000 - 2147483648
This is for 32 bit integers. Manipulate the above code a bit to make it work for 4 bits. There are many-many other ways you can achieve the same result and you should try experimenting yourself, like Salem mentions, with your compiler and editor.
If you also are familiar with C++ STL, you could try and learn the algorithm behind std::next_permutation. There's bitset's too which you could use.
Code:
#define all(x) x.begin (), x.end ()
int main ()
{
string NIBBLE = "0001";
cout << NIBBLE << endl;
next_permutation(all(NIBBLE));
cout << NIBBLE << endl;
next_permutation(all(NIBBLE));
cout << NIBBLE << endl;
next_permutation(all(NIBBLE));
cout << NIBBLE << endl;
return 0;
}
int main ()
{
for (int i = 0; i < 4; i++)
cout << bitset <4> (1 << i) << endl;
return 0;
}
Result:
Code:
0001
0010
0100
1000