Wednesday, October 26, 2005

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:

At 3:22 PM, Anonymous Anonymous said...

add the bits and drop the carry. hope that should work.

 
At 3:35 PM, Blogger Sids said...

@anonymous,

Adding the bits and dropping the carry won't work.

 
At 10:29 AM, Anonymous Anonymous said...

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

 
At 9:01 AM, Blogger Sids said...

@pradip,

That sure would work!

 

Post a Comment

<< Home