How Bitwise operator |
works is explained in the doc here and video here. However, the purpose and meaning of |
seems difficult to grasp just by reading simple examples provided in the links.
I was reading the source code of d3.shuffle
, here is a line using |
:
i = Math.random() * m-- | 0;
based on the source code, this line of code seems intend to assign a random index between 0 and m to i
.
My question is :
If the intention of this line of code is to create a random index between 0 and m, then how exactly
|
help to achieve it?In other words, how to understand the practical use and meaning of using
|
here, as at this moment I understand no why, nor I can use|
in any other cases.to rephrase the second part: is there a sort of logic to help understand the three lines of codes altogether? or should we use the sense of rounding to understand these three lines below? If so, how? If not, what do the other two lines of codes mean?
i = Math.random() * m-- | 0;
i = Math.random() * m-- | 1;
i = Math.random() * m-- | 2;
Thanks!