Use bitwise AND to make a number even : Bitwise AND « Data Type « C# / CSharp Tutorial






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 & 0xFFFE); // num & 1111 1110 
 
      Console.WriteLine("num after turning off bit zero: " 
                        +  num + "\n");  
    } 
  } 
}
num: 1
num after turning off bit zero: 0

num: 2
num after turning off bit zero: 2

num: 3
num after turning off bit zero: 2

num: 4
num after turning off bit zero: 4

num: 5
num after turning off bit zero: 4

num: 6
num after turning off bit zero: 6

num: 7
num after turning off bit zero: 6

num: 8
num after turning off bit zero: 8

num: 9
num after turning off bit zero: 8

num: 10
num after turning off bit zero: 10








2.43.Bitwise AND
2.43.1.bitwise AND
2.43.2.Use bitwise AND to make a number even
2.43.3.Use bitwise AND to determine if a number is odd.