Problem
Generate integers with specified 1's
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Generate integers with specified 1's
Created Data : 17-07-2013
Last Modified :
============================================================================
*/
#include <queue>
#include <iostream>
#include <iomanip>
using namespace std;
#define SETBIT(num, i) ((num) | (1 << i))
#define GETBIT(num, i) ((num) & (1 << i))
void PrintIntegersWithSpecfied1s(int num)
{
if(num == 0){
cout << 0 << endl;
return;
}
int i = 1;
queue<unsigned char> ints;
for(int j = 0; j < sizeof(unsigned char) * 8; ++ j){
ints.push(1 << j);
}
while(i < num){
queue<unsigned char> temp;
ints.swap(temp);
while(!temp.empty()){
unsigned char n = temp.front();
temp.pop();
for(int j = 0; j < sizeof(unsigned char) * 8; ++ j){
if(GETBIT(n, j) == 0){
ints.push(SETBIT(n, j));
}
}
}
i ++;
}
while(!ints.empty()){
char buffer[9];
itoa(ints.front(), buffer, 2);
cout.fill('0');
cout << setw(8) << (int)(ints.front()) << "----" << setw(8) << buffer << endl;
ints.pop();
}
cout << "----------------------------" << endl;
}
int main(int argc, char* argv[])
{
for(int i = 1; i < 4; ++ i){
PrintIntegersWithSpecfied1s(i);
}
return 0;
}
Output
00000001----00000001
00000002----00000010
00000004----00000100
00000008----00001000
00000016----00010000
00000032----00100000
00000064----01000000
00000128----10000000
----------------------------
00000003----00000011
00000005----00000101
00000009----00001001
00000017----00010001
00000033----00100001
00000065----01000001
00000129----10000001
00000003----00000011
00000006----00000110
00000010----00001010
00000018----00010010
00000034----00100010
00000066----01000010
00000130----10000010
00000005----00000101
00000006----00000110
00000012----00001100
00000020----00010100
00000036----00100100
00000068----01000100
00000132----10000100
00000009----00001001
00000010----00001010
00000012----00001100
00000024----00011000
00000040----00101000
00000072----01001000
00000136----10001000
00000017----00010001
00000018----00010010
00000020----00010100
00000024----00011000
00000048----00110000
00000080----01010000
00000144----10010000
00000033----00100001
00000034----00100010
00000036----00100100
00000040----00101000
00000048----00110000
00000096----01100000
00000160----10100000
00000065----01000001
00000066----01000010
00000068----01000100
00000072----01001000
00000080----01010000
00000096----01100000
00000192----11000000
00000129----10000001
00000130----10000010
00000132----10000100
00000136----10001000
00000144----10010000
00000160----10100000
00000192----11000000
----------------------------
00000007----00000111
00000011----00001011
00000019----00010011
00000035----00100011
00000067----01000011
00000131----10000011
00000007----00000111
00000013----00001101
00000021----00010101
00000037----00100101
00000069----01000101
00000133----10000101
00000011----00001011
00000013----00001101
00000025----00011001
00000041----00101001
00000073----01001001
00000137----10001001
00000019----00010011
00000021----00010101
00000025----00011001
00000049----00110001
00000081----01010001
00000145----10010001
00000035----00100011
00000037----00100101
00000041----00101001
00000049----00110001
00000097----01100001
00000161----10100001
00000067----01000011
00000069----01000101
00000073----01001001
00000081----01010001
00000097----01100001
00000193----11000001
00000131----10000011
00000133----10000101
00000137----10001001
00000145----10010001
00000161----10100001
00000193----11000001
00000007----00000111
00000011----00001011
00000019----00010011
00000035----00100011
00000067----01000011
00000131----10000011
00000007----00000111
00000014----00001110
00000022----00010110
00000038----00100110
00000070----01000110
00000134----10000110
00000011----00001011
00000014----00001110
00000026----00011010
00000042----00101010
00000074----01001010
00000138----10001010
00000019----00010011
00000022----00010110
00000026----00011010
00000050----00110010
00000082----01010010
00000146----10010010
00000035----00100011
00000038----00100110
00000042----00101010
00000050----00110010
00000098----01100010
00000162----10100010
00000067----01000011
00000070----01000110
00000074----01001010
00000082----01010010
00000098----01100010
00000194----11000010
00000131----10000011
00000134----10000110
00000138----10001010
00000146----10010010
00000162----10100010
00000194----11000010
00000007----00000111
00000013----00001101
00000021----00010101
00000037----00100101
00000069----01000101
00000133----10000101
00000007----00000111
00000014----00001110
00000022----00010110
00000038----00100110
00000070----01000110
00000134----10000110
00000013----00001101
00000014----00001110
00000028----00011100
00000044----00101100
00000076----01001100
00000140----10001100
00000021----00010101
00000022----00010110
00000028----00011100
00000052----00110100
00000084----01010100
00000148----10010100
00000037----00100101
00000038----00100110
00000044----00101100
00000052----00110100
00000100----01100100
00000164----10100100
00000069----01000101
00000070----01000110
00000076----01001100
00000084----01010100
00000100----01100100
00000196----11000100
00000133----10000101
00000134----10000110
00000140----10001100
00000148----10010100
00000164----10100100
00000196----11000100
00000011----00001011
00000013----00001101
00000025----00011001
00000041----00101001
00000073----01001001
00000137----10001001
00000011----00001011
00000014----00001110
00000026----00011010
00000042----00101010
00000074----01001010
00000138----10001010
00000013----00001101
00000014----00001110
00000028----00011100
00000044----00101100
00000076----01001100
00000140----10001100
00000025----00011001
00000026----00011010
00000028----00011100
00000056----00111000
00000088----01011000
00000152----10011000
00000041----00101001
00000042----00101010
00000044----00101100
00000056----00111000
00000104----01101000
00000168----10101000
00000073----01001001
00000074----01001010
00000076----01001100
00000088----01011000
00000104----01101000
00000200----11001000
00000137----10001001
00000138----10001010
00000140----10001100
00000152----10011000
00000168----10101000
00000200----11001000
00000019----00010011
00000021----00010101
00000025----00011001
00000049----00110001
00000081----01010001
00000145----10010001
00000019----00010011
00000022----00010110
00000026----00011010
00000050----00110010
00000082----01010010
00000146----10010010
00000021----00010101
00000022----00010110
00000028----00011100
00000052----00110100
00000084----01010100
00000148----10010100
00000025----00011001
00000026----00011010
00000028----00011100
00000056----00111000
00000088----01011000
00000152----10011000
00000049----00110001
00000050----00110010
00000052----00110100
00000056----00111000
00000112----01110000
00000176----10110000
00000081----01010001
00000082----01010010
00000084----01010100
00000088----01011000
00000112----01110000
00000208----11010000
00000145----10010001
00000146----10010010
00000148----10010100
00000152----10011000
00000176----10110000
00000208----11010000
00000035----00100011
00000037----00100101
00000041----00101001
00000049----00110001
00000097----01100001
00000161----10100001
00000035----00100011
00000038----00100110
00000042----00101010
00000050----00110010
00000098----01100010
00000162----10100010
00000037----00100101
00000038----00100110
00000044----00101100
00000052----00110100
00000100----01100100
00000164----10100100
00000041----00101001
00000042----00101010
00000044----00101100
00000056----00111000
00000104----01101000
00000168----10101000
00000049----00110001
00000050----00110010
00000052----00110100
00000056----00111000
00000112----01110000
00000176----10110000
00000097----01100001
00000098----01100010
00000100----01100100
00000104----01101000
00000112----01110000
00000224----11100000
00000161----10100001
00000162----10100010
00000164----10100100
00000168----10101000
00000176----10110000
00000224----11100000
00000067----01000011
00000069----01000101
00000073----01001001
00000081----01010001
00000097----01100001
00000193----11000001
00000067----01000011
00000070----01000110
00000074----01001010
00000082----01010010
00000098----01100010
00000194----11000010
00000069----01000101
00000070----01000110
00000076----01001100
00000084----01010100
00000100----01100100
00000196----11000100
00000073----01001001
00000074----01001010
00000076----01001100
00000088----01011000
00000104----01101000
00000200----11001000
00000081----01010001
00000082----01010010
00000084----01010100
00000088----01011000
00000112----01110000
00000208----11010000
00000097----01100001
00000098----01100010
00000100----01100100
00000104----01101000
00000112----01110000
00000224----11100000
00000193----11000001
00000194----11000010
00000196----11000100
00000200----11001000
00000208----11010000
00000224----11100000
00000131----10000011
00000133----10000101
00000137----10001001
00000145----10010001
00000161----10100001
00000193----11000001
00000131----10000011
00000134----10000110
00000138----10001010
00000146----10010010
00000162----10100010
00000194----11000010
00000133----10000101
00000134----10000110
00000140----10001100
00000148----10010100
00000164----10100100
00000196----11000100
00000137----10001001
00000138----10001010
00000140----10001100
00000152----10011000
00000168----10101000
00000200----11001000
00000145----10010001
00000146----10010010
00000148----10010100
00000152----10011000
00000176----10110000
00000208----11010000
00000161----10100001
00000162----10100010
00000164----10100100
00000168----10101000
00000176----10110000
00000224----11100000
00000193----11000001
00000194----11000010
00000196----11000100
00000200----11001000
00000208----11010000
00000224----11100000
----------------------------
Press any key to continue . . .