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