Scientific Solutions ® Inc.
On-Line Reference:
Binary, Sign-Magnitude, Two's Complement Explained
Binary, Sign-Magnitude and Two's Complement are three different methods of representing numbers. As we will show in this discussion, Binary is only good for positive (Unsigned) numbers.
We need a representation for positive and negative (Signed) numbers.
Two common methods are
- Sign-Magnitude
- Two's Complement
At the end of this discussion, we include a section about using Binary and Two's Complement representation in your data acquisition system.
Binary Representation
Binary representation uses all '0' for the smallest number, and all '1' for the largest numbers:Binary |
Hex |
Decimal |
1111 |
F |
15 |
1110 |
E |
14 |
1101 |
D |
13 |
1100 |
C |
12 |
1011 |
B |
11 |
1010 |
A |
10 |
1001 |
9 |
9 |
1000 |
8 |
8 |
0111 |
7 |
7 |
0110 |
6 |
6 |
0101 |
5 |
5 |
0100 |
4 |
4 |
0011 |
3 |
3 |
0010 |
2 |
2 |
0001 |
1 |
1 |
0000 |
0 |
0 |
Number of bits: n
Minimum Number: 0
Maximum Number: 2 n -1
Advantages:
(1). Easy to Read
(2). Good representation for positive (Unsigned) numbers
Disadvantages
(1). Cannot represent negative (Signed) numbers
Sign-Magnitude Representation:
Sign-Magnitude uses the most-significant-bit as the sign bit. The
rest of the bits represent the number in the "Binary" format:Binary |
Hex |
Decimal |
0111 |
7 |
+7 |
0110 |
6 |
+6 |
0101 |
5 |
+5 |
0100 |
4 |
+4 |
0011 |
3 |
+3 |
0010 |
2 |
+2 |
0001 |
1 |
+1 |
0000 |
0 |
+0 |
1000 |
8 |
-0 |
1001 |
9 |
-1 |
1010 |
A |
-2 |
1011 |
B |
-3 |
1100 |
C |
-4 |
1101 |
D |
-5 |
1110 |
E |
-6 |
1111 |
F |
-7 |
Number of bits: n
Minimum Number: - (2 n-1 -1 )
Maximum Number + (2 n-1 -1)
Advantages:
(1). Easy to Read
Disadvantages
(1). Two Representatives for Zero
Using our 4-bit Sign-Magnitude example we have,
+0 = 0000
-0 = 1000
(2). Arithmetic is difficult
Example, lets add Positive 2 to Negative 4, which we expect the answer to be Negative 2.
+2 |
0010 |
-4 |
1100 |
---- |
------ |
-6 |
1110 |
The result is -6, which is not what we expect
Another Example lets add +2 to -2, we expect the answer to be zero
+2 |
0010 |
-2 |
1010 |
---- |
----- |
1100 |
Two's Complement Representation:
Two's complement is the method computers use to represent Signed numbers.The most-significant-bit is the sign bit.
- 0 = positive value
- 1 = negative value
- Start with the positive number
- Flip the bits (Change '0' to '1' and '1' to '0')
- Add the value 1
Binary |
Hex |
Decimal |
0111 |
7 |
+7 |
0110 |
6 |
+6 |
0101 |
5 |
+5 |
0100 |
4 |
+4 |
0011 |
3 |
+3 |
0010 |
2 |
+2 |
0001 |
1 |
+1 |
0000 |
0 |
+0 |
1111 |
F |
-1 |
1110 |
E |
-2 |
1101 |
D |
-3 |
1100 |
C |
-4 |
1011 |
B |
-5 |
1010 |
A |
-6 |
1001 |
9 |
-7 |
1000 |
8 |
-8 |
Number of bits: n
Minimum Number: - (2 n-1)
Maximum Number + (2 n-1 -1)
Advantages:
(1). One value for Zero
(2). Conversion from Positve to Negative and Negative to Positive numbers is easy and consistent
Example:
+ 2 = 0010
Flip the bits = 1101 and add 1 makes 1110 which is -2
-2 = 1110
Flip the bits = 0001 and add 1 makes 0010 which is +2
(3). Arithmetic is easy
Example, lets add Positive 2 to Negative 4, which we expect the answer to be Negative 2.
+2 |
0010 |
-4 |
1100 |
---- |
------ |
-2 |
1110 |
The result is -2 as expected!
Another Example lets add +2 to -2, we expect the answer to be zero
+2 |
0010 |
-2 |
1110 |
---- |
----- |
0000 |
The carry of 1 from the sign bit is ignored.
Disadvantages
(1). Difficult to read
Using Binary and Two's Complement format in your data acquisition system:
Some data acquisition products provide the Analog-to-Digital (ADC) Data in a set format (either Binary or Two's Complement), while others let the user configure the hardware to provide one format or the other. The question is when is it best to use one format over the other.If fact, it really is a question of how you convert the raw ADC Data to a voltage, i.e. what math equation do you want to use.
The following table demonstrates the Binary vs. Two's Complement data format for Unipolar and Bipolar configured Analog Inputs:
Bipolar Analog Input |
Offset Binary |
Two's
Complement |
+10 |
FFFF |
7FFF |
+7.5 |
E000 |
6000 |
+5.0 |
C000 |
4000 |
+2.5 |
A000 |
2000 |
+LSB |
8001 |
0001 |
0 |
8000 |
0000 |
-LSB |
7FFF |
FFFF |
-2.5 |
6000 |
A000 |
-5.0 |
4000 |
C000 |
-7.5 |
2000 |
E000 |
-10.0 |
0000 |
8000 |
The term "Offset Binary" is used because "Binary" refers to using 0000 as the Zero number whereas in most data acquisition systems when they are configured for "Binary" mode they provide 0000 for the "minimum" number, which is called "Offset Binary"
Unipolar Analog Input |
Offset Binary |
Two's
Complement |
+10 |
FFFF |
7FFF |
+7.5 |
C000 |
4000 |
+5.0 |
8000 |
0000 |
+2.5 |
4000 |
C000 |
+LSB |
0001 |
8001 |
0 |
0000 |
8000 |
The term "Binary" is used because "Binary" normally refers to using 0000 as the Zero number which is the case when the data acquisition system is configured for Unipolar Analog Inputs.
It is suggested that you view the discussion on Math Equations for Converting ADC Data to Voltage. This discussion may help you decide what ADC Data format you want to use.