-1

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?

3 Answers3

2

No, you've got it; the & operations are erasing the BOLD and ITALIC bits, while the | operations set them.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
2

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;
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • So as a result, it creates a text that has only BOLD and ITALIC styles. –  Mar 13 '17 at 17:12
  • It seems so, yes. Certainly, you could test that yourself? – OneCricketeer Mar 13 '17 at 17:35
  • Or you could just write `text.flags |= bold_italic` – harold Mar 13 '17 at 17:44
  • 1
    @Paulpro the other flags are not turned off anyway. `& ~bold_italics` turns off *bold_italics*. It's the same flags that are turned off and then turned on again. If that's not convincing by itself [here](http://haroldbot.nl/?q=%28textflags+%26+~bold_italic%29+%7C+bold_italic) is a step-by-step proof – harold Mar 13 '17 at 18:48
  • 1
    Also, if the other flags *were* turned off, the whole thing would simplify to `text.flags = bold_italic` – harold Mar 13 '17 at 18:52
  • @harold You're right, my bad. This answer is wrong, it doesn't turn off all other flags. – Paul Mar 13 '17 at 18:52
  • @harold your haroldbot is great. I completely understand it now. –  Mar 13 '17 at 23:56
2

Lets start with 4 bits flag.

BOLD = 0001; ITALIC = 0010

flags & ~BOLD =   
flags & ~0001 = 
flags & 1110 = clear BOLD flag.

flags | ITALIC = 
flags | 0010 = 
flags | 0010 = set ITALIC flag
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245