Suppose we implement a program to simulate a computer whose ability is to carry out binary arithmetic, using bitwise operators.
Your should perform the following bitwise operations using the logical operators:
~ (not operator) & (AND operator)
| (OR operator)
^ (EX OR, which is Exclusive OR)
>> (shift right)
<< (shift left)
Your program will ask the user to enter two integers (in decimal numbers), but of course your program
(or the computer) will recognize them as binary numbers in terms of 0s and 1s.
You should test your program with the following numbers:
A = 3 = 0000 0011
B = 4 = 0000 0100
Your program will perform the logical operations on these numbers as follows:
(1) A ^ B =
(2) ~B =
(3) A | ~B =
(4) ~A^B
(5) A << 1
(6) B >> 2
Solution in C# Programming Language
using System;
public class Program
{
public static void Main()
{
Console.Write("Enter integer A: ");
int intA = int.Parse(Console.ReadLine());
Console.Write("\n Enter integer B: ");
int intB = int.Parse(Console.ReadLine());
Console.Write("\n Integer A, " + intA + ", in binary = ");
int valueA = intA;
string binaryA = Convert.ToString(valueA, 2).PadLeft(8, '0');
Console.Write(binaryA);
Console.Write("\n Integer B, " + intB + ", in binary = ");
int valueB = intB;
string binaryB = Convert.ToString(valueB, 2).PadLeft(8, '0');
Console.Write("{0:x2}", binaryB);
Console.Write("\n (1) A ^ B = ");
long exclusive = intA ^ intB;
Console.Write(exclusive);
Console.Write("\n (2) ~B = ");
long complement = ~intB;
Console.Write(complement);
Console.Write("\n (3) A | ~B = ");
long inclusive = intA | ~ intB;
Console.Write(inclusive);
Console.Write("\n (4) ~A^B = ");
long exclusiveComp = ~intA ^ intB;
Console.Write(exclusiveComp);
Console.Write("\n (5) A << 1 = ");
long leftShift = intA << 1;
Console.Write(leftShift);
Console.Write("\n (6) B >> 2 = ");
long rightShift = intB >> 2;
Console.Write(rightShift);
}
}