Shop OBEX P1 Docs P2 Docs Learn Events
Case statement question — Parallax Forums

Case statement question

Don MDon M Posts: 1,653
edited 2012-09-12 14:52 in Propeller 1
For variable d which is a long I cant get the second line to work:

d = $52 for example
d := serial.rx

case d
  $0B:  Do this....

  (d & %0_01000000) == $40:  Do that...


I know the first line $0B works fine but the second line doesn't even though the equation does = $40. Maybe I'm stating this wrong? If I use it in an IF statement like this it works:
d := serial.rx

case d
  $0B:  Do this...

if (d & %0_01000000)
  Do that...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-09-12 14:30
    That's not how the CASE statement works. It calculates the expression on the CASE statement line, then essentially compares that to each of the expressions on each of the selection lines. Essentially it compares d to (d & %0_01000000) == $40 which always has the value 0 or $FFFFFFFF since it's a Boolean expression. That's not what you want. I suggest using the IF statement instead.

    You could try
    CASE d
       $0B:   ' This
       $40..$7F:   ' That
    
    Depending on what you want to do when the 8th bit is set, you might need to use $40..$7F, $C0..$FF:
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-09-12 14:52
    It gets more complicated, still, especially since he's dealing with 9-bit values:
    CASE d
       $0B:   ' This
       $40..$7F, $c0..$ff, $140..$17f, $1c0..$1ff:   ' That
    
    I think I'd go with the if ... elseif syntax for this one.

    -Phil

    Addendum: I suppose this could work:
    CASE true
      d == $0b: 'This
      d & $40 <> 0: 'That
    

    -P.
Sign In or Register to comment.