previous | start | next

Converting binary to decimal

Converting binary to decimal depends on whether the binary pattern is to interpreted as representing only positive values or to use some convention to also represent negative values. C uses unsigned integer types for the first case.



Bits are numbered 0 to 7 from the LSB (least significant bit) to the
MSB (most significant bit)

binary representation:            00011101
bit position:                     76543210

The value of a bit at position k is multiplied by 2k.

A 1 bit in position 4 is worth 24 or 16.
A 1 bit in position 3 is worth 23 or 8.

So for the 8 bits of a 1 byte integer, the multiplers for each bit
position are:
                   _  _  _  _  _  _  _  _

                 128 64 32 16  8  4  2  1
          

For the value 00011101, 

                   0  0  0  1  1  1  0  1
                   _  _  _  _  _  _  _  _

                 128 64 32 16  8  4  2  1

the corresponding decimal value is 16*1 + 8*1 + 4*1 + 1*1 = 29
             


previous | start | next