Shop OBEX P1 Docs P2 Docs Learn Events
Variable help — Parallax Forums

Variable help

TCTC Posts: 1,019
edited 2013-04-27 07:37 in Propeller 1
Hello all,
First, please excuse my vagueness of my question. I can see what I need to do in my head, but it is hard for me to explain what I am talking about.
I am trying to find a way to change the value of a byte sized variable. I have (4) 2-bit sized values that need to make up that byte sized variable.

For example: value_A (2-bits), value_B (2-bits), value_C (2-bits), value_D (2-bits)

I was thinking of along the lines of using "<<=2" to shift the bits 2 places to the left after each value is loaded, but I don't know how I can load my value without changing what I already loaded into the variable.

I hope this makes sense.

Thank you in advance.

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-04-27 05:15
    If the source(s) are only 2 bit each then this will do:
    e := a << 6 | b << 4 | c << 2 | d
    
    If you can't guarantee 2 bit size then they have to be masked before or after shifting but before the final assembly.
  • TCTC Posts: 1,019
    edited 2013-04-27 05:23
    Wonderful!! Thank you very much.

    I can guarantee they are 2-bit values.
  • TCTC Posts: 1,019
    edited 2013-04-27 05:40
    kuroneko, I am trying to understand what is going on with your help to my problem. I might be over thinking this.

    start off with
    %00000000
    load value_A
    %00000011
    shift to the left 6 places
    %11000000
    "OR" Value_B
    %11000010
    shift to the left 4 places
    %00100000
    Value_A is gone.

    Again, I might be over thinking this.
  • kuronekokuroneko Posts: 3,623
    edited 2013-04-27 05:42
    Shift has a higher priority than binary OR (see datasheet section 6.2), IOW first all << ops are executed then all | ops. It's the same as
    e := (a << 6) | (b << 4) | (c << 2) | d
    
  • TCTC Posts: 1,019
    edited 2013-04-27 05:53
    Ah, now I get it. I knew I was over thinking it.

    Thank You
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-04-27 07:34
    Even though parenthesis are not required, I often use them for clarity. The code snippet in this thread is a really good example of that.
  • TCTC Posts: 1,019
    edited 2013-04-27 07:37
    It helped me understand it better.
Sign In or Register to comment.