Thread: Generate Possible combination of 6-bit Binary number

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    26

    Generate Possible combination of 6-bit Binary number

    Using C-Programming nested for() loops, write a program that displays all
    possible combinations of a 6 bit binary number.

    Sample Run: 000000, 000001, 000010, 000011, ...........111110, 111111

    Can anybody please help me in solving this question?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Sure - where are you stuck? Please post the code you have so far (be sure to use code tags).

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    i did a bit paper work. But didnt find any solution. I am facing problem in choosing termination condition for for() loop. I selected 6 variables for 6 bits.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Azeem View Post
    Using C-Programming nested for() loops, write a program that displays all
    possible combinations of a 6 bit binary number.

    Sample Run: 000000, 000001, 000010, 000011, ...........111110, 111111

    Can anybody please help me in solving this question?
    Looking at the sample binary numbers you posted, do you see a certain incrementing pattern?

    And btw, although it's used incorrectly most times, I refer to the above as permutations, not combinations.

    Combinations are like a group sitting around a conference table - it doesn't matter what chair you sit in, only that each person is at the table. (And a permutation IS what you have with a combination bike lock, where 1-1-1-1 could be a perfectly good combination.

    It may help to think in very simple terms: if you had two such numbers to work with like this, again using nested loops, how would you proceed?
    Last edited by Adak; 08-24-2012 at 10:13 AM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I selected 6 variables for 6 bits.
    Good start. Now, with respect to this assignment, how many different values should each of those variables be able to hold?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hint, find the highest number that is possible to represent with 6 bits. This number is the terminating condition in your loop, print all numbers up to and including that number, and that will be all possible combinations. Now you just need to figure out how to print a number in binary.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Hint, find the highest number that is possible to represent with 6 bits. This number is the terminating condition in your loop, printing all numbers up to and including that number will be all combinations. Now you just need to figure out how to print a number in binary.
    I'm assuming, based on the wording of the assignment, that having six distinct variables (as the OP has done) is the way to go - your solution would be cleaner, but wouldn't require nested "for()" loops in the way that I suspect is required.

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    Adak... the object of this question is to generate truth table using nested for() loop.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Matticus View Post
    I'm assuming, based on the wording of the assignment, that having six distinct variables (as the OP has done) is the way to go - your solution would be cleaner, but wouldn't require nested "for()" loops in the way that I suspect is required.
    It depends, if the "print binary" part is using a loop, then it would work. As a side note, it's a bit odd to require a nested loop imo.

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    Quote Originally Posted by Matticus View Post
    Good start. Now, with respect to this assignment, how many different values should each of those variables be able to hold?
    ok. variables should hold '0' & '1' as the program is for printing binary numbers. Possible combinations are 64 for 6 bit number. This means outer loop terminates at 64.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It depends, if the "print binary" part is using a loop, then it would work. As a side note, it's a bit odd to require a nested loop.
    While you're technically correct, I interpret the assignment to use nested "for()" loops with distinct variables for each bit. However, the description of the assignment is vague, so I could very well be off.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Adak View Post
    And btw, although it's used incorrectly most times, I refer to the above as permutations, not combinations.
    I don't see this as permutations (different arrangements of the elements) or combinations (different subsets of the elements). It's simply counting.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    ok. variables should hold '0' & '1' as the program is for printing binary numbers. Possible combinations are 64 for 6 bit number. This means outer loop terminates at 64.
    Ah, but if your variables should hold either 0 or 1, then none of them should ever be 64. Again, if I understand correctly, while you're printing out binary numbers, it doesn't necessarily mean they are represented as full numbers in your code.
    Last edited by Matticus; 08-24-2012 at 10:26 AM. Reason: Tried to clarify my statement

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Does it really matter how you represent your number(s) or even if you do? From what's said it does not seem that way..

    write a program that displays all
    possible combinations of a 6 bit binary number.

  15. #15
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    Quote Originally Posted by Matticus View Post
    Ah, but if your variables should hold either 0 or 1, then none of them should ever be 64. Again, if I understand correctly, while you're printing out binary numbers, it doesn't necessarily mean they are represented as binary numbers (strings of 0's and 1's) in your code.
    i selected 6 variables for 6 bits and 1 variable to control outer loop which initializes with 1 and terminates at 64. This outer loop is to generate rows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. generate number
    By cheeta in forum C Programming
    Replies: 9
    Last Post: 05-03-2010, 07:49 AM
  2. Replies: 8
    Last Post: 09-27-2008, 07:32 PM
  3. binary combination.
    By Moony in forum C Programming
    Replies: 1
    Last Post: 02-24-2008, 12:55 AM
  4. To generate bar code from a number
    By darkducke in forum C Programming
    Replies: 18
    Last Post: 01-16-2008, 06:33 AM
  5. generate a random number
    By waxydock in forum C++ Programming
    Replies: 5
    Last Post: 06-05-2005, 07:43 PM