blog

Home / DeveloperSection / Blogs / Basic Concept and program for Bit-Wise Operation

Basic Concept and program for Bit-Wise Operation

Abhishek Srivasatava1340 13-Sep-2016

Today's world is a digital world. All the computerized data manipulation is done in binary format because it is directly implemented in digital electronic circuitry using logic gates. The binary system is used internally by almost all modern computers and computer-based devices.

For C#, below is the bit-wise operators which are used to manipulate the data:

 Operator         Description  

        &             Binary AND Operator

        |             Binary OR Operator 

        ^            Binary XOR Operator

        ~            Binary Ones Complement Operator       

        <<         Binary Left Shift Operator

        >>         Binary Right Shift Operator

 

Here is the complete code to implement these operators:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication5

{

    class Program

    {

        static void Main(string[] args)

        {

            int a, b, c, d;

            Console.Write("Enter the First Number : ");

            a = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the Second Number : ");

            b    = Convert.ToInt32(Console.ReadLine());

            c = a& b; 

            Console.WriteLine("Result for the AND operator is {0}", c);

            c = a | b;

            Console.WriteLine("Result for the OR operator  is {0}", c);

            c = a ^ b;

            Console.WriteLine("Result for the XOR operator is {0}", c);

            c = ~a;

            d = ~b;

            Console.WriteLine("Result for the Binary Ones Complement Operator of 1st number is {0} and for 2nd is {1}", c,d);

            c = a << 2;

            d = b<< 2;

            Console.WriteLine("Result for the Binary Left Shift Operaton of 1st number is {0} and for 2nd is {1}", c,d);

            c = a>> 2;

            d = b>> 2;

            Console.WriteLine("Result for the Binary Right Shift Operator of 1st number is {0} and for 2nd is {1}", c,d);

            Console.ReadLine();

        }

    }

}

Basic Concept and program for Bit-Wise Operation


 

 


Updated 16-Mar-2018

Leave Comment

Comments

Liked By