Swift Decimal to Binary Conversion – 4 New Approaches Explained Simply
What You'll Learn
In this Swift guide, you'll learn how to convert a decimal number into its binary form using 4 unique and beginner-friendly methods.
These examples are different from our first Swift article and provide even more ways to understand the logic behind binary number generation.
Why You Might Need This
-
Build custom number conversion tools for iOS/macOS.
-
Understand binary systems for low-level Swift apps.
-
Learn Swift control flows like loops, recursion, and logic operators.
-
Practice problems for coding interviews or student projects.
Binary Conversion Using While Loop with Prepending
// Convert decimal to binary using a while loop and string prepending
func decimalToBinaryLoop(_ number: Int) -> String {
if number == 0 { return "0" }
var num = number
var binary = ""
while num > 0 {
binary = String(num % 2) + binary
num /= 2
}
return binary
}
print("Binary of 27:", decimalToBinaryLoop(27))
Steps Explained
-
Start with an empty string
binary
. -
Divide the number by
2
each time and store the remainder. -
Prepend
(+)
each remainder to the front of the string. -
Stop when the number becomes
0
.
Output
Binary of 27: 11011
Recursive Binary Conversion (Optimized)
// Recursive method to convert decimal to binary with fallback for 0
func decimalToBinaryRecursive(_ number: Int) -> String {
if number == 0 { return "" }
return decimalToBinaryRecursive(number / 2) + String(number % 2)
}
let result = decimalToBinaryRecursive(33)
print("Binary of 33:", result.isEmpty ? "0" : result)
Steps Explained
-
Use recursion to divide the number until it reaches
0
. -
On each return, append the remainder
(% 2)
to form the binary string. -
If the result is empty (i.e., number was 0), return
"0"
.
Output
Binary of 33: 100001
Bitwise Shift and Append Strategy
// Bitwise method to extract binary digits using shifts
func decimalToBinaryBitwise(_ number: Int) -> String {
if number == 0 { return "0" }
var result = ""
var started = false
for i in (0..<32).reversed() {
let bit = (number >> i) & 1
if bit == 1 { started = true }
if started { result.append(String(bit)) }
}
return result
}
print("Binary of 40:", decimalToBinaryBitwise(40))
Steps Explained
-
Use
>>
to shift bits and& 1
to check if that bit is1
or0
. -
Begin building the binary only after the first
1
appears (to skip leading 0s). -
Append bits to
result
.
Output
Binary of 40: 101000
Swift’s Built-in Conversion Method (Revisited)
// Using Swift’s toString radix:2 conversion
func decimalToBinaryBuiltIn(_ number: Int) -> String {
return String(number, radix: 2)
}
print("Binary of 14:", decimalToBinaryBuiltIn(14))
Steps Explained
-
Swift has a built-in way to convert numbers to any base using
String(_:radix:)
. -
Passing
radix: 2
converts the number to binary.
Output
Binary of 14: 1110
Wrapping Up
These four Swift programs demonstrate a diverse set of logic structures — including while loops, recursion, bitwise operators, and built-in shortcuts — for converting decimal numbers into binary.
This reinforces key Swift programming skills while deepening your understanding of binary computation.