Bitwise OR, AND. The OR operator combines all the set bits in both values into a third value. The AND operator returns another number that contains a 1 where each of the two numbers also have a 1.
To understand bitwise operators, we look at patterns of bits. Bitwise operators are essential for some programs that manipulate bit flags.
Bitwise OR. To start, we look at the numbers 77 and 100. These are represented with bit patterns. We combine two values with bitwise OR, and print the result.
Result All of the bits that are 1 in either or both values are 1 in the result. The rest of the bits are 0.
using System;
class Program
{
static void Main()
{
int a = 77;
int b = 100;
// Use bitwise OR.
int c = a | b;
// Display bits.
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 value)
{
return Convert.ToString(value, 2).PadLeft(32, '0');
}
}00000000000000000000000001001101 = 77
00000000000000000000000001100100 = 100
00000000000000000000000001101101 = 109
Bitwise AND. This program generates 2 random numbers. It then uses bitwise "and" on them. Next a helper method is invoked, and we write the bit values to the screen.
Note The number that was returned contains a 1 in only the positions where the two operands (value1 and value2) also have ones.
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');
}
}00000000000000000000001000101011
00000000000000000001111001100001
00000000000000000000001000100001
Discussion. Data structures often use bit flags on their nodes. In certain algorithms, you quickly combine nodes on the stack by using the bitwise OR operator.
And The result of using bitwise "OR" indicates all the bits set by at least one of the nodes.
Data structures such as digital trees use bitmasks to store information. If you take 2 nodes in the tree, you can use the bitwise and to compare the data in their bitmasks.
Result If the result is 0, the nodes contain no equal bits. A bit counting function can determine how many same bits the nodes have.
Summary. We looked at the bitwise OR and AND operators. These operators help with data structures and algorithms where storage space must be compressed into bit masks.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 2, 2024 (edit link).