Shop OBEX P1 Docs P2 Docs Learn Events
Moving 1 BIT from a variable to another — Parallax Forums

Moving 1 BIT from a variable to another

TCTC Posts: 1,019
edited 2015-01-10 10:38 in Propeller 1
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)
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

  • T ChapT Chap Posts: 4,223
    edited 2015-01-10 09:57
    I am not sure from your code what you need to do specifically, but generally speaking you use AND or OR in conjunction with a MASK to manipulate bits.

    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 0
    
    


    To extend this:
    PUB TestVal1
      BitMask := %0100_0000
      OUT_DATA[0] :=  (IN_DATA[0]  & BitMask)  >> 7      ' write over whatever may be in out_data with  :=
      OUT_DATA[0] |=  (IN_DATA[1]  & BitMask)  >> 6     ' now OR whatever other values, so not overwriting below this point
      OUT_DATA[0] |=  (IN_DATA[2]  & BitMask)  >> 5
      OUT_DATA[0] |=  (IN_DATA[3]  & BitMask)  >> 4
    
      OUT_DATA[0] |=  (IN_DATA[4]  & BitMask)  >> 3
      OUT_DATA[0] |=  (IN_DATA[5]  & BitMask)  >> 2
      OUT_DATA[0] |=  (IN_DATA[6]  & BitMask)  >> 1
      OUT_DATA[0] |=  (IN_DATA[7]  & BitMask)  >> 0
    
    This is thrown together without carefully looking at your order of bits etc.  At least you get the basic idea of AND and OR, what I wrote will need to be tweaked.  
    
    
  • TCTC Posts: 1,019
    edited 2015-01-10 10:04
    The first part was not part of my code, I just made it for an example.

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2015-01-10 10:06
    I had originally written everything after the repeats as a single statement, but this is a bit easier to understand. It does assume that the other bits in OUT_DATA are not used or initialized to whatever's needed (like zero).
    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 bit
    
    There are fast ways to do this using shift instructions in assembly, but that's very different and this should be more than adequate.
  • TCTC Posts: 1,019
    edited 2015-01-10 10:23
    Mike Green wrote: »
    I had originally written everything after the repeats as a single statement, but this is a bit easier to understand. It does assume that the other bits in OUT_DATA are not used or initialized to whatever's needed (like zero).
    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 bit
    
    There 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 bit
    
    

    I have not tested it yet, but I am working on it right now.

    Thank you
  • TCTC Posts: 1,019
    edited 2015-01-10 10:38
    Thank you so much Mike. That worked perfectly, and it is easy to understand what is going on.
Sign In or Register to comment.