Most efficient way to implement 12bit roate bits?

I am using a 12bit binary number to represent the boolean state and position of twelve flags.

I want to be able to rotate with no carry the position of these bits in left or right direction. So basically if the pattern gets shifted off either end of the 12bits it is wrapped around to the other side.

What is the most efficient way to implement this?

ECL?
TTL?

Depends on how fast you have to do it.

no idea what those mean!

Emitter-coupled logic.
Transistor Transistor logic.

Ok I know what TTL logic is, I thought that perhaps you were suggesting an assembler instruction I had not heard of :stuck_out_tongue:

This is the software forum so I would like to implement a software solution to this problem.

// flags contained in 12 right most bits
unsigned short bit_flags;

if ( bit_flags & 1) bit_flags |= 0x1000;
bit_flags >>= 1;

Code of lloyddean is for shift to right

Code for shift to the left

bit_flags <<= 1;
if (bit_flags & 0x1000) bitflags = (bitflags & 0x0FFF) | 1;

lloyddean has the right idea. There are probably a couple of things that you could do to speed it up with assembly, but in general it's probably not worth it. The difference on a 16bit shift (where C would have trouble using the carry status bit) would probably be more significant.

(Hmm. There are a number of interesting ways to do this in assembler, and there would be some savings by being able to treat the bytes separately for the boolean logic. But not lots. And it would depend somewhat on whether you want to use more space to get faster execution.)

Reminds me of Kerningham (inventor of C language) who had a nice way to shift bits by means of three 'reverse' operators

shiftright n = reverse whole byte,
reverse bit 0..n-1,
reverse bits n..8;
(just try it with pencil & paper)

Could also be used to rotate elements in an array :slight_smile: