C# Program to Illustrate Bitwise Operations

«
»

This is a C# Program to illustrate bitwise operations.

Problem Description

This C# Program Illustrates Bitwise Operations.

Problem Solution

Here there are many bitwise operators like AND, OR , XOR which performs corresponding operations.

advertisement
Program/Source Code

Here is source code of the C# Program to Illustrate Bitwise Operations. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate Bitwise Operations
 */
using System;
class bitwise
{
    byte b1, b2;
    int x;
    long y;
    bitwise()
    {
        b1 = 10;
        b2 = 5;
        x = 32;
        y = 20;
    }
    public static void Main()
    {
        bitwise bit = new bitwise();
        byte p = (byte)(bit.b1 & bit.b2);
        byte q = (byte)(bit.b1 | bit.b2);
        byte r = (byte)(bit.b1 ^ bit.b2);
        int z = (int)(bit.x & bit.y);
        Console.WriteLine("b1={0},b2={1},x={2},y={3}", bit.b1, bit.b2, bit.x, bit.y);
        Console.WriteLine("b1 & b2={0} : ", p);
        Console.WriteLine("b1 | b2={0} : ", q);
        Console.WriteLine("b1 ^ b2={0} : ", r);
        Console.WriteLine("x & y = {0} : ", z);
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to illustrate bitwise operations. Here there are many bitwise operators like AND, OR, XOR which performs corresponding operations. Binary AND Operator is used to copy a bit to the result if it exists in both operands. The Binary OR Operator is used to copy a bit to the result if it exists in either operand. And the Binary XOR Operator is used to copy the bit if it is set in one operand but not both.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases
 
b1=10,b2=5,x=32,y=20
b1 & b2 : 0
b1 | b2 : 15
b1 ^ b2 : 15
x & y :0

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.