Shop OBEX P1 Docs P2 Docs Learn Events
A problem about how to "combine the respective bits" — Parallax Forums

A problem about how to "combine the respective bits"

kevinspacekevinspace Posts: 56
edited 2011-05-18 04:31 in Propeller 1
Hello everyone !!
I encountered a problem which about how to "combine the respective bits" by 6 bits.

Example :
CON
    _clkmode = xtal1 + pll16x                           
    _xinfreq = 5_000_000
    
VAR long  Value[6]
OBJ
    Debug: "FullDuplexSerialPlus"
PUB Main
    Value[0] := 1
    Value[1] := 0
    Value[2] := 1
    Value[3] := 0
    Value[4] := 1
    Value[5] := 0
    
    Display
    
PUB Display
    Debug.start(31, 30, 0, 57600)  
    Debug.tx(16)
    
repeat    
    Debug.str(string("Display_Value_By_6Bit:", 13))       
    Debug.Bin(Value[6],6)          
    Debug.str(string(13))        
    waitcnt(clkfreq/2 + cnt)         

I want to display the 6bit as "010101" at a time !
How to do it ??
Are there any answers or suggesstions ?

Thanks a lot !!

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-05-14 05:41
    You don't really want to use a long to store a single bit value, do you? I think this is what you want to do:
    pub main  
    
      term.start(RX1, TX1, %0000, 115_200)
      waitcnt(clkfreq / 1000 + cnt)
    
      value := 0
      value := putbit(value, 0, 1)
      value := putbit(value, 1, 0)  
      value := putbit(value, 2, 1)  
      value := putbit(value, 3, 0)
      value := putbit(value, 4, 1)
      value := putbit(value, 5, 0)
    
      term.str(string("Value = "))
      term.bin(value, 6)
    
      repeat
        waitcnt(0)
    
    
    pub putbit(target, pos, bitval)
    
    '' writes bitval.0 to target.pos
    
      if (bitval & 1)
        return target | (1 << pos)
      else
        return target & !(1 << pos)
    

    Note that you don't need to re-start FDS every time you need it -- just set it up at the top of your program.
  • kuronekokuroneko Posts: 3,623
    edited 2011-05-14 20:29
    JonnyMac wrote: »
    pub putbit(target, pos, bitval)
    
    '' writes bitval.0 to target.pos
    
      if (bitval & 1)
        return target | (1 << pos)
      else
        return target & !(1 << pos)
    
    While I can't get warm with calling a method to un/set a bit have you considered this version?
    PUB putbit(target, pos, bitval)
    
      dirb := target
      dirb[pos] := bitval
      return dirb
    
  • kevinspacekevinspace Posts: 56
    edited 2011-05-18 04:31
    Thanks for JonnyMac and kuroneko~

    The question have been solved !! 
Sign In or Register to comment.