View previous topic :: View next topic |
Author |
Message |
roby Guest
|
question about binary code and variables |
Posted: Wed Mar 01, 2006 2:06 pm |
|
|
I need to read one bit from 3 pins A1 A2 A3. For example A1=1 A2=2 A3=3 then I need to see them as one binary number that is to say I need to put them side by side to create the number 101 and I it becomes a number to convert in decimal or to use.
how can I do?
Thanks from Italy |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Mar 01, 2006 2:35 pm |
|
|
Do you mean something like this?
Code: | int8 InputValue;
InputValue = input_a() & 0b00001110; // Only get state of pins A1, A2, A3
InputValue = InputValue >> 1; // A0 is not used, so shift value 1 bit to the right
printf("InputValue = %d\n", InputValue); |
|
|
![](templates/subSilver/images/spacer.gif) |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Mar 01, 2006 4:24 pm |
|
|
If what you want it is a binary representación of the type '101':
Code: |
int n = 3;
InputValue = input_a() & 0b00001110;
printf("Binary = ");
do
{
if(bit_test(InputValue,3))
{putc('1'); }
else
{putc('0'); }
rotate_left(&InputValue,1);
}while(--n);
|
Humberto |
|
![](templates/subSilver/images/spacer.gif) |
|