More basic bit operations

P

philbo30

Should be an easy question; I'm still new to bits operations...

I've got the parallel "control" register byte, initially at: 0000 0000

that after receiving an electrical "signal", looks like this:

0000 0110

The signal bits are located as shown below:

0000 0110
^ ^^
SIA

(In the case above, I and A are set to 1 and S is set to Zero.)

The goal is to run the minimal number of operations against the
register to identify the values of S,I,A.

One option would be to compare the actual value against each of the
the known combinations, but perhaps there is a more efficient way ??
 
P

philbo30

Should be an easy question; I'm still new to bits operations...

I've got the parallel "control" register byte, initially at: 0000 0000

that after receiving an electrical "signal", looks like this:

0000 0110

The signal bits are located as shown below:

0000 0110
^ ^^
SIA

(In the case above, I and A are set to 1 and S is set to Zero.)

The goal is to run the minimal number of operations against the
register to identify the values of S,I,A.

One option would be to compare the actual value against each of the
the known combinations, but perhaps there is a more efficient way ??

Art didn't come through properly, here's the translation:

S = 0
I = 1
A = 1
Least Sig Bit = 0
 
P

pete

philbo30 said:
Art didn't come through properly, here's the translation:

S = 0
I = 1
A = 1
Least Sig Bit = 0

I'm going to call that a value of (6).

/*
** You could also index into an array of functions pointers
** instead of using a switch statement.
*/

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
unsigned signal = 0x6;

printf("signal is %u\n\n", signal);
switch (signal >> 1 & 0x7) {
case 0:
puts("S = 0");
puts("I = 0");
puts("A = 0");
break;
case 1:
puts("S = 0");
puts("I = 0");
puts("A = 1");
break;
case 2:
puts("S = 0");
puts("I = 1");
puts("A = 0");
break;
case 3:
puts("S = 0");
puts("I = 1");
puts("A = 1");
break;
case 4:
puts("S = 1");
puts("I = 0");
puts("A = 0");
break;
case 5:
puts("S = 1");
puts("I = 0");
puts("A = 1");
break;
case 6:
puts("S = 1");
puts("I = 1");
puts("A = 0");
break;
case 7:
puts("S = 1");
puts("I = 1");
puts("A = 1");
break;
default:
puts("invalid case");
break;
}
return 0;
}

/* END new.c */
 
T

Thad Smith

The easiest and typically most efficient way is to perform a bit-wise
and with a separate mask for each bit:
s = register & 0x08;
etc.

Whether this is the most efficient for your purposes depends on several
factors, but it is straight-forward.

Art didn't come through properly, here's the translation:

S = 0
I = 1
A = 1
Least Sig Bit = 0

Use spaces instead of tabs and it should align properly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top