C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# program that shows bitwise or
using System;
class Program
{
static void Main()
{
int a = 77;
int b = 100;
int c = a | b;
Console.WriteLine("{0} = {1}", GetIntBinaryString(a), a);
Console.WriteLine("{0} = {1}", GetIntBinaryString(b), b);
Console.WriteLine("{0} = {1}", GetIntBinaryString(c), c);
}
static string GetIntBinaryString(int n)
{
char[] b = new char[32];
int pos = 31;
int i = 0;
while (i < 32)
{
if ((n & (1 << i)) != 0)
{
b[pos] = '1';
}
else
{
b[pos] = '0';
}
pos--;
i++;
}
return new string(b);
}
}
Output
00000000000000000000000001001101 = 77
00000000000000000000000001100100 = 100
00000000000000000000000001101101 = 109
Tip: This can yield amazing performance improvements in rare situations. Most algorithms do not benefit.
Also: Check out the bitwise XOR operator for an exclusive OR, which is useful in other rare programs.
XOR