How to swap the first two bits of a number in Python

1 Answer

0 votes
n = 162
     
print(bin(n))
   
n ^= (1 << 0);
n ^= (1 << 1);
 
print(bin(n))
      
      
       
'''
run:
   
0b10100010
0b10100001
   
'''

 



answered Mar 18, 2019 by avibootz

Related questions

1 answer 95 views
1 answer 105 views
1 answer 101 views
1 answer 109 views
1 answer 105 views
1 answer 130 views
...