Introduction
Integer data types are used for storing whole numbers and characters. The integers are internally stored in binary form.
Program/Example
Here is an example that shows how integers are stored in the binary form.
Number =13
Decimal representation = 1*101 + 3*100
Binary representation = 1101 = 1*23 + 1*22 + 0*21 + 1*1
Each 1 or 0 is called a bit, thus the number 13 requires 4 bits.
In the same way, the number 130 is 1000 0010 in binary.
If the general data type is char, 8 bits are allocated. Using 8 bits, you can normally represent decimal numbers from 0 to 255 (0000 0000 to 1111 1111). This is the case when the data type is unsigned char. However, with signed char, the leftmost bit is used to represent the sign of the number. If the sign bit is 0, the number is positive, but if it is 1, the number is negative.
Binary representation of the following numbers in signed char is as follows:
Number = 127 Binary representation = 0111 1111 (leftmost bit is 0, indicating positive.)
Number = −128 Binary representation = 1000 0000 (leftmost bit is 1, indicating negative.)
The negative numbers are stored in a special form called "2's complement". It can be explained as follows:
Suppose you want to represent −127:
1. Convert 127 to binary form, i.e. 0111 1111.
2. Complement each bit: put a 0 wherever there is 1 and for 0 put 1. So you will get 1000 0000.
3. Add 1 to the above number
4. 1000 0000
5. + 1
6. -------------
7. 1000 0001 (−127)
Thus in the signed char you can have the range −128 to +127, i.e. (−28 to 28−1).
The binary representation also indicates the values in the case of overflow. Suppose you start with value 1 in char and keep adding 1. You will get the following values in binary representation:
0000 0001 (1)
0111 1111 (127)
1000 0000 (-128)
1000 0001 (-127)
In the case of unsigned char you will get
0000 0001 (1)
0111 1111 (127)
1000 0000 (128)
1000 0001 (129)
1111 1111 (255)
0000 0000 (0)
This concept is useful in finding out the behavior of the integer family data types.
The bytes allocated to the integer family data types are (1 byte = 8 bits) shown in Table 2.1.
Table 2.1: Integer data type storage allocations
Data Type Allocation Range
signed char 1 byte −27 to 27−1 (−128 to 127)
Unsigned char 1 byte 0 to 28−1 (0 to 255)
short 2 bytes −215 to 215 −1 (−32768 to 32767)
Unsigned short 2 bytes 0 to 216 −1 (0 to 65535)
long int 4 bytes 231 to 231−1 (2,147,483,648 to 2,147,483,647)
int 2 or 4 bytes depending on implementation Range for 2 or 4 bytes as given above
Explanation
1. In C, the range of the number depends on the number of bytes allocated and whether the number is signed.
2. If the data type is unsigned the lower value is 0 and the upper depends on the number of bytes allocated.
3. If the data type is signed then the leftmost bit is used as a sign bit.
4. The negative number is stored in 2's complement form.
5. The overflow behavior is determined by the binary presentation and its interpretation, that is, whether or not the number is signed.
Points to Remember
1. The behavior of a data type can be analyzed according to its binary representation.
2. In the case of binary representation, you have to determine whether the number is positive or negative.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment