Author Topic: Need unsigned integers  (Read 11543 times)

ScarBelly

  • Newbie
  • Posts: 6
  • I'm a llama!
    • View Profile
Need unsigned integers
« on: March 02, 2012, 12:50:24 PM »
On an FMD1616 the following does not work for me when Input 16 is used due to it reversing the sign of the input as a signed integer.

If (INPUT[1]  > 0) AND (INPUT[1] > DM[1]) Then
         'DM[1] contains the last state of INPUT[1] and I'm looking for additional inputs On.
    SetBIT Relay[1],0      
EndIf

Is there a simple way around this?  Could you consider adding unsigned integers?
Thanks

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:Need unsigned integers
« Reply #1 on: March 02, 2012, 03:21:46 PM »
If you want to check for differences between two variables, then the best way is to use exclusive OR.

E.g.  DM[2] = INPUT[1] ^ DM[1]

DM[2] will only become zero if INPUT[1] and DM[1] are identical. Otherwise the bit that is ON indicate the differences between the two variables. This method does not depend on the order in which the bits are turned ON.

Unsigned integer is not supported. However, you may assign them to 32-bit integer and mask out the upper bits so that you can compare them.

E.g.  DM[1] = &H9000    ' this is a negative number
        A = DM[1] & &HFFFF  ' this will mask out the upper 16-bit of extended sign so that A = &H00009000 instead of &HFFFF9000


Email: support@triplc.com
Tel: 1-877-TRI-PLCS

ScarBelly

  • Newbie
  • Posts: 6
  • I'm a llama!
    • View Profile
Re:Need unsigned integers
« Reply #2 on: March 05, 2012, 08:06:14 AM »
Thanks for the help :D
XOR won't help because I still need the 2nd greater than evaluation.
I was able to fix it using your 2nd suggestion.

A = INPUT[1] & &HFFFF
B = DM[1] & &HFFFF
If A <> B Then
   If (A  > 0) AND (A > B) Then   
       SetBIT Relay[1],0
       CLRBIT Relay[1],1      
   EndIf
'...........
EndIf