# Decimal to binary formula c++

Every integer has an equivalent representation in decimal and binary. Except for 0 and 1, the binary representation of an integer has more digits than its decimal counterpart. To find the number of binary digits bits corresponding to any given decimal integer, you could convert the decimal number to binary and count the bits.

For example, the two-digit decimal integer 29 converts to the five-digit binary integer Sometimes you want to know, not how many bits are required for a specific integer, but how many are required for a d -digit integer — a range of integers.

A range of integers has a range of bit counts. For example, four-digit decimal integers require between 10 and 14 bits. For any d -digit range, you might want to know its minimum, maximum, or average number of bits. Those values can be computed directly as well. In this article, I will show you those calculations. All of the discussion assumes positive integers, although it applies to negative integers if you temporarily ignore their minus signs. An integer is an integer, independent of its base.

For larger numbers, you could consult a table of powers of two to find the consecutive powers that contain your number. To see why this works, think of the binary representations of the integers 2 4 through 2 5 — 1, for example. They are throughall possible 5-bit values. The above method can be stated another way: You can state that mathematically as:.

How many bits do numbers in this range require? For example, consider four-digit decimal integers. The number of bits varies between those extremes.

For example, requires 11 bits, requires 12 bits, and requires 13 bits. Why does this occur? Because that single power of ten range spans all or part of five consecutive power-of-two ranges. The minimum number of bits required for a d -digit integer is computed simply by using the specific number formula on the minimum d -digit value:.

The maximum number of bits required for a d -digit integer is computed simply by using the specific number formula on the maximum d -digit value:. This allows us to use this more computationally efficient formula to the same effect:. The average number of bits required for a d -digit integer is the total number of bits required to represent all d -digit integers divided by the number of d -digit integers.

For our example, the average is. First we have to figure out how to take the base-2 logarithm of a number. Many programming environments do not have a base-2 logarithm function.

You can deal with that by doing a change of base:. You have to be careful when computing logarithms; floating-point inaccuracies could cause an incorrect result, particularly at power-of-two boundaries. People commonly round this to 3. You need to specify the constant with more precision to get the correct result.

I think there is something wrong with the counting of bits on your site. Yes, 2 5 is 6 bits and my formula gives that. Can you show me specifically what you calculated? I heard in class that an integer of size n takes O log n bits. Yes, you are correct. That is used commonly too. Thank you for your information sir. Can I know the formula for calculating the number of digits for n-bits?

Could you mail me if possible. Check out the companion article: Sir, It was a very informative post. I came across it through google search for minimum bits required for representing a decimal fraction. I am still searching for the answer. Your email address will not be published. Skip to content Every integer has an equivalent representation in decimal and binary.

Get articles by e-mail. Willie, Yes, 2 5 is 6 bits and my formula gives that. Sometimes blogs are better than books! Roy, Thanks for the feedback. Aravind, Check out the companion article: Vachaspati, Please explain what you are looking for an example would help. Leave a Reply Cancel reply Your email address will not be published.

Ratio of Bits to Decimal Digits.