signed and unsigned

guest

Guest
G
Gentlemen,
what exactly is meant/difference between a signed and unsigned integer? I constantly see reference to them but no real explanation.
I thank you in advance for your response
 
Usually integers use 16 bits. A signed integer uses the highest bit for +/- limiting the range of the integer to what the remaining 15 bits can represent.
 
With an unsigned integer all of the bits that compose the value have positive values. Therefore you cannot arrive at a negative number.
In a 16-bit storage location in a PLC for example this would most commonly mean you could store any number from 0 - 65535.

More typically though the last bit is used to arrive at a negative number. This is usually accomplished using what is called two's-complement binary. Another less common method is signed magnitude binary.

Basically either of those make use of the last bit to indicate a positive or a negative number. So with only 15 bits instead of sixteen, you cannot count as high, only up to 32767. But, you can also count negative down to -32768 (2's complement) or -32767 (signed magnitude). Every PLC I have used, has used 2's complement.

Lots of good web sites will explain those two methods if you really wanna go that deep into it.

OG
 
Thank you all very much!!! That question has been nagging me for some time, and as usual once explained the answer is quite simple! Thanks again guys.....Once again I leave this sight with a little more knowledge than I had when I first came here!
 
must type faster ... must type faster ...

ok ... I got beat again ... but I'm going to go ahead and post this anyway ...

first let’s look at “signed” integers ...

let’s use Allen-Bradley’s 16-bit integers as a specific example ... suppose that we begin with all 16 of the bits equal to “0” ... then the decimal value of the word is “0” ...

0000000000000000 binary equals 0 decimal

now turn on the very first (lowest value – the one on the far right – the zero bit - however you want to say it) ... the decimal value of that bit is “1” ... so now the decimal value of the word is “1” ...

0000000000000001 binary equals 1 decimal

now leave the first bit on and turn on the next bit too ... the decimal value of that bit is “2” ... so now the decimal value of the word is “3” ...

0000000000000011 binary equals 3 decimal

now leave those two bits on and turn on the next bit too ... the decimal value of that bit is “4” ... notice that the decimal value of each bit is double the decimal value of the previous bit ... so now the decimal value of the word is “7” ... yes, there IS a pattern here ... each time we turn on a bit, the value of that bit gets ADDED to the value of all of the other ON bits ...

0000000000000111 binary equals 7 decimal

now suppose that we keep turning on each bit right-up-to but NOT including the last bit ... (it’s special) ... now the decimal value of the word is 32767 ...

0111111111111111 binary equals 32767 decimal

so what about that last bit? ... well first let’s talk about the fact that when you get right down to the basics, the PLC has NOTHING else in its memory except bits ... and each bit can be thought of as a box which can hold either a “1” or a “0” ... NOTHING ELSE ... well that presents a special problem if we ever need to represent a negative number ... think about it this way: if ALL we can put into a bit is either a “1” or a “0”, then how in the heck could we ever enter a negative sign? ... so the PLC designers came up with a clever idea ... let’s use that last bit to represent a negative sign ... now if you were to take the time to keep doubling values the way we were doing earlier, you’d find that the actual value of that last bit is 32768 ... that’s POSITIVE 32768 ... BUT ... since we’ve decided to use this bit as a special “negative sign” bit, then we’ll assign it the value of NEGATIVE 32768 ... that’s right ... we just make up a rule and make it happen ... now ... finally ... turn on that last bit ...

as before, we ADD the value of this ON bit (-32768) to the value of all of the other ON bits ... 32767 ... do the math and you come up with NEGATIVE 1 ...

1111111111111111 binary equals –1 decimal

now just for kicks, turn off all of the bits EXCEPT for the “sign bit” ... since it’s still ON, it ADDS a NEGATIVE 32768 to the value of the integer ... and since all of the other (positive) bits are OFF, then the value of the integer is simply NEGATIVE 32768 ...

so that’s the secret handshake for “signed” integers ... a fancy word for this common numbering system is “two’s complement binary” ...

now for “unsigned” integers ...

again we’ll use all of the preceding rules and start by turning on all of the bits except for the last one ...

again 0111111111111111 binary equals 32767 decimal ...

so nothing has changed so far ...

but this time we won’t use the “special” rule which says that the last bit is to be used as a “negative sign” ... this time we’ll just go ahead and treat this bit like all of the other positive bits ... so its value is now POSITIVE 32768 ... and when we turn that puppy on and ADD its value to all of the other ON bits, we get the total decimal value 65535 ...

1111111111111111 binary equals 65535 decimal

but remember ... this is only true for an “unsigned” integer ... and incidentally, Allen-Bradley always treats their PLC and SLC integers as “signed” integers ... so that’s where their “normal” range of integer values (32767 to –32768) comes from ... (note: the ControlLogix platform has more options) ...

I hope this helps ... if you want to play around with these ideas, the best way is to use the RSLogix software and open two windows for an integer file ... set the radix (numbering system) for one of the windows to “binary” ... and set the radix for the other window to “decimal” ... then start changing the bit patterns in one window and watch what happens to the equivalent value in the other ... just remember that with Allen-Bradley you’re not going to be able to see the “unsigned” integer effect ...

one more thing ... the next time you have a SPARE system to play around with, go to the Output Image Table and change its radix to decimal ... find the address of an unused output module ... type in –1 and see what happens to the outputs ...
 
i hate to follow up rons great post,,but for more information on 2's compliment,,you can find other awesome information on it with a google search,,and whats even better is when you get into bitwise logic,,what would be nice (and ill work on it ) is to have a posted sticky on bitwise logic and normally used functions,,

lets take XOR(a good one for encryptions),,
Code:
101
001 XOR
100
as opposed to OR
Code:
101 
100 OR
101



hope that helps,,and im not too drunk to post bad information,,

Fred Raud
 
Fred Raud said:
what would be nice (and ill work on it ) is to have a posted sticky on bitwise logic

Phil's got one HERE that's hasn't come unstuck yet. Feel free to expand on it though. It can always find a home at Ron's site (the other Ron).

beerchug

-Eric
 
thanx for the links mr nelson,,,since its late i didnt do my research and find those links,,and look forward to looking into Rons site,,,this is my first look there,,

and hey,,,dont you have a bedtime Nelson??i thought i was the only one up at this hour,,would be nice to have an mirc channel to chat real time with all the other late night crazies like myself,,


Fred Raud
 
Bedtime? What's that?...

Fred Raud said:
and hey,,,dont you have a bedtime Nelson??

Yup... When the sun comes up. I work the overnight shift, so these are my regular hours, if you can call 5pm to 5am 'regular'... ;)

Yes, please do have a look around Ron's site, Fred. I think you'd find it interesting... :nodi:

beerchug

-Eric
 
USINT data type allen bradley

Hello,

It is possible to change form INT to USINT a variable data type?
For the moment, I receive the value -32766 (Local:6:I.Ch0Data) from analogue device (1769-IF4XOF2F/A), and I need to be positive. I changed the wiring, but no succes. The settings for input configuration is 0V to 10 V (60 Hz, Raw/Proportional).
Someone have any idea? I cannot find to change data type to USINT on allen bradley...
Thanks

Images: https://imgur.com/a/2bZKf
 
Last edited:

Similar Topics

I'm having to make an AOI to use with a generic ethernet device because the manufacturer does not provide an AOP. I just want to do a sanity check...
Replies
13
Views
1,143
Is there a way in CX-Programmer to ad UINTs together with an instruction without a compiler warning. Ladder Rung: Warning: Instructions...
Replies
2
Views
1,212
I'm trying to read a u32 register from a fuel meter over modbus rtu. There isn't a unsigned integer option under the "treat as" field. I select...
Replies
15
Views
3,808
Hello, I'm reading a modbus register 40125 I'm using a modbus poll program to read this register. If i read the register as a "signed" value it...
Replies
12
Views
8,976
PLC Programming Noob here... Converting a Micro/WIN program to RSLogix 5000. Anyway to do an Unsigned DINT in RSLogix 5000? Have a value of...
Replies
4
Views
4,153
Back
Top Bottom