How to swap the first two bits of a number in C#

1 Answer

0 votes
using System;
    
class Program
{
    static void print_bits(int n) { 
        for (int i = 7; i >= 0; i--)
            Console.Write((n >> i) & 1);
        Console.WriteLine();
    } 
    static void Main()
    {
        int n = 162;
      
        print_bits(n);
            
        n ^= (1 << 0);
        n ^= (1 << 1);
          
        print_bits(n);
    }

}
    
    
/*
run:
    
10100010
10100001
 
*/

 



answered Mar 18, 2019 by avibootz

Related questions

1 answer 206 views
1 answer 95 views
1 answer 106 views
1 answer 101 views
1 answer 109 views
1 answer 106 views
1 answer 131 views
...