Objective-C Program to Convert Decimal to Binary (Loop, Recursion, Bitwise, NSString)
Overview
In this article, we will write Objective-C programs to convert a decimal number into binary using different approaches like loops, recursion, bitwise shifts, and NSString manipulations.
Objective-C is a C-based language, so these examples combine C logic with Objective-C syntax.
When Do You Need This?
-
To build iOS/macOS tools that deal with binary logic
-
To understand how low-level operations work in Objective-C
-
To practice for systems-level or embedded programming interviews
Loop-Based Binary Conversion
// Convert decimal to binary using a while loop
#import <Foundation/Foundation.h>
NSString* binaryUsingLoop(int number) {
if (number == 0) return @"0";
NSMutableString *binary = [NSMutableString string];
while (number > 0) {
[binary insertString:[NSString stringWithFormat:@"%d", number % 2] atIndex:0];
number = number / 2;
}
return binary;
}
int main() {
@autoreleasepool {
NSLog(@"Binary of 25: %@", binaryUsingLoop(25));
}
return 0;
}
Explanation
-
% 2
gives the remainder (bit). -
Insert each bit at the beginning of the string.
-
Simple while loop tracks until number becomes
0
.
Output
Binary of 25: 11001
Recursive Method for Binary Conversion
// Recursive approach to convert decimal to binary
NSString* binaryRecursive(int number) {
if (number == 0) return @"";
return [NSString stringWithFormat:@"%@%d", binaryRecursive(number / 2), number % 2];
}
int main() {
@autoreleasepool {
NSString *binary = binaryRecursive(18);
NSLog(@"Binary of 18: %@", binary.length == 0 ? @"0" : binary);
}
return 0;
}
Explanation
-
Recursive division by
2
. -
Append remainder at each return step.
-
Fallback for
0
handled using length check.
Output
Binary of 18: 10010
Bitwise Shift Method
// Bitwise conversion from decimal to binary
NSString* binaryUsingBitwise(int number) {
NSMutableString *binary = [NSMutableString string];
BOOL started = NO;
for (int i = 31; i >= 0; i--) {
int bit = (number >> i) & 1;
if (bit == 1) started = YES;
if (started) [binary appendFormat:@"%d", bit];
}
return binary.length == 0 ? @"0" : binary;
}
int main() {
@autoreleasepool {
NSLog(@"Binary of 42: %@", binaryUsingBitwise(42));
}
return 0;
}
Explanation
-
Shift the number to the right
>> i
and check each bit. -
Use
& 1
to get the bit value. -
Start appending only after the first
1
.
Output
Binary of 42: 101010
NSString Built-in with Custom Formatter
// Using NSString and NSNumber for binary conversion
NSString* binaryUsingNSString(int number) {
if (number == 0) return @"0";
NSMutableString *binary = [NSMutableString string];
while (number > 0) {
[binary insertString:[@(number % 2) stringValue] atIndex:0];
number /= 2;
}
return binary;
}
int main() {
@autoreleasepool {
NSLog(@"Binary of 13: %@", binaryUsingNSString(13));
}
return 0;
}
Explanation
-
Similar to the loop method but uses
NSNumber
andNSString
features. -
Great for Objective-C style string handling.
Output
Binary of 13: 1101
Wrap-Up
In this Objective-C article, we explored 4 different ways to convert decimal to binary. Each method highlights different core concepts—loops, recursion, bitwise manipulation, and Objective-C string handling.