Bitwise compliments in C#
Now we are going to learn about how to use bit wise compliments in C#.
Bitwise complements in C#
Bitwise complement operators generally used with combination of other bitwise operators. The means of the bitwise compliment is to converts all the bit as like 1 change into 0 and 0 change into 1. We have used "~" characters to denote the complement operators.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int value = 8;
Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value);
value = ~value;
Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value);
value = ~value;
Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value);
Console.ReadLine();
}
static string GetIntBinaryString(int n)
{
char[] a = new char[15];
int pos = 14;
int i = 0;
while (i < 15)
{
if ((n & (1 << i)) != 0)
a [pos] = '1';
else
a[pos] = '0';
pos--;
i++;
}
return new string(a);
}
}
}
Output