1

I searched for a methode to create a random hex color in JavaScript.

What I found was this: '#'+(Math.random()*0xFFFFFF<<0).toString(16); I found this here.

I don't like to copy code I don't understand. I think I got everything except one tiny detail. This part right here: <<0. It's in there to cut all numbers after the point by moving the binary code to the left by "0". But why does it cut the numbers? If I use a binary calculator and input something like 13 and 13.2, it gives me the same result. So what does moving the whole thing by 0 cut the point numbers?

I created a fiddle to demonstrate what I mean:

https://fiddle.jshell.net/ucjb3quL/

I hope you can help me

Nicolo Lüscher
  • 595
  • 7
  • 22
  • 2
    The example you found is Java*Script*, not Java. Is your question about JavaScript? Note that Java and JavaScript are two different languages, and the line you quoted is not valid in Java. – Jesper Oct 05 '17 at 11:43
  • I'm sorry, I meant JavaScript (like mentioned in the question) I think i just typed Java and hit enter :) my bad. – Nicolo Lüscher Oct 05 '17 at 11:45

0 Answers0