How to display the binary format of a value in JavaScript

3 Answers

0 votes
function toBinFormat(binary_value, n) {
    for (let i = 0; i < 8; i++) {
        binary_value[i] = n & 0x80 ? '1' : '0';
        n <<= 1;
    }
}
 
const value = 7; 
const arr = [0,0,0,0,0,0,0,0];

toBinFormat(arr, value);
 
console.log(value + ' = ' + arr);
 


  
/*
run:
 
"7 = 0,0,0,0,0,1,1,1"
  
*/

 



answered Jun 15, 2015 by avibootz
edited May 26, 2022 by avibootz
0 votes
function toBinFormat(binary_value, n) {
    return (binary_value >>> 0).toString(2);
}
 
const value = 7; 
 
console.log(toBinFormat(value));
 

  
/*
run:
 
"111"
  
*/

 



answered Jun 15, 2015 by avibootz
edited May 26, 2022 by avibootz
0 votes
const value = 7; 
 
console.log(value.toString(2));
 
 

  
/*
run:
 
"111"
  
*/

 



answered May 26, 2022 by avibootz

Related questions

1 answer 186 views
1 answer 172 views
2 answers 221 views
2 answers 144 views
1 answer 128 views
2 answers 197 views
...