Calculating bits required to store decimal number

后端 未结 12 785
自闭症患者
自闭症患者 2020-12-23 22:21

This is a homework question that I am stuck with.

Consider unsigned integer representation. How many bits will be required to store a decimal number

12条回答
  •  Happy的楠姐
    2020-12-23 22:56

    The simplest answer would be to convert the required values to binary, and see how many bits are required for that value. However, the question asks how many bits for a decimal number of X digits. In this case, it seems like you have to choose the highest value with X digits, and then convert that number to binary.

    As a basic example, Let's assume we wanted to store a 1 digit base ten number, and wanted to know how many bits that would require. The largest 1 digit base ten number is 9, so we need to convert it to binary. This yields 1001, which has a total of 4 bits. This same example can be applied to a two digit number (with the max value being 99, which converts to 1100011). To solve for n digits, you probably need to solve the others and search for a pattern.

    To convert values to binary, you repeatedly divide by two until you get a quotient of 0 (and all of your remainders will be 0 or 1). You then reverse the orders of your remainders to get the number in binary.

    Exampe: 13 to binary.

    • 13/2 = 6 r 1
    • 6/2 = 3 r 0
    • 3/2 = 1 r 1
    • 1/2 = 0 r 1
    • = 1101 ((8*1) + (4*1) + (2*0) + (1*1))

    Hope this helps out.

提交回复
热议问题