Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Xor

Tags:

vb.net

I've been given the following method:

Private Sub boldButton_Click(sender As System.Object, e As System.EventArgs) Handles boldButton.Click
    Dim curFont As Font
    Dim newFont As Font
    curFont = rtb.SelectionFont
    If curFont IsNot Nothing Then
        'create the new font
        newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor FontStyle.Bold)
        'set it
        rtb.SelectionFont = newFont
    End If
End Sub

Currently having problems understanding what is happening with this part of the code curFont.Style Xor FontStyle.Bold. What is a valid way to achieve the same result without using the operator Xor ?

EDIT (As commented by us2012) Do I need an alternative?

I've looked up Xor on MSDN but still having trouble understanding the implementation of it in boldButton_Click procedure.

like image 863
whytheq Avatar asked Feb 18 '23 09:02

whytheq


1 Answers

Bitwise XOR toggles a flag. Let's assume that the Style bitfield looks like this

00000000
     ^^^
     BIU (Bold, Italic, Underline)

So the value of FontStyle.Bold would be:

00000100

Now something Xor FontStyle.Bold will just flip this bit in something. Example:

00000111 Xor 00000100 = 00000011    (Boldness removed)
00000001 Xor 00000100 = 00000101    (Boldness added)

Note that the other bits are unaffected.


Since you explicitly asked for alternatives: You could check whether the bit is set style And Bold <> 0, and then either set it style = style Or Bold or remove it style = style And (Not Bold).

like image 92
Heinzi Avatar answered Feb 19 '23 23:02

Heinzi