Книга: 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

????
1010
which is ?6

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

–––––––-
11100111
 which is ?25

Оглавление книги


Генерация: 3.730. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз