none
Bitwise operators

    Question

  •     I am really new to C# and am still learning its many idiosyncrasies.  While learning how to code for the RichTextbox control (I'm reading the book "Beginning Visual C# 2005" from Wrox Press, I ran across this piece of code that has me rather stumped.  I know it works but I don't have any idea how it works.  After reading about bitwise operators it got me even more confused.

        The code checks the selected text and changes the FontStyle to Bold if the style is Regular or vice versa if the style is Bold.

        Can anyone please tell me what the bitwise And and the bitwise Or are doing?

        I know there are "cleaner" and easier understood ways of codeing this but this is really holding my attention!

    private void buttonBold_Click ( object sender, EventArgs e )
    {
       
    Font oldFont;
       
    Font newFont;

        //* retrieve the font currently being used
       
    oldFont = this.richtextboxText.SelectionFont;

        //* if the font is using the bold style now, remove the formatting

        if ( oldFont.Bold )
        {
           
    //* the tilde performs a bitwise complement on the operand, in this
           
    //* case FontStyle, effectivly setting it to non-bold
           
    newFont = new Font ( oldFont, oldFont.Style & ~FontStyle.Bold );    <==== What is the bitwise And doing?
        }
       
    else
       
    {
           
    newFont = new Font ( oldFont, oldFont.Style | FontStyle.Bold );       <==== What is the bitwise Or doing?
        }
           
    //* insert the new font and return focus to the RTB
           
    this.richtextboxText.SelectionFont = newFont;
           
    this.richtextboxText.Focus ( );
        }

    Tuesday, June 06, 2006 4:59 PM

Answers

  • >newFont = new Font ( oldFont, oldFont.Style & ~FontStyle.Bold );    <==== What is the bitwise And doing?

    The oldFont object has a Style property which is an enum that has an Int32 value.  FontStyle.Bold is a specific item of the enumeration and it's value is 1.

    The expression is taking the current value of the Style property and doing a bitwise AND with the complement of the value 1 and then passing that into the Font constructor. 

    In this case if the Bold bit is set, it is turning it off.  Both Style and Bold are integer values. 
    Tuesday, June 06, 2006 8:49 PM

All replies

  • Are you asking for an explanation of bitwise operators in general, or what specifically they are doing in the context of the FontStyle class?

    -Tom Meschter
    Software Dev, Visual C# IDE

    Tuesday, June 06, 2006 5:41 PM
  • You can think of bitwise operators as switch operators and a bit as a linear switch bank of switches.  Each byte is made of 8 bits.  A bit is one digit in a binary number. (Forgive me if you already know this but perhaps someone that's reading doesn't).

     

    the digit placement in decimal is, of course:

    1-10-100-1000-1000 so that each next digit to the left is 10 times the last.

    likewise, each digit placement in binary is:

    1-2-4-8-16-32-64-128-256 and so on, so that each next digit to the left is 2 times the last.

    Examining a bit value of the decimal equivelent of 5:

    00000101 (you can see that the 1 bit and the 4 bit are both turned on which equals 5)

    in a binary switch 0 = off and 1 = on. 

    The OR operator basically examines two binary values lined up together (think of it as two switch banks) and returns a value of 1 if a value of 1 exists in either bank.  It does this for each digit place individually. so to examine the or value of two binary numbers:

    01000010 (First binary value to compare)
    00000001 (Second binary value to compare)
    --------------
    01000011 (OR result)

     

    The AND operator works similarly but only returns a value of 1 if a value of 1 exists in both banks:

    01000010 (First binary value to compare)
    01000001 (Second binary value to compare)
    --------------
    01000000 (AND result)

    This is useful if I've assigned an enumeration binary values as such:

    enum MyEnum { a=1,b=2,c=4,d=8,e=16... and so on}

    Now I can pool multiple enumerations together in one value and later extract which enumerations exist in that value by using the OR and AND operators respectfully:

    Store enum values a and d in an enumeration instance:

    MyEnum enumInstance = MyEnum.a & MyEnum.d;

    Test to see if enum value d is in the instance enumInstance:

    if ((enumInstance | MyEnum.d) == MyEnum.d) { do whatever...}

     

    Hope that helps,

     

    Devin

     

     

     

     

     

    Tuesday, June 06, 2006 6:34 PM
  • No.  I (somewhat) understand bitwise operators.  But, this combination is puzzling, for instance, the tilde operator according to Microsoft is used:

    "The ~ operator performs a bitwise complement operation on its operand, which has the effect of reversing each bit. Bitwise complement operators are predefined for int, uint, long, and ulong."

    Now, that tells me that it is used on number or numeric fields.  Then what is it doing to the Boolean field FontStyle.Bold?  And, how is the bitwise And '&' affecting the outcome?

     

    I hope I have explained this a little clearer?  As I said, I'm pretty new at C#.  I've been coding in COBOL for 35 years.

     

    Thank you;

    Greg

    Tuesday, June 06, 2006 6:40 PM
  • >newFont = new Font ( oldFont, oldFont.Style & ~FontStyle.Bold );    <==== What is the bitwise And doing?

    The oldFont object has a Style property which is an enum that has an Int32 value.  FontStyle.Bold is a specific item of the enumeration and it's value is 1.

    The expression is taking the current value of the Style property and doing a bitwise AND with the complement of the value 1 and then passing that into the Font constructor. 

    In this case if the Bold bit is set, it is turning it off.  Both Style and Bold are integer values. 
    Tuesday, June 06, 2006 8:49 PM
  • FontStyle.Bold is not boolean,  it is an int32 enum.  That's why the bitwise operators can work on it.
    Tuesday, June 06, 2006 8:51 PM
  • You have to remember that even numbers, although we think of then as integers and such, are stored in the computer as 0's and 1's and are binary in nature as stored in computer memory.  Therefore a bitwise operator can work on number values just fine.

     

    From my understanding, a complement operator inverts the bits assigned to a value in memory.  ie:

     

    00100101 (start value = 37)
    11011010 (result of ~ operator = 218)

     

    HTH,

     

    Devin

     

     

    Wednesday, June 07, 2006 1:17 PM
  • Thanks to everyone who answered this post.  I am finally getting an understanding of what was going on with the operators.  My confusion was with the bitwise and (&) and the tilde(~).  To me they were cancelling each other out.

    Thanks again!

     

    Rhubarb

    Thursday, June 08, 2006 4:05 PM