Majority function in C ++ using 3 uint8_t

uint8_t a = 0x01; //00000001
uint8_t b = 0xff; //11111111
uint8_t c = 0xf0; //11110000

maj(a,b,c) = 0xf1; //11110001

      

I'm not sure how to access and compare each bit so that I can arrive at a result?

+3


source to share


2 answers


result = (a & b) | (b & c) | (c & a);

      



+6


source


@Henrik has already suggested a good straight forward solution that requires 5 operations. FWIW, if efficiency is an issue, you can reduce this to 4 operations:



result = (a & (b | c)) | (b & c);

      

+5


source







All Articles