# Forums.MrPLC.com: How to read SINT (short signed Integer) with CJ2 PLC - Forums.MrPLC.com

Page 1 of 1

## How to read SINT (short signed Integer) with CJ2 PLC Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://forums.mrplc.com/index.php?s=9bcf2a5cb222821fdb6f643f37a54c5b&amp;app=forums&module=ajax&section=topics&do=rateTopic&t=23627&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1   Nicolas  Posted 13 September 2012 - 05:40 PM

• Sparky
• Group: MrPLC Member
• Posts: 14
• Joined: 10-August 09
• Location:Houston TX
• Country:United States
I am reading some joystick data over CAN-OPEN, all analog values are 8 bit signed integers or Short Int. I know the first bit is the sign...
I can't find an easy way to read that Signed 8 Bit integer value into cx-programmer, using PLC instructions/conversion. I read about the NEG(160) two'd complement but I can't get it to work.
Any ideas?
0

### #2   Michael Walsh  Posted 13 September 2012 - 07:01 PM

• Omron Expert
• Posts: 440
• Joined: 03-April 06
• Gender:Male
• Location:Charlotte, NC
• Country:United States
The easiest way that I can figure to do it would be to use the ANDW function to mask out bits 7-15, leaving the absolute value of the short signed integer. Then look at the sign bit, if it is on, multiply the masked value by -1, if it is off, just move the value as shown below:

Michael
Charlotte, NC
0

### #3   Nicolas  Posted 14 September 2012 - 09:06 AM

• Sparky
• Group: MrPLC Member
• Posts: 14
• Joined: 10-August 09
• Location:Houston TX
• Country:United States

Michael Walsh, on 13 September 2012 - 07:01 PM, said:

The easiest way that I can figure to do it would be to use the ANDW function to mask out bits 7-15, leaving the absolute value of the short signed integer. Then look at the sign bit, if it is on, multiply the masked value by -1, if it is off, just move the value as shown below:

Short int.jpg

I tried that approach but that doesn't return the correct value...this is what you must get:
for 1111 1111 two's complement value= -1
but using the mask and multiply method you get -127
Unless I am doing something wrong...

0

### #4   Nicolas  Posted 14 September 2012 - 10:24 AM

• Sparky
• Group: MrPLC Member
• Posts: 14
• Joined: 10-August 09
• Location:Houston TX
• Country:United States
Here's the final answer attached for anyone interested, easy to put into a little function block:

Result:

0

### #5   Michael Walsh  Posted 14 September 2012 - 02:43 PM

• Omron Expert
• Posts: 440
• Joined: 03-April 06
• Gender:Male
• Location:Charlotte, NC
• Country:United States
Ahh, yes, you are correct. I did not spend enough time on this and made a mistake. At least I got you headed in the right direction!
Michael
Charlotte, NC
0

### #6   Sergei Troizky  Posted 14 September 2012 - 06:02 PM

• Expert
• Group: MrPLC Member
• Posts: 605
• Joined: 04-March 03
• Gender:Male
• Location:Montreal

#### Attached thumbnail(s)

This post has been edited by Sergei Troizky: 14 September 2012 - 06:06 PM

0

### #7   Michael Walsh  Posted 14 September 2012 - 07:50 PM

• Omron Expert
• Posts: 440
• Joined: 03-April 06
• Gender:Male
• Location:Charlotte, NC
• Country:United States
Ooooh! Ultra elegant! Nice!

Michael
Charlotte, NC
0

### #8   BITS N BYTES  Posted 17 September 2012 - 09:15 AM

• Expert
• Group: MrPLC Member
• Posts: 539
• Joined: 22-December 04
• Gender:Male
• Location:Miami Beach FL
• Country:United States
Structured Text conversion of Single Signed Integer.

0