Algorithms and Data Structures in C part 2 doc - Pdf 16


which can be written as
where is defined as the unary complement:
The one’s complement of a number, A, denoted by , is defined asFrom Eq. 1.18 it can be
shown that
To see this note that
and
This yields
Inserting Eq. 1.24 into Eq. 1.22 yields

which gives

By noting

one obtains

which is -A. So whether A is positive or negative the two’s complement of A is equivalent to -A.

Note that in this case it is a simpler way to generate the representation of -1. Otherwise you
would have to note that

Similarly
However, it is useful to know the representation in terms of the weighted bits. For instance, -5,
can be generated from the representation of -1 by eliminating the contribution of 4 in -1:

Similarly, -21, can be realized from -5 by eliminating the positive contribution of 16 from its
representation.

The operations can be done in hex as well as binary. For 8-bit 2’s complement one has
with all the operations performed in hex. After a little familiarity, hex numbers are generally
easier to manipulate. To take the one’s complement one handles each hex digit at a time. If w is a

Complemen
t
‐128 NR

 NR 10000000
‐127 NR 11111111 10000001
‐2 NR 10000010 11111110
‐1 NR 10000001 11111111
0 00000000 00000000
10000000
00000000
1 00000001 00000001 00000001
127 01111111 01111111 01111111
128 10000000 NR NR
255 11111111 NRNR


.Notrepresentablein8‐bitformat.

Table1.4
Rangesfor
2’s
Complement
and
Unsigned
Notations#
Bits

2’sComplement Unsigned
8 ‐128≤A≤127 0≤A≤255

32-bit formats to 64-bit formats.
Given A as

and B as

the objective is to determine b
k
such that B = A.
1.1.4.1SignedMagnitude
For signed-magnitude the b
k
are assigned with

1.1.4.2Unsigned
The conversion for unsigned results in

1.1.4.32’sComplement
For 2’s complement there are two cases depending on the sign of the number:
(a) (a
n - 1
= 0) For this case, A reduces to

It is trivial to see that the assignment of b
k
with


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status