Another use is for bit-level comparisons or extractions, such as for example seperate color values into R, G and B.
will print:
00000000111011110011001100111000
239 (red)
51 (green)
56 (blue)
Here the "& FF0000" and such will narrow down the string to just the portions o fthe string we want, and the SHR command will scroll the output to the right side of the string. shr 16 is the same as dividing by 65536, but:
- shr 16 can be an easier visual aid if you're familiar with binary
- shr and shl are faster than dividing or multiplying, since there is no general-purpose math routines involved. If you know you'll be dividing or multiplying by powers of two, shr and shl are a nice optimization.
col=15676216 print bin(col) print (col & $FF0000) Shr 16 ' RED print (col & $00FF00) Shr 8 ' GREEN print (col & $0000FF) ' BLUE
will print:
00000000111011110011001100111000
239 (red)
51 (green)
56 (blue)
Here the "& FF0000" and such will narrow down the string to just the portions o fthe string we want, and the SHR command will scroll the output to the right side of the string. shr 16 is the same as dividing by 65536, but:
- shr 16 can be an easier visual aid if you're familiar with binary
- shr and shl are faster than dividing or multiplying, since there is no general-purpose math routines involved. If you know you'll be dividing or multiplying by powers of two, shr and shl are a nice optimization.