given two bits in a set of four, fine position of two other bits

Posted by aaa on Stack Overflow See other posts from Stack Overflow or by aaa
Published on 2010-03-23T21:46:26Z Indexed on 2010/03/23 21:53 UTC
Read the original article Hit count: 461

Filed under:
|
|
|

hello I am working on a simple combinatorics part, and found that I need to recover position of two bits given position of other two bits in 4-bits srring.

for example, (0,1) maps to (2,3), (0,2) to (1,3), etc. for a total of six combinations.

My solution is to test bits using four nested ternary operators:

ab is a four bit string, with two bits set.
c = ((((ab & 1) ? (((ab & 2) ? ... ))) : 0)
abc = ab | c
recover the last bit in the same fashion from abc.

can you think of a better way/more clever way? thanks

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about bits