Convert Binary to Decimal in Swift
Last Updated :
24 Oct, 2023
In computers, we use two main number systems: binary (with 0s and 1s) and decimal (with numbers from 0 to 9). Converting binary numbers (like 1010) into decimal numbers (like 10) is a common task in programming. In this article, we'll learn how to do this in Swift, a programming language developed by Apple.
Problem Statement
The problem is to convert a binary number represented as a string to its decimal equivalent in Swift.
Example:
Example 1:
Binary Number: "1010"
Decimal Equivalent: 10
Example 2:
Binary Number: "11011"
Decimal Equivalent: 27
Approach 1: Using the Built-in Binary Integer Function
In this method, we use a special function built into Swift to do the conversion easily.
Swift
// Auther: Nikunj Sonigara
let binaryString = "1010"
let decimalNumber = Int(binaryString, radix: 2)!
print("binaryString: ", binaryString) // binaryString: 1010
print("decimalNumber: ", decimalNumber) // decimalNumber: 10
Output:
binaryString: 1010
decimalNumber: 10
Here, we define our binary number as a string called binaryString. We then use the Int function with radix set to 2 to tell Swift that it's a binary number. The result is stored in decimalNumber.
Complexity Analysis:
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach 2: Implementing the Conversion Algorithm Manually
In this method, we write our own code to convert binary to decimal.
Swift
func binaryToDecimal(binary: String) -> Int {
var decimal = 0
var base = 1
for digit in binary.reversed() {
if digit == "1" {
decimal += base
}
base *= 2
}
return decimal
}
let binaryString = "1010"
let decimalNumber = binaryToDecimal(binary: binaryString)
print("binaryString: ", binaryString) // binaryString: 1010
print("decimalNumber: ", decimalNumber) // decimalNumber: 10
Output
binaryString: 1010
decimalNumber: 10
Here, we create a function called binaryToDecimal that takes the binary string as input and calculates the decimal number. We go through each digit in the binary string, and if it's a 1, we add a certain amount to the decimal value. We multiply this amount by 2 for each digit we process. Finally, we return the decimal value.
Complexity Analysis
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Swift - Convert String to Int Swift Swift provides a number of ways to convert a string value into an integer value. Though, we can convert a numeric string only into an integer value. In this article, we will see the two most common methods used to convert a string to an integer value. A string is a collection of characters. Swift pr
3 min read
Data Type Conversions in Swift Before we start let's see what is type conversion. Type Conversion is used to check the type of instances to find out whether it belongs to a particular class type or not using the type conversion method or approach. Basically, a Type Conversion is a method in which we can convert one instance data
5 min read
How to Reverse an Array in Swift? Swift provides various generic collections and array is one of them. Just like other programming languages, in Swift also an array is an ordered collection that is used to store the same type of values like int, string, float, etc., you are not allowed to store the value of another type in an array,
3 min read
How to Swap Array items in Swift? Swift supports various generic collections like set, dictionary, and array. In Swift, an array is an ordered collection that is used to store the same type of values like string, int, float, etc. In an array, you are not allowed to store the value of a different type in an array, for example, if you
2 min read
cbrt() in swift The cbrt() function returns the cube root of a number. It is an inbuilt function in Swift. The cbrt() function in Swift returns the cube root of a number. Using the cbrt() function in Swift, we can calculate the cube root of any natural, decimal, and negative number (perfect and non-perfect cubes).
2 min read
Swift - Integer, Floating-Point Numbers Integers are whole numbers that can be negative, zero, or positive and cannot have a fractional component. In programming, zero and positive integers are termed as âunsignedâ, and the negative ones are âsignedâ. Swift provides these two categories in the form of 8-bit, 16-bit, 32-bit and 64-bit form
2 min read