[previous_group] [previous] [up] [next] [next_group]

Back to Numeric Processing

So far we have seen three data types:
* int
* float
* char
Some of these data types come in different sizes. As we have seen, characters need only 8 bits, since that is enough to encode all the characters we have in the ASCII character set. For numeric data types, the number of bits used to represent the numbers puts limitations on the size of the number which can be stored (or the accuracy with which we represent numbers).

Why is this so?

The number of bits we have determines the number of different patterns we can use to represent information.

For example, with 3 bits, we have

         3
        2  = 8
combinations; namely:
        0 0 0
        0 0 1
        0 1 0
        0 1 1
        1 0 0
        1 0 1
        1 1 0
        1 1 1
We can interpret these patterns of bits as binary numbers (i.e. numbers in base 2). The binary number system is a positional number system, like decimal, where the value of each digit is either 0 or 1, and the weight is determined by its position in the number. For binary, the weights are powers of 2:
        2    1    0
       2    2    2
                            2        1        0
       1    0    1   = 1 * 2  + 0 * 2  + 1 * 2   =  5
In general, for n bits, we have
      n
     2
combinations which can represent numbers in the range
        n
     [ 2 -1 .. 0]
for unsigned numbers or
         n-1         n-1
     [ +2    -1 .. -2    ]
for signed numbers (using a representation called two's compliment).

Can we control the size of integers in C?


[up] to Overview.

[next]