C# Bitwise Operators

In this chapter you will learn:

  1. How to use Bitwise Operators
  2. Example for bitwise operators
  3. Using bitwise AND
  4. Use bitwise AND to make a number even
  5. Use bitwise AND to determine if a number is odd.
  6. Use bitwise OR to make a number odd.
  7. How to use bitwise NOT operator
  8. How to use bitwise OR operator

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,

pqp & q p | qp ^ q~p
0 0 0 0 0 1
1 00 11 0
0 1 0 1 1 1
1 1 1 1 0 0

Example

The following code shows how to use the bitwise operators.


//ww w.  ja  va 2 s  . 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.  ja 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 & 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; /*w w  w  .  j av a2 s.com*/
 
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 www .  ja  v a 2  s .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; /*from   w w  w . ja va2 s  .  co  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 | 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/*www . ja  v  a2 s . co  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//w w  w  .  ja  va2s  .com
{

  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.

Next chapter...

What you will learn in the next chapter:

  1. How to use shift operators
  2. left shift
  3. right shift
  4. Use the shift operators to multiply and divide by 2
Home »
  C# Tutorial »
    C# Language »
      C# Operators
C# Arithmetic Operators
C# Arithmetic Assignment Operator
C# Increment and Decrement Operators
C# Remainder operator
C# Relational operators
C# Conditional Operators
C# Ternary operator(The ? Operator)
C# Bitwise Operators
C# Bitwise Shift Operators
C# sizeof Operator
C# typeof Operator