Programming and Graphics

(Kiana) #1

1.2 The binary system 5


where the ones are repeatedptimes. The decimal-number equivalent is


1 × 2 p−^1 +1× 2 p−^2 +1× 2 p−^3 +···
+1× 22 +1× 21 +1× 20 =2p− 1. (7)

To demonstrate this equivalence, we recall from our high school years the iden-
tity


ap−bp=(a−b)(ap−^1 +ap−^2 b+...abp−^2 +bp−^1 ), (8)

whereaandbare two variables, and seta=2andb=1.


When one bit is available, we can describe only the integers 0 and 1, and
the largest integer is 1. With two bits the maximum is 3, with three bits the
maximum is 7, with eight bits the maximum is 255, and with thirty-one bits
the maximum is 214,748,3647. We see that a very rich person requires a lot of
bits to record her fortune. How many bits do you need to record your savings
in US dollars?


Signed integers


To encode a signed integer, we allocate the first bit to the sign. If the
leading bit is 0, the integer is positive; if the leading bit is 1, the integer is
negative. The largest signed integer that can be represented withpbits is then
2 p−^1 −1. According to this convention, the integer−5=−(101) 2 is stored as
the binary string 1101. To derive this binary string, we may use the method of
two’s complement: we flip the bits of 5 to obtain (010) 2 , and then add (1) 2 to
obtain the desired code 1101.


Bytes and nibbles


A set of eight bits is onebyte. The largest integer that can be represented
with one byte is


(11111111) 2 (9)

whose decimal-number equivalent is


1 × 27 +1× 26 +1× 25 +1× 24 +1× 23 +1× 22 +1× 21 +1× 20
=128+64+32+16+8+4+2+
=2^8 −1 = 255. (10)

Large units of bytes are shown in Table 1.2.1. As a point of reference,
the deficit of the United States in 2005 was approximately $400 Billion, that is,
$400,000,000,000, which is on the order of one third of a Gbyte.

Free download pdf