Книга: Advanced PIC Microcontroller Projects in C
1.16 Negative Numbers
Разделы на этой странице:
1.16 Negative Numbers
The most significant bit of a binary number is usually used as the sign bit. By convention, for positive numbers this bit is 0, and for negative numbers this bit is 1. Figure 1.5 shows the 4-bit positive and negative numbers. The largest positive and negative numbers are +7 and –8 respectively.
Binary number | Decimal equivalent |
---|---|
0111 | +7 |
0110 | +6 |
0101 | +5 |
0100 | +4 |
0011 | +3 |
0010 | +2 |
0001 | +1 |
0000 | 0 |
1111 | ?1 |
1110 | ?2 |
1101 | ?3 |
1100 | ?4 |
1011 | ?5 |
1010 | ?6 |
1001 | ?7 |
1000 | ?8 |
Figure 1.5: 4-bit positive and negative numbers
To convert a positive number to negative, take the complement of the number and add 1. This process is also called the 2’s complement of the number.
Example 1.22
Write decimal number ?6 as a 4-bit number.
Solution 1.22
First, write the number as a positive number, then find the complement and add 1:
0110
+6
1001
complement
1
add 1
????
which is ?6
1010
Example 1.23
Write decimal number ?25 as a 8-bit number.
Solution 1.23
First, write the number as a positive number, then find the complement and add 1:
00011001
+25
11100110
complement
1
add 1
–––––––-
which is ?25
11100111
- 1.1 Introduction
- 1.2 Microcontroller Systems
- 1.3 Microcontroller Features
- 1.4 Microcontroller Architectures
- 1.5 Number Systems
- 1.6 Converting Binary Numbers into Decimal
- 1.7 Converting Decimal Numbers into Binary
- 1.8 Converting Binary Numbers into Hexadecimal
- 1.9 Converting Hexadecimal Numbers into Binary
- 1.10 Converting Hexadecimal Numbers into Decimal
- 1.11 Converting Decimal Numbers into Hexadecimal
- 1.12 Converting Octal Numbers into Decimal
- 1.13 Converting Decimal Numbers into Octal
- 1.14 Converting Octal Numbers into Binary
- 1.15 Converting Binary Numbers into Octal
- 1.16 Negative Numbers
- 1.17 Adding Binary Numbers
- 1.18 Subtracting Binary Numbers
- 1.19 Multiplication of Binary Numbers
- 1.20 Division of Binary Numbers
- 1.21 Floating Point Numbers
- 1.22 Converting a Floating Point Number into Decimal
- 1.23 BCD Numbers
- 1.24 Summary
- 1.25 Exercises
- 1.6 Converting Binary Numbers into Decimal
- 1.7 Converting Decimal Numbers into Binary
- 1.8 Converting Binary Numbers into Hexadecimal
- 1.9 Converting Hexadecimal Numbers into Binary
- 1.10 Converting Hexadecimal Numbers into Decimal
- 1.11 Converting Decimal Numbers into Hexadecimal
- 1.12 Converting Octal Numbers into Decimal
- 1.13 Converting Decimal Numbers into Octal
- 1.14 Converting Octal Numbers into Binary
- 1.15 Converting Binary Numbers into Octal
- 1.17 Adding Binary Numbers
- 1.18 Subtracting Binary Numbers