1、十进制转二进制

  1. 特点 遇到21
  2. var num = 216
  3. var arr = []
  4. while(num>0){
  5. arr.unshift(num%2)//0101
  6. num = Math.floor(num/2)//521
  7. }
  8. console.log(Number(arr.join("")));//11011000

2、二进制转十进制

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