Even of Odd?

I don't know what type of number you are looking at, or what type of system you are using. But IsOdd() should probably work
 
If your plc allows you to refer to integers at bit level then you can simply use the status of the least significant bit of your number:

0 = even
1 = odd
 
Look at the binary.

1 = 0001 - odd
2 = 0010 - even
3 = 0011 - odd
4 = 0100 - even
5 = 0101 - odd
6 = 0110 - even
7 = 0111 - odd

Do you see a pattern?

Check the least significant bit. If its a 1 then the number is odd.

In ladder

MyInteger.0 IsOdd
----] [---------------------( )--




PS. you can also use a similar trick to see if a number is negative by checking the most significant bit; if it is a 1 then the number is negative.
 
An interesting original question - and my first thought was "he has to be asking about integers" (i.e. whole numbers).

Then the thought occurred, "If 4.000000 is even, is 4.000001 even or odd, or neither" ?
Can the concept of even-ness be applied to floating point numbers, taking into account the degree of imprecision of them?


EDIT : As always, Wikipedia provides the answer... even-ness applies only to Integer Numbers

http://en.wikipedia.org/wiki/Even_number
 
Last edited:
I like threads like this one. I tried this out on our system. If number is a INT or DINT I must convert the DINT to a DWORD first. According the the IEC specification I can't check bits of an integer. I tried
DINT_TO_DWORD(iNumber).0
but that didn't work but I think it should. I will bug the compiler writer about this.
I had to
wNumber:=DINT_TO_DWORD(iNumber);
Then check bit 0.
wNumber.0
 
It is only harder than it needs to be.

Which system is that Peter?

I find it hard to believe that any system would make it hard or impossible to access bit status of an integer when the system is dealing with that integer as a series of bits anyway!
I should have said our product. We are following the IEC specification where it makes sense. The IEC speficiation allows DWORD.0 but not DINT.0.

Customers want the IEC spefication but I really wonder if they know about all the stupid little things the IEC specificaitons makes difficult and it is so wordy.

This works
Odd:=(iTemp and 1)<>0; // Even this is a violation......., I mean extension of the specification. It should be
Odd:=(DINT_TO_DWORD(iTemp) and 1)<>0; // That is a lot of typing to do so little. What a pain.
This does too
Odd:=wTemp.0;
but not this
Odd:=iTemp.0;

I also hate the DINT_TO_DWORD() function. Too long and does nothing. I prefer simply DWORD(). This would convert the agrument to a DWORD whether it is a DINT or REAL.
Then I could type
Odd:=DWORD(iTemp).0;
This is less wordy but still keeps things type safe.
 
Last edited:
Which system is that Peter?

I find it hard to believe that any system would make it hard or impossible to access bit status of an integer when the system is dealing with that integer as a series of bits anyway!

That is actually a bit (NPI) backwards. The processors in PLC's, computers, well, everything actually deal with single bits as whole words (8/12/16/32/64/any other bit length is dependent on the system).
I can't think of any processor on the market (though I don't know all of them) that have bit level addressing.

For that matter, many processors don't have any direct access to the bits of a word, requiring the use of bitwise logical instructions (and, or, xor).
 

Similar Topics

I have an Allen Bradley temperature switch that I am trying to use in studio 5000. I am getting the message "Unable to interpret the IODD file"...
Replies
0
Views
69
So I had an odd request from a customer for the above. I have written the logic and tested it all in one PLC with only using 7 outputs and 7...
Replies
15
Views
428
I have been requested to test this proportioning valve for PLC control of flow/pressure. Dwyer Series SVP Proportioning Solenoid Valve The flow...
Replies
10
Views
421
Howdy guys - Looking for some insight on the MAM instruction. Had an interesting one today: I performed a controller upgrade on an older system...
Replies
1
Views
437
Hi everyone, curious if anyone has ever witnessed the following comm error: A little history, I had a department PC die, WIN 7. Had to upgrade...
Replies
4
Views
624
Back
Top Bottom