setting a range of variables

in Propeller 1
Will this set a range of bytes
Thanks
Aaron
VAR
byte flags[7]
PRI SetFlags 'set flags according to key press
IF din[0] == $01
flags[0] := 1 '1st byte = 1
flags[7-1] := 0 'all other bytes = 0
CON
'Will this set a range of bytes? Or is there a different way? ( .. ) didn't work
Thanks
Aaron
Comments
BYTEFILL(@flags[1],0,6)
Look up BYTEFILL in the Propeller manual for detailsif (din[0] == 1) flags := |<0
This will set bit0 of flags and clear the other bits.I just thought there might be an easier way. I might have to read up on CASE also
Aaron
flags := |<4 | |<1
...will set bits 4 and 1 while clearing the others. Two flags set; six flags cleared; one line of code.The bytefill() instruction does set one or many bytes in a single instruction, but they must be the same value.
They only need to be 1 or 0 so that will work fine. I'll experiment with it early tomorrow morning.
Thanks
Aaron
pub get_bit(value, pos) '' Returns bit (0..1) from pos (0..31) in value if ((pos => 0) and (pos =< 31)) return (value >> pos) & 1 else return 0 pub set_bit(value, pos, bit) '' Write bit (0..1) to position (0..31) in value if (bit) ' set return value | |<pos else return value & !(|<pos)
Something that I suggest you do to make your code more readable is to create CON masks for your flags.
con LIVING_RM = %00000001 DINING_RM = %00000010 KITCHEN = %00000100
This allows you code to make more sense -- when you do things like this
flags |= LIVING_RM
My point is to be careful using "magic numbers" like I did in my example above. You can still set/clear multiple flags with one line, and make it readable:
You can test with a named flag easily, too:
if (flags & KITCHEN) kitchen_lights(OFF)