1 out of 1 rated this helpful - Rate this topic

| Operator (C# Reference)

Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.

User-defined types can overload the | operator (see operator).

class OR
{
    static void Main()
    {
        Console.WriteLine(true | false);  // logical or
        Console.WriteLine(false | false); // logical or
        Console.WriteLine("0x{0:x}", 0xf8 | 0x3f);   // bitwise or
    }
}
/*
Output:
True
False
0xff
*/


Concepts

Reference

Other Resources

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Testing A Flag

Testing Values

(this usage was extracted from the CodeGuru at http://www.codeguru.com/vb/sample_chapter/article.php/c12963__1)

For this example imagine a enum defined as follows:

Say you want to express a morning checklist of five items as a flags enumerator. This might be the definition:

// C#
[ flags() ]
enum todo

{
   do_nothing           = 0,
   walk_dog             = 1,
   cook_breakfast       = 2,
   deliver_newspaper    = 4,
   visit_miss_kerbopple = 8,
   wash_covers          = 16
}
'' VB.NET
<flags()> _
Enum todo
   do_nothing           = 0
   walk_dog             = 1
   cook_breakfast       = 2
   deliver_newspaper    = 4
   visit_miss_kerbopple = 8
   wash_covers          = 16
EndEnum

There are many means of testing to see whether (for the sake of this article) I washed the covers. This example is going to demonstrate the means that I found to be most appropriate.

Say I have a variable of type todo that actually reflects what IS DONE.

// C#
todo is_done = do_nothing;
'' VB.NET
Dim is_done as todo = do_nothing

Looking binary

test 1( 10000 is of 11000 ) test 2 (00100 is of 11000).

Say I have a variable of type todo that actually reflects what IS DONE.todo is_done = do_nothing;

If I evaluate ( 10000 | 11000 ), I will get 11000. If I evaluate ( 00100 | 11000 ), I will get 11100.

This means that if I place the bitwise or between any two binary sequences, the result will be a binary sequence where each bit reflects whether at least one sequence is set to 1 in that exact position. If all of the 1s in sequence 1 are reflected as 1s in sequence 2 (as is the case in the first example), sequence 2's value will be the return value. The same applies in the reverse (all of sequence 2's 1s are reflected in sequence 1).

Expressed as flags

Consider:

// C#
todo for_monday = ( todo.walk_dog | todo.cook_breakfast |
                    todo.deliver_newspaper )
'' VB.NetDim for_monday As todo= ( todo.walk_dog Or todo.cook_breakfast Or todo.deliver_newspaper )

(for_monday | todo.wash_covers in c# ) (for_monday Or todo.wash_covers in VB.Net ) yields a different value. for_monday | todo.walk_dog yields for_monday. This is the case because, in appending the additional flags, no change was made. You now can express this in a function that will return true if the option has been completed.

// C#bool testFlag( todo flg_test, todo option )
{
   /// do_nothing should not be compared in the same way (flag 0).if( option == todo.do_nothing )
   returnfalse;
   // returns true if bitwise or yields same as flg_test// (no new binary digits have been set to 1).return flg_test == ( flg_test | option );
}
'' VB.NetFunction testFlag( todo flg_test, todo option ) AsBoolean''' do_nothing should not be compared in the same way (flag 0).
   if option = todo.do_nothing then return false
   '' returns true if bitwise or yields same as flg_test'' (no new binary digits have been set to 1).
   return (flg_test = ( flg_test Or option ))
EndFunction