Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Logical Not operator in script PLCs?
#1
I had expected to be able to use the unary negation operator (!) yet I can find no documentation supporting this and it causes an error. How do I do a logical negation in a script plc?
Reply
#2
You need to use parenthesis around the condition.

open plc 1
if(!(p2)){
p1++;
}
close
Reply
#3
(05-04-2009, 04:23 PM)brad Wrote: You need to use parenthesis around the condition.

open plc 1
if(!(p2)){
p1++;
}
close
This seems to only work within conditionals?
Still have problems. Consider setting up a t-flop:

state = !state; //fail
state = !(state); //fail
state != state; //fail

state += 1 // works, but at best is "trick" programming to be avoided
it also leaves not havint a unary negation in complex logical assignments.
Reply
#4
In Power PMAC script we do not have a Type Bool. So this brings up some questions concerning how does ! peroform on Doubles. If we follow the "C" world then we have a perfect example of some of the problems. On an x86 you can do ! with doubles. But in the powerpc architecture it works differently. I do specifically remember 20yrs ago a professor saying don't do that kind of stuff because it won't work on some computers. Here is an example

#include <stdio.h>

int main(void)
{
double a = 0.0;
int b = 0;

b = !b;
printf("b = %d\r\n",b);
b = 34;
b = !b;
printf("b = %d\r\n",b);

a = !a;
printf("a = %lf\r\n",a);

a =4.2;
a = !a;
printf("a = %ld\r\n",a);
};

x86 Output
----------
root@lt-linuxhenry:/opt/eldk-4.2/debian_gdm/root# ./a.out
b = 1
b = 0
a = 1.000000
a = 0

PowerPC Output
-----------
root@10.34.9.210:~# ./a.out

b = 1
b = 0
a = 1.000000
a = 267339300

This output goes back to the ~ function. In Power PMAC script our unary inversion is done with "~", not "!". It is a bit-by-bit inversion.

For a Boolean value (is this what you want?) it changes between 1 and 0.

So with: M1->*u.1

M1=~(M1)

will invert the logical state of M1.

With multi-bit variables, it inverts all bits.

So with: M2->*u.8

M2=1
M2=~(M2)

the value of M2 ends up as 254.

We have "~" documented as a bit-by-bit operator. On reflection, it should be
listed as a function, because the argument must be in parentheses.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)