Shop OBEX P1 Docs P2 Docs Learn Events
max and min of 2's complement — Parallax Forums

max and min of 2's complement

pgbpsupgbpsu Posts: 460
edited 2009-04-16 13:19 in Propeller 1
I'm trying to grab the max and min values (in SPIN) from an array of 24-bit 2's complement data. Any clever suggestions? I'm afraid I need to convert all the data into signed ints, then scan them. Is there a better way, a way to determine this without converting them first?

Thanks,
Peter

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-16 02:11
    I assume you have 24 bit 2's complement values stored in an array of 32 bit longs. I suggest you convert them in the process of scanning for the maximum and minimum like this:
    CON tableSize = 32
    VAR long theMax, theMin, table[noparse][[/noparse]tableSize]
    PRI findMinMax | i, temp
       theMin := POSX   ' Anything is lower than this
       theMax := NEGX   ' Anything is higher than this
       repeat i from 0 to tableSize-1   ' Go through the table
          temp := (table[noparse][[/noparse] i ] << 8) ~> 8   ' Convert to signed long
          theMin <#= temp   ' Find the minimum value in the table
          theMax #>= temp   ' Find the maximum value in the table
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-04-16 02:18
    You don't need to do complete conversions during your scan. Just shift them all left by 8 bits to compare; then, when you're done, arithmetically shift the min and max right by eight bits to get your result. Spin makes this kind of testing easy with its limit minimum and limit maximum operators:

    x := candidate << 8
    minval <#= x
    maxval #>= x
    
    
    


    -Phil
  • pgbpsupgbpsu Posts: 460
    edited 2009-04-16 13:19
    Mike and Phil-

    Thank you both for the prompt and useful suggestions. I should have know shifting was required because my sign bit isn't in bit 31. I think either one of these solutions will work quite well.

    Thanks,
    Peter
Sign In or Register to comment.