1、十进制转化二进制

  1. <script>
  2. var num = 10;
  3. var arr = [];
  4. while(num>0){
  5. arr.unshift(num%2);
  6. num = Math.floor(num/2);
  7. }
  8. console.log(Number(arr.join("")))
  9. </script>

2、二进制转十进制

  1. <script>
  2. var num = 1010;
  3. str = (num+"").split("").reverse();
  4. console.log(str);
  5. var sum = 0;
  6. str.forEach((item,index)=>{
  7. console.log(item);
  8. console.log(index);
  9. sum+=item*Math.pow(2,index);
  10. })
  11. console.log(sum)
  12. </script>