Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,995 questions

51,940 answers

573 users

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 168 views
1 answer 156 views
2 answers 208 views
2 answers 127 views
1 answer 115 views
2 answers 173 views
...