Generate all possible 5-bit binary combinations
Solved
Hello, I would like to write a program to generate all the combinations of size five in binary
example:
00000
00001
00010
00011
00100
00101
00110
00111
...
11111
But I don't know exactly how to go about it, at first I wanted to use a tree structure but I'm not very good at that. Do you have a simpler idea (with an array for example) to do this?
Thank you in advance.
example:
00000
00001
00010
00011
00100
00101
00110
00111
...
11111
But I don't know exactly how to go about it, at first I wanted to use a tree structure but I'm not very good at that. Do you have a simpler idea (with an array for example) to do this?
Thank you in advance.
1 answer
-
ContributorHello, in what context are you doing this?
-
-
Contributor@MaxxxTwo lines of thought for improvement:
1) Did you know that these binary combinations can be used to represent numbers? Can you write the decimal representations of each binary number on your list?
2) How would you go about generating all the combinations of 3 decimal digits? -
@yg_be1) Each binary number represents a decimal number in the usual order, which means that:
00000=> 0
00001 => 1
00010 =>2
00011 =>3
00100 =>4
...
11111=>(2^5)-1=31
2) I know that we need to start from 100 and stop at 999, but I don't see how, among all decimal digits, we can select only those with 3 digits. -
Contributor@Maxxx1) Doesn't that give you an idea for obtaining these combinations?
2) Aren't you forgetting the number 0? -
@yg_beWell, I finally managed to do it by taking the decimal number each time, I store the remainder using %2 and I divide my number by two. This gives me all the combinations and by using some restrictions I get the one I want. Thank you very much for your help, have a nice day.
-