How to toggle a bit at specific position in JavaScript

1 Answer

0 votes
function get_bits(n){
    return (n >>> 0).toString(2);
}
 
let n = 365
const pos = 2;
   
console.log(get_bits(n));
   
n ^= (1 << pos);
       
console.log(get_bits(n));
      
   
    
/*
run:
     
101101101
101101001
      
*/

 



answered Mar 20, 2019 by avibootz
edited Apr 3 by avibootz
...