Thread: The Binary game
View Single Post
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 04-27-2013, 01:10 PM

Quote:
Originally Posted by Kelrizzo View Post
Oh I understand the physical steps to make a variable. Maybe I'm too tired to make sense I was talking about the theoretical...when to use a variable versus a parameter.

I'm going to work on the project again now so I may get somewhere on my own, but I need to assign a random value 0 or 1 to each of the three switches. The user guesses what each switch is by flipping it up or down. Up represents 1, down represents 0. So you have a binary number with 3 digits.

If you guess the right number, the lightbulb goes on.
If I understand correctly you have a target value from 0 to 7 and you want to use this to tell when the switches are in the correct position. Each switch can either be off-0 or on-1 what you may be missing is the binary math here. Using three bits in binary I can represent any number from 0 to 7. Here is the binary:

000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7

Why did I go through all that? Well since each switch can be either a 0 or a 1 we need to scale the value of two of the switches to create the three binary digits above.
So your switches value would be something like "set currentVal = sw1 + sw2 * 2 + sw3 *4"
Now you have converted the individual switches into one value between 0 and 7.

PS the values 2 and 4 in the conversion come from binary positional notation. If you want to look this up, start with decimal positional notation. This will make the concept clear and then see how it would be done for numbering systems in other bases.

Mark
   
Reply With Quote