# Is there a way to find out if a number contains a specific decimal digit from its binary representation?

If the number has 2 on it: starting from 2 >> 0000 0010, 12 >> 0000 1100, 20 >> 0001 0100 and so on.

I tried to figure out a relationship whether the number contains e.g. 2 and the number of one's and their position on its binary representation? Is there a relationship in the first place?

• "Is there a way to find out if a number contains a specific decimal digit from its binary representation?" Answer: "Yes. You convert the number into decimal and check if a 2 appears". – 5xum Mar 29 '17 at 8:07
• A comment to 5xum's comment: (@Muhammed) Your question has to be more precise. You could ask: "Which is the easiest way to get ...?" because it is always possible to do the check "the hard way" (by converting binary to decimal first). – Martin Rosenau Mar 29 '17 at 8:10
• Okay, Is there a "pattern" to generically distinguish a decimal digit in a binary-encoded number "Without converting to decimal"? – Mosaaleb Mar 29 '17 at 8:11
• I would be very surprised if this is possible! – Peter Mar 29 '17 at 8:38

There is a relationship, of course. Just continue listing all numbers with $2$ in their decimal representations, and give the binary for each, and you have the members of that relationship.

To derive an algorithm that will allow you to deduce whether there is a $2$ in the decimal representation of some binary number, without converting the binary number to decimal or consulting a list such as the one described in the last paragraph, is a harder question.

For example, the binary representation of $20\,000\,000_\mathrm{ten}$ is $1001100010010110100000000_\mathrm{two}.$ There are eight $1$s in that representation, and if we change the binary number just by changing any of these $1$s to a $0,$ the decimal representation will not have a $2$ in the $10^7$ place. On the other hand, we can change some of the $1$s to zero and still have a $2$ in the $10^7$ place, provided that we change certain $0$s to $1$s. In short, if we have a large binary number, we have to look at more than eight bits of the binary number in order to determine whether there is a $2$ in the $10^7$ place of the decimal representation of that number.

It is actually worse than this sounds, because powers of $2$ typically have non-zero digits in most places of their decimal representations, so when you change the binary digit of a large number that corresponds to such a power of $2,$ it changes most of the digits of the decimal representation of that number in places that are less than that power of $2.$ That means that whether a $2$ occurs at the $10^n$ position in a decimal number depends not only on many of the digits that would occur in the binary representation of $10^n,$ but also on many of the digits to their left.

I also discuss the question of determining a digit of a number in this answer.

In short, if you want to see whether a particular digit of the decimal representation of a binary number will be a $2,$ the number of digits of the binary number that you will have to examine is limited only by the size of the binary number itself. As for whether there is some pattern in the digits that you can use to easily evaluate whether all these binary digits somehow determine a $2$ in the decimal representation: if anyone had found such a pattern that is easier to apply than simply converting the number to decimal, we would already be using it to make binary-to-decimal or decimal-to-binary conversions easier.

This is not a rigorous proof that no such "easy" method exists, but the prospects to find one do not look good.