Your task is to devise a programme that extracts the bits, in order, represented by the first argument based on the set bits represented by the second argument (the mask).
Formal Definition:
Let the first argument
x
represent the binary stringS=(xn)(xn-1)...(x1)
withn>=0
andx1
being the least significant digit andxn = 1
.Let the second argument
y
represent the binary stringT=(ym)(ym-1)...(y1)
withm>=0
andy1
being the least significant digit andym = 1
.Let the result represent
0
ifx
represents 0 ory
represents 0.Let the number of
1
digits inT
bep
. Let{di}, i in [1, p]
be the set of binary strings of the form(dim)(di(m-1))...(d1)
defined by (for all i, (for all k,|{dik such that dik = 1}| = 1
)) and (if 1 <= i < j <= p then 0 < di < dj
) and (dp | dp-1 | ... | d1 = T
). In other words,{di}
is the ordered set of strings with a single1
digit that makes up the stringT
when|
is applied.Let the result of the programme represent the following binary string:
((dp & S) << (p - 1))| ... | ((d2 & S) << 1) | (d1 & S)
Examples:
- 1111, 110 -> 11
- 1010, 110 -> 01
- 1010, 10110 -> 01
- 1101010111, 11111000 -> 01010
Input and Output:
- The two arguments may be taken in any way
- The two arguments need not be binary strings. They can also be integers which represent binary strings.
- Answers which do not take input representing binary strings of arbitrary length are permitted.
Winning: Shortest code wins.
Clarifications:
- If your output gives a bit string, order of bits is not important. Simply state the order in your answer. As stated before, the output of your programme need only represent the bits required.