Moving 1 BIT from a variable to another
TC
Posts: 1,019
Hello all,
I am working a on a project where I have to split variables into bits, and but it into another variable. I need to take the BYTE data in a variable, split it into BITS, then put each bit into other variables.
I'll give you an idea... (I hope)
I need the output to be
I hope this makes sense, and I can bet there is a way to do it, but I don't know how or even where to start to figure it out.
Thanks
TC
I am working a on a project where I have to split variables into bits, and but it into another variable. I need to take the BYTE data in a variable, split it into BITS, then put each bit into other variables.
I'll give you an idea... (I hope)
VAR IN_DATA[10] OUT_DATA[7] IN_DATA[0] := %01100000 IN_DATA[1] := %11010000 IN_DATA[2] := %00000000 IN_DATA[3] := %11111111 IN_DATA[4] := %10101010 IN_DATA[5] := %01010101 IN_DATA[6] := %00001111 IN_DATA[7] := %11110000 IN_DATA[8] := %11001100 IN_DATA[9] := %00110011
I need the output to be
OUT_DATA[0] = %0101100110 ' BIT 7 OF EACH "IN_DATA" OUT_DATA[1] = %1101010110 ' BIT 6 OF EACH "IN_DATA" OUT_DATA[2] = %1001100101 ' BIT 5 OF EACH "IN_DATA"
I hope this makes sense, and I can bet there is a way to do it, but I don't know how or even where to start to figure it out.
Thanks
TC

Comments
If you want to find out if a certain bit is high, you can test by
PUB Main IF TestVal1 == 1 ' then your bit to test is high, else not PUB TestVal1 BitMask := %0100_0000 ResultVal := IN_DATA[0] & BitMask If ResultVal > 0 Return 1 ' If you need to move the bit to a position, then bitshift left ie Return 1 << 6 Else Return 0To extend this:
I don't know why I didn't think of AND or OR. That might just work, I will have to see if I can figure something out.
Thank you
repeat i from 0 to 2 repeat bitNum from 0 to 9 temp1 := IN_DATA[bitNum] & |< (7-i) ' Isolate bit from IN_DATA temp2 := temp1 <> 0 ' make all one bits or all zero bits temp3 := temp2 & |< bitNum ' reposition bit to go in OUT_DATA temp4 := OUT_DATA[i] & ! |< bitNum ' force output bit to zero OUT_DATA[i] := temp4 | temp3 ' move in new bitThere are fast ways to do this using shift instructions in assembly, but that's very different and this should be more than adequate.Thanks Mike,
I only care about bits 0 to 9 of DATA_OUTPUT. Taking your code and changing the names, I have this
repeat INDEX from 0 to 6 repeat bitNum from 0 to 9 temp1 := DISPLAY[bitNum] & |< (7-INDEX) ' Isolate bit from IN_DATA temp2 := temp1 <> 0 ' make all one bits or all zero bits temp3 := temp2 & |< bitNum ' reposition bit to go in OUT_DATA temp4 := DISP_BUFFER[INDEX] & ! |< bitNum ' force output bit to zero DISP_BUFFER[INDEX] := temp4 | temp3 ' move in new bitI have not tested it yet, but I am working on it right now.
Thank you