Search code examples
bit-manipulationbitwise-operatorsbit-fields

What does this bit wise operation doing?


I read about bitwise operations a lot, but still, I couldn't give a meaning to this line.

((text.flags & ~Text.BOLD) & ~Text.ITALIC) | Text.BOLD | Text.ITALIC

It seems like the author is trying to be sure that this text doesn't have styles BOLD and ITALIC, and then he makes the text ITALIC and BOLD.

Am I right, or missing some detail?


Solution

  • It seems to be turning off all flags not BOLD and not ITALIC (via & with the complement), and then ensuring that BOLD | ITALIC is set (via |).

    The end result would be that for any input text regardless of style, the output is text

    Could be re-written as

    int bold_italic = Text.BOLD | Text.ITALIC;
    text.flags = (text.flags & ~bold_italic) | bold_italic;