Shop OBEX P1 Docs P2 Docs Learn Events
How do I grab specific bits from a 32 bit long? — Parallax Forums

How do I grab specific bits from a 32 bit long?

tosjduenfstosjduenfs Posts: 37
edited 2013-10-18 09:49 in Propeller 1
Hi guys

My name is Mike, I'm relatively new to the prop but not programming in general. It has however been quite a long time since I've done any real code writing.

The real time clock object in the prop tool outputs time in Microsoft Time format, which is basically a 32 bit long where certain ranges of the bits represent seconds, hours, etc. I'm trying to figure out how to isolate certain ranges of the bits so I can convert them into meaningful numbers.

For example If I have a 32 bit long how would I look at the 23rd bit, or see bits 4 through 9.

I've looked through the prop manual and nothing obvious popped out at me, and truthfully I'm not really even sure what to search to get an answer.

I'd appreciate any help.

Thanks,

Mike

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-10-11 09:58
    There are two ways to do this. The first is by using standard bit logical operations and shifts. The second is by using some non-existant registers.

    To isolate bit 23 of X and present it as 0 or 1 do: (X >> 23) & 1

    To isolate bits 4 through 9 and present as 0-63 do: (X >> 4) & $3F

    The I/O registers OUTB and DIRB don't really exist as registers, but there are associated cog memory locations and the Spin notation that allows bit access works for these

    To access bit 23 of X, you'd copy X to OUTB with: OUTB := X, then you'd use: OUTB[ 23 ]

    To access bits 4 through 9 of x, you'd copy X to OUTB, then you'd use: OUTB[ 9..4 ]. This puts bit 9 as the most significant bit.

    The Propeller Manual and the Propeller Tool help files have more information on these operators and notations.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-10-11 10:18
    Mike Green's suggestion is easiest (no method code required), but I'm not a fan of using OUTB in applications as I assume anything I've written for the Propeller 1 will be ported to the Propeller 2.

    Here's a simple method that returns target bits in a value (using shifting and logic operations):
    pub rd_bits(value, msb, lsb)
    
      value >>= lsb                                                 ' move target lsb to bit0
      value &= ($FFFF_FFFF >> (31 - (msb - lsb)))                   ' mask off other bits
    
      return value
    
  • tosjduenfstosjduenfs Posts: 37
    edited 2013-10-11 16:17
    Thanks guys, those methods were very helpful. I got what I needed. I appreciate it. Could someone tell me how to change the flag to solved?

    Mike
  • Mike GreenMike Green Posts: 23,101
    edited 2013-10-11 20:07
    You go back to the first message of the thread and click on Edit Post, then Go Advanced. That will allow you to change the thread title including the Solved / Unsolved flag.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-10-12 09:50
    Another way, using the bit-reverse operator twice:
    result := value >< (msb+1) >< (lsb+1)

    That with bit numbering from 0 to 31.
  • dbpagedbpage Posts: 217
    edited 2013-10-18 09:49
    The object "Nibble" in OBEX demonstrates how to manipulate bits. This object defaults to 4-bit nibbles, but you could modify the code to manipulate any number of bits to do more than just grab bits.

    http://obex.parallax.com/object/427
Sign In or Register to comment.