This C# program demonstrates how the bitwise operators are used.
using System;
namespace XoaX {
class Program {
static void Main(string[] args) {
uint i = 1303515112;
// The bitwise complement
uint comp = ~i;
uint j = 0xF5AB3973; // Hexadecimal
PrintBits(i);
Console.WriteLine(" is {0} in binary ", i);
PrintBits(comp);
Console.WriteLine(" is the bitwise complement");
PrintBits(j);
Console.WriteLine(" is {0} in binary: ", j);
Console.WriteLine("Print the operands for clarity");
PrintBits(i);
Console.WriteLine();
PrintBits(j);
Console.WriteLine();
PrintBits(i & j);
Console.WriteLine(" is the bitwise &");
PrintBits(i | j);
Console.WriteLine(" is the bitwise |");
PrintBits(i ^ j);
Console.WriteLine(" is the bitwise ^");
int n = 692358457;
int m = -692358457;
Console.WriteLine("A value and its signed twos complement");
PrintBits((uint)n);
Console.WriteLine();
PrintBits((uint)m);
Console.WriteLine();
Console.WriteLine("Right shifted to demonstrate sign bit filling");
PrintBits((uint)(n >> 17));
Console.WriteLine();
PrintBits((uint)(m >> 17));
Console.WriteLine();
Console.WriteLine("Left shifted to demonstrate zero filling");
PrintBits((uint)(n << 17));
Console.WriteLine();
PrintBits((uint)(m << 17));
Console.WriteLine();
}
static void PrintBits(uint uiBits) {
for (int i = 31; i >= 0; --i ) {
Console.Write((uiBits >> i) & 1);
}
}
}
}
01001101101100100000111111101000 is 1303515112 in binary 10110010010011011111000000010111 is the bitwise complement 11110101101010110011100101110011 is 4121639283 in binary: Print the operands for clarity 01001101101100100000111111101000 11110101101010110011100101110011 01000101101000100000100101100000 is the bitwise & 11111101101110110011111111111011 is the bitwise | 10111000000110010011011010011011 is the bitwise ^ A value and its signed twos complement 00101001010001001000110100111001 11010110101110110111001011000111 Right shifted to demonstrate sign bit filling 00000000000000000001010010100010 11111111111111111110101101011101 Left shifted to demonstrate zero filling 00011010011100100000000000000000 11100101100011100000000000000000 Press any key to continue . . .
© 20072025 XoaX.net LLC. All rights reserved.