JeffLowe Posted May 4, 2009 Share Posted May 4, 2009 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? Link to comment Share on other sites More sharing options...
bradp Posted May 4, 2009 Share Posted May 4, 2009 You need to use parenthesis around the condition. open plc 1 if(!(p2)){ p1++; } close Link to comment Share on other sites More sharing options...
JeffLowe Posted May 7, 2009 Author Share Posted May 7, 2009 [quote='brad' pid='156' dateline='1241479380'] You need to use parenthesis around the condition. open plc 1 if(!(p2)){ p1++; } close [/quote] 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. Link to comment Share on other sites More sharing options...
bradp Posted May 12, 2009 Share Posted May 12, 2009 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 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. Link to comment Share on other sites More sharing options...
Recommended Posts