补码

60 阅读1分钟
function negativeToBinary(negative){

    let binary='';

    let n=negative;

    while(n<0){

       

        let y=n%2;

        binary=y+binary;

        n=Math.trunc(n/2);

    }

    

    let num=32-binary.length;

    for(let i=0;i<num;i++){

        binary='0'+binary;

    }

    return binary;

}



let negative=-81;

let comp=negativeToBinary(negative);

console.log(`${negative}的补码表示为${comp}`);