Shop OBEX P1 Docs P2 Docs Learn Events
Spin : Unsigned Longs — Parallax Forums

Spin : Unsigned Longs

hippyhippy Posts: 1,981
edited 2007-11-12 16:48 in Propeller 1
To save re-inventing the wheel, a wad of testing, debugging and proving, does anyone have any code already written to handle unsigned long variables ...

* To add two numbers with overflow detected

* To subtract two numbers with underflow detected

* To compare two numbers

I've got a set of compare routines but cannot help thinking there must be a better way to do it ...

CON

  LT = %100
  EQ = %010
  GT = %001
  LE = %110
  NE = %101
  GE = %011

PRI CompareUnsigned( lhs, rhs )

  if lhs == rhs         ' lhs rhs
    result := EQ        ' ??? ??? => lhs EQ rhs
  else
    if lhs => 0
      if rhs => 0
        if lhs > rhs
          result := GT  ' +Ve +Ve => lhs GT rhs
        else
          result := LT  ' +Ve +Ve => lhs LT rhs
      else
        result := LT    ' +Ve -Ve => lhs LT rhs
    else
      if rhs => 0
        result := GT    ' -Ve +Ve => lhs GT rhs
      else
        if lhs > rhs
          result := GT  ' -Ve -Ve => lhs GT rhs
        else
          result := LT  ' -Ve -Ve => lhs LT rhs

PRI IsUnsignedLt( lhs, rhs )
  result := CompareUnsigned(lhs,rhs) == LT

PRI IsUnsignedEq( lhs, rhs )
  result := lhs == rhs

PRI IsUnsignedGt( lhs, rhs )
  result := CompareUnsigned(lhs,rhs) == GT

PRI IsUnsignedLe( lhs, rhs )
  result := ( CompareUnsigned(lhs,rhs) & LE ) <> 0

PRI IsUnsignedNe( lhs, rhs )
  result := lhs <> rhs

PRI IsUnsignedGe( lhs, rhs )
  result := ( CompareUnsigned(lhs,rhs) & GE ) <> 0



Post Edited (hippy) : 11/12/2007 4:57:59 PM GMT
Sign In or Register to comment.