-2

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));
VLAZ
  • 22,934
  • 9
  • 44
  • 60
Starlex
  • 1
  • 1
  • A relevant related question: [JavaScript triple greater than](https://stackoverflow.com/questions/7718711/javascript-triple-greater-than) – DBS May 25 '22 at 09:48

1 Answers1

0

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.

spender
  • 112,247
  • 30
  • 221
  • 334
  • Thank you for your clarification, I have understood the process, but I still do not know how (( >>> Zero fill right shift )) works with zero, like what is the result of ( -7 >>> 0 ) and (7 >>> 0 ) ? – Starlex May 25 '22 at 13:06