easy way to Add in base 2 with Obj-C?

Discussion in 'iPhone/iPad Programming' started by oxcug, Feb 1, 2012.

  1. macrumors newbie

    Joined:
    Oct 3, 2011
    Messages:
    24
    #1
    I am trying to create a binary calculator in Objective-C. Is there an easy way to add a bit to a base 2 number in the decimal system? So for example, I have

    011

    I want to add a bit to this number making it 100 (4) instead of 012.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Messages:
    15,045
    Location:
    The Centennial State
    #2
    Remember, Objective-C is a superset of C.
     
  3. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    Messages:
    2,675
    #3
    4 = 3 + 1
     
  4. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Messages:
    15,043
    Location:
    Quebec, Canada
    #4
    You're representing your binary how exactly ? A string ? An integer value ? An "integerized" binary number (storing decimal 11 to mean 2, etc..).

    Some details/code would go a long way to helping us help you.

    If you're just storing integers, then obviously, adding a bit means simply incrementing by 1.
     
  5. macrumors 68040

    Joined:
    Feb 2, 2008
    Messages:
    3,360
    #5
    If you add 1 to 011 and get 12, then 011 is really eleven not three. You can not work directly with binary values in C, you have decimal, octal and hex representations available.

    You should probably start with creating methods for converting to/from binary strings and integer variables, then you can operate on those variables. The methods would only be used to get and present input to the user.
     

Share This Page