Friday 17 August 2018

C#: Bitwise Operators

Bit Wise AND(&), OR(|), Exclusive OR(^)
Bit 1
Bit 2
&
|
^
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0


Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        byte a = 8;
        byte b = 9;

        Console.WriteLine("a & b is " + (a & b));
        Console.WriteLine("a | b is " + (a | b));
        Console.WriteLine("a ^ b is " + (a ^ b));
    }
}

Output

a & b is 8
a | b is 9
a ^ b is 1

Explanation
   a    = 0000 1000
   b    = 0000 1001
   a&b = 0000 1000

   a    = 0000 1000
   b    = 0000 1001
   a|b = 0000 10001

   a     = 0000 1000
   b     = 0000 1001
   a^b = 0000 0001


Previous                                                 Next                                                 Home

No comments:

Post a Comment