Yes - the chaos operator does this in a pseudo random way. In practice I don’t normally use this line and I usually set X from some other process (e.g. a voltage input) that I want to quantise. (Also I don’t normally have this script in metro - I usually put it in script 8, which I call whenever I want to quantise whatever is stored in X at the time.)
BSET sets a particular binary digit within a number on. This line gives us a number with just one of the 12 semitones set to “on” (controlled by X - the % throws away the octave information and just gives us a number between 0 and 11). Think of it like taking a zero and then flipping one bit to on.
This is a very condensed bit of code! Basically it shifts the bit (the semitone that’s going to play) rightwards (I.e. down the the scale) until that note coincides with a note that is in the scale. The logical and operator ‘&’ here is doing the work for us. It will return zero if the single bit that’s on in J doesn’t correspond to a bit that’s on in A and non-zero otherwise.
Hope this helps! I tried to do a line-by-line translation further up the thread. Have fun!
A quick tip: you can play around with some of these lines of code in the live mode to try and get a sense of what they do. For example, try typing BSET 0 1 or BSET 0 2 etc. in the live mode to see what gets returned by this operator. Also it’s helpful to run scripts with the variable view showing (press the back quote key in live mode) so you can keep track of what’s happening.