Shop OBEX P1 Docs P2 Docs Learn Events
Accessing Bits — Parallax Forums

Accessing Bits

SRLMSRLM Posts: 5,045
edited 2008-11-12 06:12 in Propeller 1
Hi

How do you get into the individual bits of a long? I'm working through a protocol that needs a start bit of 0 and a stop bit of 1, along with a parity bit.

Also, is there an easy way to get a bit that tells the odd parity? I was going to use something like a loop and two counters, but I suspect that there must be an easier way.

Thank you.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-11 05:12
    To get individual bits you have to use various combinations of shifts and logical and/or/xor.

    In assembly language, the logical operations put the parity of the result into the carry flag. In Spin there's no easy way. You can write an expression that xor's all the bits of interest together to get the parity. For serial communications, it's not too hard:

    char |= (((char >> 6) ^ (char >> 5) ^ (char >> 4) ^ (char >> 3) ^ (char >> 2) ^ (char >> 1) ^ char) << 7) & %10000000

    This converts a 7 bit value to 8 bits including odd parity.
  • nohabnohab Posts: 96
    edited 2008-11-11 12:51
    Interesting row... gives "high level language" a new meaning smile.gif

    Can you give a short lesson how to read/interpret that, please ?!
  • SRLMSRLM Posts: 5,045
    edited 2008-11-12 06:12
    What about 8 bits into a one bit parity?

    It looks like what you're doing is shifting bits right, then I get lost with the chevron and amperstand.

    Any explanation would be great. Also, I took a more beginer's approach and wrote a good 'ole for-loop.


    PUB getOddParity (Value) : bit | counter, index
    
      repeat index from 0 to 7
        'If the modulus of the number is even, then the last bit is a 0, if it's odd, then it's a 1
        if((value >> index) // 2 )
          'Must be a 1 bit at the end
          counter ++
        
    
      if(counter // 2)
        bit := 1
      else
        bit := 0
    
      if Value == 0
        bit := 1
          
    
    



    Looks okay to me. Are there any errors that anybody can spot?
Sign In or Register to comment.