June 2017
Beginner
1296 pages
69h 23m
English
The discussion so far in this appendix has focused on positive numbers. In this section, we explain how computers represent negative numbers using two’s complement notation. First we explain how the two’s complement of a binary number is formed, then we show why it represents the negative value of the given binary number.
Consider a machine with 32-bit integers. Suppose
int value = 13;
The 32-bit representation of value is
00000000 00000000 00000000 00001101
To form the negative of value we first form its one’s complement by applying Java’s bitwise complement operator (~):
onesComplementOfValue = ~value;
Internally, ~value is now value with each of its bits reversed—ones become zeros and ...