Shop OBEX P1 Docs P2 Docs Learn Events
Arghh... this should be simple (I think!) But I can't figure it out. Help please? — Parallax Forums

Arghh... this should be simple (I think!) But I can't figure it out. Help please?

RforbesRforbes Posts: 281
edited 2012-10-18 14:12 in Propeller 1
Heya folks,

I'm trying to figure out a way to do some bit manipulations, but I think I've gotten myself all discombobulated and a little catiwampus in the process.

The binary value of 1011_0111_0111_1011_1110_1111 is equal to 12,024,815...

I have a long named DEC_VALUE that := 12,024,815

I am trying to figure out how to decode this long (using spin only if possible) into an array of 24 BYTES such that each will either be a value of 0 or 1 based on the binary value and bit# of DEC_VALUE. Basically, I'd like to use each of the byte values in this or that method for any number of things to happen.

Example

VAR
Long DEC_VALUE
BYTE BIN_VALUE[24]

PUB Main

DEC_VALUE:= 12_024_815 'Assign a value to DEC_VALUE

BIN_VALUE[0]:= 'this should be made to equal 1
BIN_VALUE[1]:= 'this should be made to equal 0
BIN_VALUE[2]:= 'and so on so forth

If BIN_VALUE[0]:= 1 '1st bit is 1?
Neato 'Run this method

If BIN_VALUE[1]:= 1 '2nd bit is 1?
NeatoToo 'Run this method


I understand how to do this with IO pins and using OUTA, (example: OUTA[0..23]:=DEC_VALUE) but that's where I've fallen off my brainwave.

Any assistance? Please? Pretty please? With sugar on it?

Thanks in advance,
Robert

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-10-18 14:01
    Try something like this:
      dec_value := 12_024_815
      mask := 1 << 23
      repeat i from 0 to 23
        bin_value[i] := (dec_value & mask) >> (23 - i)
        mask >>= 1
    

    BTW,

    attachment.php?attachmentid=78421&d=1297987572

    -Phil
  • RforbesRforbes Posts: 281
    edited 2012-10-18 14:12
    Phil,

    Haha... wow. Yep, I'm glad I asked this question. I'd never have come up with that on my own. Gonna have to try it tonight when I get some "relax time" and get my head around it! Thanks so very much.

    Oh, and thanks again for the link! I was wondering how you guys do all the cool stuff around here. :)

    Robert
Sign In or Register to comment.