CX Programming Two's Complement

kevilay

Member
Join Date
Feb 2009
Location
Ontario
Posts
174
Hey Guys,

I am doing some changes on an older omron CJ2M plc using CX programmer. I am adding an ethernet device that is actually using a Two's Complement method. This is the first time I have ever seen this and it is very odd. Any suggestions to convert this to a regular signed dint?

The first digit is a sign, but when the sign is high everything is negitive and off by one.

EX.

5
0000 0101

-5
11111011

For the -5 you have to invert the bits then add 1.


Here is a calculator to show how it works:
https://www.exploringbinary.com/twos-complement-converter/

Please help!
 
Consider it this way (I originally saw it from @Ron Beaufort, but it could have originated elsewhere):

You can think of the bits after the sign bits as supplying (if 1) or not (if 0) a value that is a power of 2. For example, in a 16-bit unsigned (i.e. not twos complement) integer: the least-significant bit 0 has a value of 1 or 0; bit 1 has a value of 2 or 0; bit 2 has a value of 4 or 0; ...; bit 14 has a value of 16384 or 0; bit 15 has a value of 32768 or 0. So the possible range of values is 0-65535.

For twos complement, everything up to bit 14 is the same, and the sign bit 15 has a value of -32768 or 0; note that minus sign before the 32768!

So, if all of the bits 0-14 are 1s, then the sum to that point is 32767. So the sign bit, if 1, will add -32768, and the final 16-bit signed (twos complement) integer's value is (32767 + -32768) = -1. If all the the bits 0-13 are 1s, bit 14 is 0, and the sign bit is 1, then the sum is (32766 + -32768) = -2. And so on e.g. if all of the bits 0-14 are 0s, and the sign bit is 1, then the sum is (0 + -32768) = -32768. If bit 0 is 1, and bits 1-14 are 0s, and the sign bit is 1, the sum is (1 + -32768) = -32767.

So the "normal" unsigned sequence of

  • 0x0000 (0), 0x0001 (1), ..., 0x7ffe (32766), 0x7fff (32767), 0x8000 (32768), 0x8001 (32769), ..., 0xfffe (65534), 0xffff (65535)
becomes a signed (twos complement) sequence of

  • 0x0000 (0), 0x0001 (1), ..., 0x7ffe (32766), 0x7fff (32767), 0x8000 (-32768), 0x8001 (-32767), ..., 0xfffe (-2), 0xffff (-1)
 
Last edited:
Sorry, I just re-read your original post, and I don't think my answer helped you much.

What is coming in from this ethernet device i.e. how many bits?

And how do you want to represent that in in the CJ2M i.e. again how many bits?
 
The neg instruction seems to be working for what I need. It turns it into a positive number but for that I can do a work around.
 

Similar Topics

Hi all, i am the new controls guy at the plant and i have inherited a pc from the previous controls guy with Siemens tia portal version 16 and 17...
Replies
16
Views
506
I need to pull the program off of an old 90-30 so I can convert it to Allen Bradley. This is my first time messing with GE and I don't have the...
Replies
2
Views
80
New to vfds. I put in parameters. IP, but I get ethernet flashing and link solid. What did I do wrong?
Replies
9
Views
446
I'm been deciphering a program for a press here. I've gotten most of it deciphered using the manual to understand the instructions (first mitsu...
Replies
11
Views
259
Hello colleagues, Some time ago I started my adventure with programming. With your help and the courses, things are starting to come together...
Replies
13
Views
652
Back
Top Bottom