1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Immedaite Help

  1. Oct 11, 2009 #1
    1. The problem statement, all variables and given/known data

    In this problem I need to check if the input num. from the useer is a binary or Not.
    And, then I need to convert this Binary Num. to Decimal.

    3. The attempt at a solution

    Now, for a number to be a Binary it should only contains 0s and 1s,
    should I make an if statment ?
    But, even if I make this if it will be so complex, because I have to check that every num. except 0 and 1 does not exist ?

    Or, there is a method to check that ?
    Last edited: Oct 11, 2009
  2. jcsd
  3. Oct 12, 2009 #2
  4. Oct 12, 2009 #3


    User Avatar
    Science Advisor

    Binary numbers are not the only ones that contain only 1s and 0s.

    The number 1010101 is a perfectly valid Decimal number.
  5. Oct 12, 2009 #4
    that is true, but even humans cannot distinguish between "1010101" and "ten million one hundred and one thousand and one" in integer form. So there is no way to program that with 0% error unless you ask the user if the input is a binnary number or an integer.
  6. Oct 12, 2009 #5
    Thanks alot!!

    I will check this =)
  7. Oct 13, 2009 #6


    User Avatar
    Science Advisor

    That is right.
    A number with just ones and zeros can occur in a string of decimal numbers.

    You can work out the odds of this happening.
    Each digit has to be a 1 or a zero and not a number between 2 and 9, inclusive.

    So there is a 20% chance of this happening once, a 4 % chance of it happening twice and so on.
    If you took the first 10 digits and they were all 0s or 1s the odds against this happening randomly in a decimal number would be nearly 10 million to 1.

    To be 100% sure, though, the data should be tagged with some marker that it is binary.
    It is traditional to use the percent sign as a tag to indicate binary. So, you would say %11111 = 31
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook