Reputation: 19151
I'm working with some old code, trying to improve it, and I came across the following, which I am having trouble understanding:
controlToUpdate.Font =
new System.Drawing.Font(someFont,
someFontSize,
controlToUpdate.Font.Style ^
(controlToUpdate.Font.Style & FontStyle.Bold));
Specifically, I am confused as to what the last parameter does. As I understand it, the following should do a bitwise comparison, and return the result:
controlToUpdate.Font.Style ^ (controlToUpdate.Font.Style & FontStyle.Bold)
..but what does that mean in this situation? What are the possible results, that may be passed as the third parameter to new Font(...)
, and how can I rewrite this more clearly, while keeping with the intent of the original programmer?
Sidenote: Is this a normal way to do things when working with Windows Forms? I'm a little new in that area - is the intent here obvious to coders more experienced in this field?
Upvotes: 1
Views: 204
Reputation: 62265
If this is normal, or not, depends on what was the reason of doing this, but basically this means:
controlToUpdate.Font.Style ^ (controlToUpdate.Font.Style & FontStyle.Bold)
(controlToUpdate.Font.Style & FontStyle.Bold)
: bitwise AND
, so it's enough having a 0
, and it will return 0
(can think about this like a multiplication)
1 0 = 0
0 1 = 0
1 1 = 1
0 0 = 0
So (controlToUpdate.Font.Style & FontStyle.Bold)
will return true, only if controlToUpdate.Font.Style
is Bold too
after we have
controlToUpdate.Font.Style ^
: a bitwise XOR
operator, where the same value gives 0
1 1 = 0
0 0 = 0
1 0 = 1
0 1 = 1
So, considering previous output (say it's a Bold) the result will be false
or 0
,so Regular font style.
In practise, this is a way to enforse Regular type font, independently of the real style set on the control.
Upvotes: 2
Reputation: 1064114
controlToUpdate.Font.Style & FontStyle.Bold
performs an "and" to return FontStyle.Bold
if the style (controlToUpdate.Font.Style
) includes bold, and 0
if the style does not include bold: basically, it gets just the "bold" bit.
controlToUpdate.Font.Style ^ (controlToUpdate.Font.Style & FontStyle.Bold)
performs an "xor"; if the bold bit was set, it removes it; if the bold bit was not set, then it does nothing (since "xor" with 0 is a no-op).
So basically, that complicated code just removes the "bold" bit (if it is set). A simpler implementation would have been:
controlToUpdate.Font.Style & ~FontStyle.Bold
How that works: here, the ~FontStyle.Bold
inverts all the bits; FontStyle.Bold
is 1
:
000....000001
so ~FontStyle.Bold
is:
111...1111110
we then "and" that with our current style, which means it keeps all of the old style except the bold-bit.
Upvotes: 3
Reputation: 499352
The FontStyle
enumeration is a Flags
enumeration, making them into bitmaps.
Using bitwise operators, allows you to find out which flags are "on", "off" and of course, change them.
This is very common - for example, to find out if the style is bold or italic, you would use:
FontStyle.Bold | FontStyle.Italic
Upvotes: 2