1. Originally Posted by Alan Gott
Wouldn't that involve doing the calculations in display_binary then return them to main? (If not, I'm totally clueless to what you are trying to teach me. :S
No. What I was getting at is this (note, these numbers are not correct for your example, you have to modify them):
Code:
`command = (number & 0xF000) >> 12;`
& with 0xF000 leaves the left 4 bits (0xF000 = 1111 0000 0000 0000) the way they are and clears the rest of them. The >> 12 moves everything to the right 12 places, so that you have numbers, from, say 0-8 instead of 8192-57344.

2. Originally Posted by CommonTater
No...

You had to do all that looping to display the binary value underlying your variable.

Bitwise and ( & ) operates directly on that underlying value.

Do a little reading on the logic functions AND OR NEG and XOR for a better insight.
Ahh, that clears it up. Thanks!

Originally Posted by anduril462
No. What I was getting at is this (note, these numbers are not correct for your example, you have to modify them):
Code:
`command = (number & 0xF000) >> 12;`
& with 0xF000 leaves the left 4 bits (0xF000 = 1111 0000 0000 0000) the way they are and clears the rest of them. The >> 12 moves everything to the right 12 places, so that you have numbers, from, say 0-8 instead of 8192-57344.
Thanks, that makes sense.

3. Sorry to bump this thread again, but I have ONE more question. When I am trying to identify the switch, something is happening incorrectly. I have:
Code:
```switch = (number & 0xF000) >> 7;
if(switch == 0)
printf("     Switch: OFF");
else
printf("     Switch: ON");```
Where the switch is determined by the 8th digit from the right, 1 being on and 0 being off. But, when I enter 220, or 1101 1100, it is telling me that the switch is off. Any ideas as why?

4. You are applying the wrong value...

What hex value puts a 1 in only bit 7? (which is the 8th bit from the right)

Opens Win7 calculator, Programmer mode... click bit 7... = 0x80

Thus...
Code:
```if (number & 0x80)
puts("Eyyup, it's on!");
else
puts("Where's that light switch, Martha?");```

5. Originally Posted by CommonTater
You are applying the wrong value...

What hex value puts a 1 in only bit 7? (which is the 8th bit from the right)

Opens Win7 calculator, Programmer mode... click bit 7... = 0x80

Thus...
Code:
```if (number & 0x80)
puts("Eyyup, it's on!");
else
puts("Where's that light switch, Martha?");```
So does that mean I am also finding the command wrong?
Code:
```command = (number & 0xF000) >> 13;
printf("    Command: %d\n", command);```
The above works, but maybe what I need is a link to a thorough explanation of things such as 0xF and 0x80.

6. Originally Posted by Alan Gott
So does that mean I am also finding the command wrong?
Code:
```command = (number & 0xF000) >> 13;
printf("    Command: %d\n", command);```
Given that the command is supposed to be only the 3 msb (most significant bits) yes...

You probably want to AND with 0xE000 instead of ... 0xF000

However! ... since you are looking only for the three most significant bits, there's no need to strip lower bits with AND at all... You can just send them to the bit-bucket.
Code:
`command = number >> 13:`
will get the job done.

Something you will find very handy when bit twiddling is to take a second and write down the binary values for 0 to F ... then you have a visual reference that can show you what values you need.

7. Originally Posted by CommonTater
Given that the command is supposed to be only the 3 msb (most significant bits) yes...

You probably want to AND with 0xE000 instead of ... 0xF000

However! ... since you are looking only for the three most significant bits, there's no need to strip lower bits with AND at all... You can just send them to the bit-bucket.
Code:
`command = number >> 13:`
will get the job done.

Something you will find very handy when bit twiddling is to take a second and write down the binary values for 0 to F ... then you have a visual reference that can show you what values you need.
Thanks a ton! I will definitely look into this more.