C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The number that was returned contains a 1 in only the positions where the two operands (value1 and value2) also have ones.
C# program that uses bitwise and operator
using System;
class Program
{
static void Main()
{
int value1 = 555;
int value2 = 7777;
// Use bitwise and operator.
int and = value1 & value2;
// Display bits.
Console.WriteLine(GetIntBinaryString(value1));
Console.WriteLine(GetIntBinaryString(value2));
Console.WriteLine(GetIntBinaryString(and));
}
static string GetIntBinaryString(int value)
{
return Convert.ToString(value, 2).PadLeft(32, '0');
}
}
Output
00000000000000000000001000101011
00000000000000000001111001100001
00000000000000000000001000100001
Tip: This sometimes leads to important performance improvements—particularly on large and complex data structures.
Also: It helps with data structures and algorithms where storage space must be compressed into bit masks.