I do not understande code below which is used to convert decimal number to a binary.
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
console.log(dec2bin(-5));
I do not understande code below which is used to convert decimal number to a binary.
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
console.log(dec2bin(-5));
dec >>> 0
is an unsigned 32-bit right-shift of zero.
This has the effect of
a) truncating the non-integer part of the number
b) converting the number to 32-bit
c) removing the sign bit from the 32-bit number
someNumber.toString(2)
will convert to a string representation of the binary value of someNumber
.
function dismailToBinary(num) {
let binary = "";
while (num > 0) {
let digit = num % 2;
binary = "" + digit + binary;
num = parseInt(num / 2);
}
return binary;
}
console.log(dismailToBinary(5));
function decimalToBinary(num) {
let binary = "";
while (num > 0) {
let digit = num % 2;
binary = "" + digit + binary;
num = parseInt(num / 2);
}
return binary;
}
console.log(decimalToBinary(5));