Two's complement notation
Sitter och bläddrar Computer Science: An overview, och jag är osäker på om jag helt förstår vad "Two's complement notation" är för något.
Texten anger följande:
The most popular system for representing integers within today’s computers is
two’s complement notation. This system uses a fixed number of bits to represent each of the values in the system. In today’s equipment, it is common to use
a two’s complement system in which each value is represented by a pattern of
32 bits. Such a large system allows a wide range of numbers to be represented
but is awkward for demonstration purposes. Thus, to study the properties of two’s
complement systems, we will concentrate on smaller systems.
Figure 1.19 shows two complete two’s complement systems—one based on
bit patterns of length three, the other based on bit patterns of length four. Such
a system is constructed by starting with a string of 0s of the appropriate length
and then counting in binary until the pattern consisting of a single 0 followed by
1s is reached. These patterns represent the values 0, 1, 2, 3, . . . . The patterns
representing negative values are obtained by starting with a string of 1s of the
appropriate length and then counting backward in binary until the pattern consisting of a single 1 followed by 0s is reached. These patterns represent the values
-1, -2, -3, . . . . (If counting backward in binary is difficult for you, merely start
at the very bottom of the table with the pattern consisting of a single 1 followed
by 0s, and count up to the pattern consisting of all 1s.)
Note that in a two’s complement system, the leftmost bit of a bit pattern indicates the sign of the value represented. Thus, the leftmost bit is often called the
sign bit. In a two’s complement system, negative values are represented by the
patterns whose sign bits are 1; nonnegative values are represented by patterns
whose sign bits are 0.
In a two’s complement system, there is a convenient relationship between the
patterns representing positive and negative values of the same magnitude. They
are identical when read from right to left, up to and including the first 1. From
there on, the patterns are complements of one another. (The complement of a
pattern is the pattern obtained by changing all the 0s to 1s and all the 1s to 0s;
0110 and 1001 are complements.) For example, in the 4-bit system in Figure 1.19 the patterns representing 2 and -2 both end with 10, but the pattern representing 2 begins with 00, whereas the pattern representing -2 begins with 11.
This observation leads to an algorithm for converting back and forth between bit
patterns representing positive and negative values of the same magnitude. We
merely copy the original pattern from right to left until a 1 has been copied, then
we complement the remaining bits as they are transferred to the final bit pattern
(Figure 1.20).
Understanding these basic properties of two’s complement systems also leads
to an algorithm for decoding two’s complement representations. If the pattern to be decoded has a sign bit of 0, we need merely read the value as though the
pattern were a binary representation. For example, 0110 represents the value 6,
because 110 is binary for 6. If the pattern to be decoded has a sign bit of 1, we
know the value represented is negative, and all that remains is to find the magnitude of the value. We do this by applying the “copy and complement” procedure in Figure 1.20 and then decoding the pattern obtained as though it were a
straightforward binary representation. For example, to decode the pattern 1010,
we first recognize that since the sign bit is 1, the value represented is negative.
Hence, we apply the “copy and complement” procedure to obtain the pattern
0110, recognize that this is the binary representation for 6, and conclude that the
original pattern represents -6.
Tabellerna är omständiga att formatera in här, men de är långt förståeligare än texten för mig.
Första biten i en integersträng anger att heltalet är negativt om biten = 1, och följande bitar används helt vanligt och räknar uppåt från det lägsta negativa heltalet som ryms inom antalet bitar.
Är det svårare än så, och texten är bara väldigt omständig på ämnet, eller är det något viktigt inbakat här som jag missar?
Är det här något allmängiltigt för integertal, eller är detta en särskilt integer som tillåter negativa tal?