Signed Bits

Join Date
Feb 2007
Location
Oklahoma
Posts
275
Hi to all,
Where is a good web page to learn about signed bits in PLCs? Posotive and negative numbers for under flow bits and over flow bits. Thank you all for your help.
 
Hi to all,
Where is a good web page to learn about signed bits in PLCs? Posotive and negative numbers for under flow bits and over flow bits. Thank you all for your help.

I don't know about a good website, but I can give you a good start in understanding signed numbers in PLCs.

First off, looking at the 16-bit or 32-bit data mamory that PLCs now use can be confusing, so lets start with a smaller memory size - take a 4-bit register as an example, and start off with looking at UNSIGNED binary...

0 (decimal) is represented by the bit pattern 0000
add 1
1 (decimal) is represented by the bit pattern 0001
2 (decimal) is represented by the bit pattern 0010
3 (decimal) is represented by the bit pattern 0011
4 (decimal) is represented by the bit pattern 0100
...
7 (decimal) is represented by the bit pattern 0111
...
15 (decimal) is represented by the bit pattern 1111

i.e. each "bit" in the binary representation is worth (reading right to left), 1, 2, 4, 8 : this is called the binary "weighting", and we can add up the binary weightings to get back to the decimal number - eg. binary 1010 is worth (reading right to left), 2 + 8 = 10 (decimal).

In unsigned binary, the full permutations of 1s and 0s gives us a numerical (decimal) range of 0 to 15.

Now lets look at signed binary numbers.

To confuse things there are 2 possible formats used for signed binary numbers, "Signed Magnitude", and "2's complement".

By far the most widely used is the "2's complement" notation, but I will explain "Signed Magnitude" later.

In the "2's complement" notation, the highest order bit is used as a sign bit, so in a 4-bit number we only have 3 bits available to use for the number, the fourth bit is used to indicate the sign, positive (the sign bit is off), or negative (the sign bit is on).

So our 4-bit register , starting at zero again...

0 (decimal) is represented by the bit pattern 0000
add 1
+1 (decimal) is represented by the bit pattern 0001
+2 (decimal) is represented by the bit pattern 0010
+3 (decimal) is represented by the bit pattern 0011
+4 (decimal) is represented by the bit pattern 0100
...
+7 (decimal) is represented by the bit pattern 0111

Now when we add 1 to 0111 binary, we get 1000 binary, but that leftmost bit (in a 4-bit signed integer) is a sign bit, so what is the decimal equivalent of 1000 binary? To find out the magnitude of our negative number, using this 2's complement notation, we "complement and add 1" - that's the rule...

So 1000 complemented is 0111, which we know from above is 7, then we add 1 = 8. So our 2's complement binary 1000 is evaluated as -8.

Now before you go saying this is absurd, or whatever other words you want to use, let me say that this is the most natural way for the processor to operate, doing binary maths is a doddle for electronic circuits.

It is us humans who want our numbers presented to us in a friendly fashion. Thankfully, and usually, the programming software we use does the conversion from the native binary representation into the decimal numbers we like to see.

The numeric ranges for binary numbers is as follows

2-bit : unsigned 0 to 3 : signed -2 to +1
4-bit : unsigned 0 to 15 : signed -8 to +7
8-bit : unsigned 0 to 255 : signed -128 to +127
16-bit : unsigned 0 to 65,535 : signed -32,768 to +32,767
32-bit : unsigned 0 to 4,294,967,295 : signed -2,147,483,648 to +2,147,483,647

Take your time to analyse how the simpler 4-bit number works, how it's only a 3-bit number and a sign bit, and how the addition rollover from the maximum positive number drops us to the maximum negative number, and you will have cracked one of the more difficult concepts in the PLC world.

Take a breather, then when you think you understand it, you can extend the idea to the larger binary numbers, hopefully it will all click into place.


I mentioned the other binary representation for negative numbers "Signed Magnitude" - and I said it was rarely used (the minority).

In this notation the sign bit is simply used to negate the number in the other bits....

+5 = 0101
-5 = 1101

In this system, we can only represent -7 to +7 (as opposed to -8 to +7 in the 2's complement notation) with a 4-bit signed number. I can't think of any PLC that uses this notation today, but you may come across it at some point.

Apologies to all for the long post - lot's of info for you to digest - tried and failed to keep it short - come back if there's anything you don't understand.

Oh, and you asked about overflow and underflow - these conditions arise when you can't store a number in a destination location that's too big (overflow), or too small (underflow). The overflow and underflow bits are not stored in the number, they are always processor status flags that you can inspect after doing a math operation.

e.g add 1 to +7 in a 4-bit signed memory location = -8 :: The Overflow bit will be set after doing this math, because the instruction has recognised that is has carried a "1" into the sign bit - the opposite occurs when subtracting
 
Excellent explenation. For me the concept of the representation of negative numbers is by starting from zero and going back one (ignoring the fact that we had to perform a 'borrow' from somwhere) we get all 1's. This is the natural representation of minus one. The continued backward progression is just by continued subtraction.

Another thing that stuck in my mind was the fact that the positive range was one less than the negative range. But of course the representation of zero (all zeroes) with the high bit not set takes up one of the positive representations. I guess it had to be somewhere.
 
Bernie, the second link I posted has a good table that explains the different representations....

I had no idea that DEC PDP11's used 1's complement !! and i'd never heard of the other one..

I think it fair to say that 2's complement is the most widely used today, probably because it is the most efficient, because every bit pattern equates to a number, and there is no ambiguity about +0 or -0
 
Zero has no concept of being positive or negative in the real world, it is the origin of our numbering systems.

To a binary logic circuit however, it has to be one or the other, (being part of a "binary" family), and therefore has been "classified" as being not negative, ergo, it must be positive !!
 
Thank you

Thank you very much deba for your excellent help and taking the time to explain this in such great detail. Thank you bernie for also helping me out.
 

Similar Topics

Hi guys, I have a problem with setting up the parameters for PowerFlex 525. So we have 4x motor connected in parallel to 1 PowerFlex, each motor...
Replies
8
Views
1,022
I set up a Mitisubishi test system on the bench. Q03UDSPU Q173DCPU MR-J3-40B HF-KP43 HMI is a LS XP50 TTA/KP43 I am trying to use a signed...
Replies
1
Views
389
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,150
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,223
Replies
3
Views
1,311
Back
Top Bottom