English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi,

I need to write a program which can produce all different combinations of a 10 binary digit sequence. e.g 2^10 or 1024 different binary values.

e.g 0000000000, 0000000001, 0000000010, 0000000011, ..........., 1111111111

Anyone knows how to produce such a sequence?


Jialat liao........i have to complete it by tomorrow and I don't know how to write

2007-02-13 18:44:19 · 3 answers · asked by ZhiYi 1 in Computers & Internet Programming & Design

The language I am using is C++ and the datatype used are integers (1 and 0).

2007-02-13 19:07:48 · update #1

3 answers

Plz specify which language
Also whether binary nuber is generated from decimal or hex

//here is a simple example
//use an array of char>>>> its better in C

char binary[10];
int i;


printf("Please enter an interger : ");
scanf("%d", &mynum);

for(i=0; i<10; i++)
{
if(num%2 )
{
binary[i] = '1';
num -= 1;
num /=2;
}
else
{
binary[i] = '0';\
num /=2;
}
}
//printing the binary number
for (i=9; i>=0; i--){
printf("%c", binary[i]);
}
}

2007-02-13 19:02:52 · answer #1 · answered by Anonymous · 0 0

#include
using namespace std;

int main(){
int n=10;
char binary[n];
int i,num,k;

for(k=0;k<1024; k++){

num=k;
cout << k << "\t" ;

for(i=0; i<10; i++)
{

if(num%2 )
{
binary[i] = '1';
num -= 1;
num /=2;
}
else
{
binary[i] = '0';
num /=2;
}

}

for (i=9; i>=0; i--){
cout << binary[i];
}//for

cout << endl;
}
system("pause");
}

2007-02-13 20:13:51 · answer #2 · answered by iyiogrenci 6 · 0 0

/* I am a little rusty. Check if this will compile. In C*/

#include stdio.h
void main()
{
int i, j, k;
k = 1024;
for ( i=0 ; i++ {for ( j = 1 ; j < k ; j <<= 1 )
printf("%c", (( j & i ) ? '0','1'));
printf (",");
}
printf ("\n");
}

2007-02-13 19:18:32 · answer #3 · answered by J C 5 · 0 0

fedest.com, questions and answers