What are the Bitwise Operators in C#
Description
The bitwise operators act directly upon the bits of their operands. They are defined only for integer operands. They cannot be used on bool, float, or double.
Operator | Result |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR (XOR) |
> | Shift right |
< | Shift left |
~ | One's complement (unary NOT) |
All of the binary bitwise operators can be used in compound assignments.
x = x ^ 127;
x ^= 127;
True table,
p | q | p & q | p | q | p ^ q | ~p |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 |
1 | 0 | 0 | 1 | 1 | 0 |
0 | 1 | 0 | 1 | 1 | 1 |
1 | 1 | 1 | 1 | 0 | 0 |
Example
The following code shows how to use the bitwise operators.
// w ww .j a va2s. c o m
using System;
class Program
{
static void Main(string[] args)
{
int i = 5;
int j = 6;
Console.WriteLine(i & j);
Console.WriteLine(i | j);
Console.WriteLine(i ^ j);
Console.WriteLine(i << 2);
Console.WriteLine(i >> 2);
}
}
The output:
AND Example
Using bitwise AND
class MainClass// ww w .j a va2s . c o m
{
public static void Main()
{
byte byte1 = 0x9a; // binary 10011010, decimal 154
byte byte2 = 0xdb; // binary 11011011, decimal 219
byte result;
System.Console.WriteLine("byte1 = " + byte1);
System.Console.WriteLine("byte2 = " + byte2);
result = (byte) (byte1 & byte2);
System.Console.WriteLine("byte1 & byte2 = " + result);
}
}
The code above generates the following result.
Example 2
Use bitwise AND to make a number even
using System; /*from w ww . ja v a 2s. c o m*/
class Example {
public static void Main() {
ushort num;
ushort i;
for(i = 1; i <= 10; i++) {
num = i;
Console.WriteLine("num: " + num);
num = (ushort) (num & 0xFFFE); // num & 1111 1110
Console.WriteLine("num after turning off bit zero: "
+ num + "\n");
}
}
}
The code above generates the following result.
Example 3
Use bitwise AND to determine if a number is odd.
using System; //from w w w . ja v a2s. co m
class Example {
public static void Main() {
ushort num;
num = 10;
if((num & 1) == 1)
Console.WriteLine("This won't display.");
num = 11;
if((num & 1) == 1)
Console.WriteLine(num + " is odd.");
}
}
The code above generates the following result.
Example 4
Use bitwise OR to make a number odd.
using System; /*w w w. j av a 2 s.c om*/
class Example {
public static void Main() {
ushort num;
ushort i;
for(i = 1; i <= 10; i++) {
num = i;
Console.WriteLine("num: " + num);
num = (ushort) (num | 1); // num | 0000 0001
Console.WriteLine("num after turning on bit zero: "
+ num + "\n");
}
}
}
The code above generates the following result.
Example 5
How to use bitwise NOT operator
class MainClass/* w ww .j a v a2s. c o m*/
{
public static void Main()
{
byte byte1 = 0x9a; // binary 10011010, decimal 154
byte byte2 = 0xdb; // binary 11011011, decimal 219
byte result;
System.Console.WriteLine("byte1 = " + byte1);
System.Console.WriteLine("byte2 = " + byte2);
result = (byte) ~byte1;
System.Console.WriteLine("~byte1 = " + result);
}
}
The code above generates the following result.
Example 6
How to use bitwise OR operator
class MainClass//from www .j a va 2s .c o m
{
public static void Main()
{
byte byte1 = 0x9a; // binary 10011010, decimal 154
byte byte2 = 0xdb; // binary 11011011, decimal 219
byte result;
System.Console.WriteLine("byte1 = " + byte1);
System.Console.WriteLine("byte2 = " + byte2);
result = (byte) (byte1 ^ byte2);
System.Console.WriteLine("byte1 ^ byte2 = " + result);
}
}
The code above generates the following result.