How do I grab specific bits from a 32 bit long?
tosjduenfs
Posts: 37
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
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
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.
Here's a simple method that returns target bits in a value (using shifting and logic operations):
Mike
result := value >< (msb+1) >< (lsb+1)
That with bit numbering from 0 to 31.
http://obex.parallax.com/object/427