Democracy among integers
You have a 32 bit unsigned integer. You want it to be really reliable, so you store it three times (triple redundancy). Write a subroutine that takes three unsigned, 32 bit integer arguments, and returns a single unsigned 32 bit integer that is constructed by having the bit in each bit-position "vote" for the corresponding output bit (e.g. if at least two of the low-order bits in the passed in arguments are 1, then the low-order bit in the output is a 1).
Hint: There's an easy, fast way, and there's a hard, slow way. I'm looking for the easy, fast way.
(Found on /.)
4 Comments:
add the bits and drop the carry. hope that should work.
@anonymous,
Adding the bits and dropping the carry won't work.
The idea is if any of the 2 bits are 1's we need the output is 1 so the answer sould be do and AND beteween AB , AC , BC and or the results
(A.B + A.C + B.C ) where A,B,C are the three bits. This would work
-Pradip
@pradip,
That sure would work!
Post a Comment
<< Home