Ch 2. Internal Data Representation of Characters, Integers, Real Numbers
2.2 Non-negative and Negative Integers, and Arithmetic Operations on Integers
In digital systems, positive and negative integers must be represented using binary digits. Since computers operate using binary logic, special encoding schemes are used to represent signed integers, that is, integers that can be either positive or negative. This section explores how integers are represented and manipulated in binary form.
The simplest type of integer to represent is an unsigned integer, which uses bits to represent values starting from 0 up to a maximum determined by the number of bits. We saw that ASCII uses 7 bits to represent alphanumeric characters, which corresponds to the range 0000000 to 1111111 in binary, or 0 to 127 in decimal. To determine the maximum value of an unsigned integer with n bits, the following formula is used: [latex]2^{n} - 1[/latex]
Example 2.4
Let’s add 7 + 8 using 4-bit unsigned integers. Since 7 = 0111 and 8 = 1000
0111
+ 1000
-------
1111
The result is 15, which is correct.
If you want to represent both positive and negative integers, there are three encoding schemes that you should know. The first is the sign-magnitude representation, where the most significant bit (MSB) indicates the sign: 0 for positive and 1 for negative. For example, in 4 bits, the binary number 0101 is +5, and 1101 is -5.
While this method is simple, there is an issue with representing zero: it can be 0000 or 1000, which complicates arithmetic operations.
Example 2.5
Let’s add +3 and -2 using 4-bit sign-magnitude. While +3 = 0011 and -2 = 1010, binary addition directly on sign-magnitude values is not straightforward. You must check signs, compare magnitudes, subtract if signs differ, and assign the correct sign to the result.
The second encoding scheme is one’s complement, in which negative numbers are represented by inverting all bits (0s become 1s and 1s become 0s) of the corresponding positive number. For example, in 4 bits, the binary number 0101 is +5, and 1010 is -5.
Like sign-magnitude, this also has two representations for zero (0000 and 1111).
Example 2.6
Let’s add +4 and -3 using 4-bit one’s complement. We have +4 = 0100 and -3 = 1100, which is 0011 inverted
0100
+ 1100
-------
10000
This is a 5-bit result, including the carry-out, so we drop the carry-out bit and add it back to the result. This is done by taking 0000 and adding 1 to get 0001, which is +1, as expected.
The third encoding scheme is two’s complement, in which negative numbers are represented by inverting the positive number and adding 1. This is the most widely used method in modern computing. For example, in 4 bits, the binary number 0101 is +5, and 1011 is -5.
Two’s complement has only one representation for zero (0000) and simplifies arithmetic operations.
For binary arithmetic, addition follows the same principles as Section 1.3 and subtraction is performed using two’s complement:
[latex]x - y = x + (\text{two's complement of } y)[/latex]
Example 2.7
Let’s compute 3 - 2 using 4-bit two’s complement. We have +3 = 0011 and +2 = 0010. The two’s complement of 2 is obtained by inverting 0010 to 1101 and then adding 1 to obtain 1110. The subtraction is then performed by adding +3 and -2.
0011 (+3)
+ 1110 (-2)
-------
10001
This is a 5-bit result, so we drop the carry-out bit to obtain the correct result of 0001 = +1.
Real-World Example 2.2: Balance and Transaction System
You’re designing the backend logic for an ATM. The system must handle account balances, withdrawals, and deposits using binary arithmetic.
Balances are stored as signed binary integers. For example, a balance of +500 might be stored in two’s complement format using 16 bits.
A withdrawal of $200 is treated as adding -$200 to the balance. The system uses two’s complement subtraction to compute the new balance:
New Balance = Old Balance + (−Withdrawal Amount)
If the result is negative, the system checks whether the account allows overdraft. If not, the transaction is denied.
Let’s say the balance is $300 and the user tries to withdraw $400. In 16-bit binary, +300 is 0000000100101100 and +400 is 0000000110010000. The two’s complement of 400 is computed by inverting the binary value to 1111111001101111 and then adding 1 to obtain 1111111001110000. Adding the binary values for +300 and -400 gives
0000000100101100 (+300)
+ 1111111001110000 (-400)
-------------------
1111111110011100 (-100)
The result is -$100, so the system knows the account is overdrawn.