What is this structure: w[something]...
in Propeller 1
Hello,
I am muddling through .spin and would like to know what this structure represents, in plain English please.
w[btm_line]:=((?rand & $07) * 8)
I am familiar with BASIC from the 80's and this statement has me stumped. Clearly something (an array?) is being defined as being equal to a random number, which is logically combined to $07 for some reason, etc . Any help would be appreciated.
Thank you.
KB
I am muddling through .spin and would like to know what this structure represents, in plain English please.
w[btm_line]:=((?rand & $07) * 8)
I am familiar with BASIC from the 80's and this statement has me stumped. Clearly something (an array?) is being defined as being equal to a random number, which is logically combined to $07 for some reason, etc . Any help would be appreciated.
Thank you.
KB
Comments
Random ‘?’ The Random operator is a special, immediate operator that uses a variable’s value as a seed to create a pseudo random number and assigns that number to the same variable. It can only be used in run-time variable expressions. Random has two forms, forward and reverse, depending on which side of the variable it appears on. The forward form appears to the left of a variable and the reverse form appears to the right of a variable. Random generates pseudo-random numbers ranging from -2,147,483,648 to +2,147,483,647. It’s called “pseudo-random” because the numbers appear random, but are really generated by a logic operation that uses a “seed” value as a tap into a sequence of over 4 billion essentially random numbers. If the same seed value is used again, the same sequence of numbers is generated. The Propeller chip’s Random output is reversible; in fact, specifically it is a 32-bit maximum-length, four-tap LFSR (Linear Feedback Shift Register) with taps in both the LSB (Least Significant Bit, rightmost bit) and the MSB (Most Significant Bit, leftmost bit) allowing for bi-directional operation. Think of the pseudo-random sequence it generates as simply a static list of over 4 billion numbers. Starting with a particular seed value and moving forward results in a list of a specific set of numbers. If, however, you took that last number generated and used it as the first seed value moving backward, you would end up with a list of the same numbers as before, but in the reverse order. This is handy in many applications. Here’s an example: ?X The above shows the Random forward form; it uses X’s current value to retrieve the next pseudo-random number in the forward direction and stores that number back in X. Executing ?X again results in yet a different number, again stored back into X. X? The above shows the Random reverse form; it uses X’s current value to retrieve the next pseudo-random number in the reverse direction and stores that number back in X. Executing X? again results in yet a different number, again stored back into X. Since Random is always an assignment operator, the rules of Intermediate Assignments apply to it (see page 147).
So, your "?rand" is a "Random forward form", using the value of the rand variable to generate a new pseudo-random number. The "& $07" logically ANDs the rand variable value with $07, which is a binary 00000111 (masking the 3 rightmost bits).dgately
The rest should be straightforward if you remember that the assignment statement is written using ":=" rather than just an equal sign.
KB
"x := y" changes the variable (or array element) x to have the value y.
Here's how to initialize an array so that each element is equal to 0:
VAR long a[8] ' an array of 8 longs PUB cleara | i repeat i from 0 to 7 a[i] := 0
This would be written in BASIC as (I've used the fastspin dialect, since that's one of the BASICs that runs on the propeller):dim a(7) ' an array of 8 integers, indexed from 0 to 7 sub cleara dim i for i = 0 to 7 a(i) = 0 next i end sub
longfill(@a, 0, 8)
Of course, there is also bytefill and wordfill.Spin borrows Pascal's assignment operator for variables. For constants as single = sign is used. To test for equality, use ==. For example: This will set z to true if x and y are equal, false otherwise.