View Full Version : easy way to Add in base 2 with Obj-C?

oxcug

Feb 1, 2012, 08:58 PM

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.

dejo

Feb 1, 2012, 09:54 PM

Remember, Objective-C is a superset of C.

PhoneyDeveloper

Feb 2, 2012, 07:12 AM

4 = 3 + 1

KnightWRX

Feb 2, 2012, 07:39 AM

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.

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.

subsonix

Feb 2, 2012, 09:16 AM

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.