Combine set, clear and include in one line C

I am trying to combine three bit operations on one line of C. For an 8-bit char, I need to set bits 2, 4, 6; clear bits 1, 3, 7 and translate bits 0 and 5 into just one code on line C. I could do this on three lines, but I can't combine them. The following is what I have done so far:

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

They give the correct answer for the given value of x. But I tried

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

and

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

Those do not work. Any suggestion would be appreciated.

+4
source share
2 answers

This is simple

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

, x , , , x.

, , , .

+4

- . :

x = ((x & 0xD5) | 0x51) ^ 0x84;

:

x = (x & (0xD5 & ~0x51)) ^ (0x84 | 0x51);

, , , , , , , . , .

+3

Source: https://habr.com/ru/post/1625742/


All Articles