bit manipulation - Combine Set, Clear and Toggle in one line of C -


i trying combine 3 bit operations in 1 line of c. 8 bit char, have set bits 2, 4, 6; clear bits 1, 3, 7 , toggle bit 0 , 5 in 1 line code of c. these in 3 line cannot combine these. below have done far:

x= x & 0xd5; x = x | 0x51; x = x ^ 0x84; 

they give right answer given value of x. tried

x = (x & 0xd5) | (x | 0x51) | (x ^ 0x84) 

and

x = x & 0xd5 | 0x51  ^ 0x84 

those not work. suggestion appreciated.

it's this

x = (((x & 0xd5) | 0x51) ^ 0x84) 

your first try wrong, because values of x not updated operations work on same value, besides oring values not equivalent assigning result of operation x.

the second, wrong because or operator precedence, need parentheses.


Comments