Search code examples
cbinaryunsignedbitunsigned-integer

Confused about the value of a number and the range of the number in C


I missed my lecture and I don't understand the last two points. Can someone explain the third point and show me how to calculate the forth point? Thanks


Solution

  • The value of a number is the sum of the values of the bit positions containing 1.

    If converting a number from binary to decimal, you have to keep on multiplying the bit value with 2^(position value - 1) //[ assuming position value starts from 1 on the rightmost side] and summing the value with the previous values obtained in this way.

    So, if the bit is 0, then the result has to be 0, irrespective of the bit-position. Hence, it doesn't add any value to the number. Only the bits having 1 count to the value.

    What range of numbers can be represented in an unsigned integer type with : 4 bits, 8 bits, 16 bits, 32 bits, n bits?

    For an unsigned integer of n bits, the integers represented can be from 0 to (2^n)-1.

    So, 4 bits can represent numbers from 0 to (2^4) - 1 = 15.
    8 bits can represent numbers from 0 to (2^8) - 1 = 255.
    16 bits can represent numbers from 0 to (2^16) - 1 = 65,535.
    32 bits can represent numbers from 0 to (2^32) - 1 = 4,294,967,295.
    

    And so on.