Kotlin Program to Convert Decimal to Binary (Using Recursion, Loop, Bitwise, and More)
Introduction
Kotlin is a modern, concise, and expressive programming language that works on Android and other platforms.
If you’re learning number systems or want to build conversion tools, knowing how to convert decimal to binary in Kotlin is very useful. Let's explore 6 different methods with beginner-friendly explanations.
Problem Statement
Write multiple Kotlin programs to convert a decimal number into binary using different methods like loops, recursion, bitwise operations, and built-in utilities.
Use Cases
-
Teaching number system conversions to students.
-
Developing utilities in Android apps.
-
Handling binary representations in compilers or interpreters.
-
Understanding core logic and recursion in Kotlin.
Method 1: Using While Loop and StringBuilder
// Convert Decimal to Binary using While Loop
fun convertUsingLoop(number: Int): String {
if (number == 0) return "0"
var num = number
val binary = StringBuilder()
while (num > 0) {
binary.append(num % 2)
num /= 2
}
return binary.reverse().toString()
}
fun main() {
println("Binary (37): ${convertUsingLoop(37)}")
}
Explanation
-
Use modulo
%
to get remainders. -
Append each bit to a StringBuilder.
-
Reverse the binary string at the end.
Output
Binary (37): 100101
Method 2: Using Recursion
// Convert Decimal to Binary using Recursion
fun convertRecursive(number: Int): String {
if (number == 0) return ""
return convertRecursive(number / 2) + (number % 2)
}
fun main() {
val result = convertRecursive(19)
println("Binary (19): ${if (result.isEmpty()) "0" else result}")
}
Explanation
-
Break the number using division.
-
Combine the result from smaller numbers recursively.
-
Append remainder at each return.
Output
Binary (19): 10011
Method 3: Using Bitwise Operator
// Convert Decimal to Binary using Bitwise Operator
fun convertUsingBitwise(number: Int): String {
if (number == 0) return "0"
val binary = StringBuilder()
var started = false
for (i in 31 downTo 0) {
val bit = (number shr i) and 1
if (bit == 1) started = true
if (started) binary.append(bit)
}
return binary.toString()
}
fun main() {
println("Binary (12): ${convertUsingBitwise(12)}")
}
Explanation
-
Use
shr
to shift bits right. -
Use
and
to extract bit.1
-
Skip leading
0s
until the first1
.
Output
Binary (12): 1100
Method 4: Using Built-in Function
// Convert Decimal to Binary using Built-in toString(radix)
fun convertBuiltIn(number: Int): String {
return number.toString(2)
}
fun main() {
println("Binary (64): ${convertBuiltIn(64)}")
}
Explanation
-
Kotlin allows
number
for base conversion..toString(radix)
-
Use
2
for binary radix.
Output
Binary (64): 1000000
Method 5: Stack Simulation with ArrayList
// Convert Decimal to Binary using Stack Logic
fun convertUsingStack(number: Int): String {
if (number == 0) return "0"
val stack = mutableListOf<String>()
var num = number
while (num > 0) {
stack.add((num % 2).toString())
num /= 2
}
return stack.reversed().joinToString("")
}
fun main() {
println("Binary (21): ${convertUsingStack(21)}")
}
Explanation
-
Push each bit to a list (like a stack).
-
Reverse the list to fix the order.
-
Join list into a final string.
Output
Binary (21): 10101
Method 6: Manual Append from Right to Left
// Convert Decimal to Binary by Appending at Start
fun convertManual(number: Int): String {
if (number == 0) return "0"
var num = number
var binary = ""
while (num > 0) {
binary = (num % 2).toString() + binary
num /= 2
}
return binary
}
fun main() {
println("Binary (18): ${convertManual(18)}")
}
Explanation
-
Keep collecting remainders.
-
Add them to the start of the string.
-
Stop when number is 0.
Output
Binary (18): 10010
Conclusion
In this article, we covered 6 unique methods to perform decimal to binary conversion in Kotlin using loops, recursion, bitwise, and built-in functions. These techniques help you understand how binary numbers are formed and how Kotlin handles control flow and string operations.