Use the eight rightmost bits for the byte. The value is indeed 0. Two’s Complement delivers the cor- rect result. Example 2 Show the binary representations of 32 and –32. 32 0 0 1 0 0 0 0 0 First, write the positive number. 0010 0000 Second, flip the bits. 1101 1111 Third, add 1 to the rightmost bit. 1110 0000 Remember, 1+1 = 10, so write the 0 and carry the 1, as shown. 1 1 1 1 1 Carry row 1 1 0 1 1 1 1 1 Bits-flipped row + 1 Add 1 row 1 1 1 0 0 0 0 0 Two's Complement –32 1 1 1 0 0 0 0 0 Math and Java (Continued) Assignment 1. Show the content of the following variables in binary format. A. byte bakersDozen = 13 B. byte temperature = –11 C. byte centuryAgo = –100 D. byte minNegative = –128 2. The above examples used the byte data type to conserve space. The Two’s Complement process is similar for the int, short, and long data types. Show the content of the following variables in binary format. A. int halfDozen = 6 B. short temperature = –10 C. long centuryAgo = –100 3. Explain why the largest value for the byte data type is 127 while the lowest is –128. 4. Speculate why the Two’s Complement scheme always puts a 1 in the leftmost bit of a negative number. 5. Can you think of another way to represent negative numbers in a memory location? Explain how the sum of this number and its opposite will be 0. Chapter 4 Variables 79 Copyright Goodheart-Willcox Co., Inc.
Previous Page Next Page