Use bitwise OR to make a number odd. : Bitwise OR : Data Type C# Examples


C# Examples » Data Type » Bitwise OR »

 

Use bitwise OR to make a number odd.









    
using  System;  
  
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");    
        }  
    }  
}
    
   
  
   



Output

num: 1
num after turning on bit zero: 1

num: 2
num after turning on bit zero: 3

num: 3
num after turning on bit zero: 3

num: 4
num after turning on bit zero: 5

num: 5
num after turning on bit zero: 5

num: 6
num after turning on bit zero: 7

num: 7
num after turning on bit zero: 7

num: 8
num after turning on bit zero: 9

num: 9
num after turning on bit zero: 9

num: 10
num after turning on bit zero: 11


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Type
» Bitwise OR