
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift Program to convert Decimal to Binary
This tutorial will discuss how to write Swift program to convert Decimal to Binary number.
Decimal numbers are those numbers whose base value is 10. Decimal numbers are also known as base-10 number system which contain 10 numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Here, the position of every digit in the decimal number has weight is a power of 10. For example, (89)10 = 8 x 101 + 9 x 100.
Binary numbers are those numbers whose base value is 2. Binary numbers are also known as base-16 number system which contain only two number 1 and 0. Here, the position of every digit in the binary number has weight is a power of 2.
For example, (1011)2 is a binary number. Binary numbers are the most commonly used number system in computer devices where every digit is represented by a bit.
Now we convert the decimal(base-10) into binary(base-2) number using any of the following methods.
Below is a demonstration of the same −
Input
Suppose our given input is −
Decimal number = 20
Output
The desired output would be −
Binary number = 10100
METHOD 1 - USING BITWISE OPERATOR
We can convert decimal number into binary number using bitwise right shift operator and AND(&) operator. It is much faster than arithmetic operators.
Example
The following program shows how to convert decimal to binary number.
import Foundation import Glibc // Decimal Number var decimalValue = 59 // String binary number var binaryValue = "" print("Decimal Number: ", decimalValue) // Converting decimal to binary number while(decimalValue > 0) { // Perform bitwise AND operation to find 1 and 0 if ((decimalValue & 1) == 1){ // Store “1” binaryValue += "1" } else { // Store “1” binaryValue += "0" } // Right shift the decimal number by 1 decimalValue >>= 1 } // Reversing the string var res = String(binaryValue.reversed()) print("Binary Number: ", res)
Output
Decimal Number: 59 Binary Number: 111011
METHOD 2 - USING POWER
We can also convert binary to decimal number by extracting each digit from the number and then multiply the extracted digit by base(power of 10) and add to the binary variable. At the end of the program you will get all the binary digits in the binary variable.
Example
The following program shows how to convert decimal to binary number.
import Foundation import Glibc // Decimal Number var decimalValue = 19 // String binary number var binaryValue = 0 var count = 0.0 print("Decimal Value:", decimalValue) // Converting decimal to binary number while(decimalValue != 0){ // Extraction rightmost digit from the decimal number using remainder let remainder = decimalValue % 2 // Find power of 10 let value = pow(10.0, count) // Now multiply the digit with the base(power of 2) and add the value to the binaryValue binaryValue += remainder * Int(value) // Divide the decimal number by 2 decimalValue /= 2 // Store the exponent value count += 1 } print("Binary Value:", binaryValue)
Output
Decimal Value: 19 Binary Value: 10011
METHOD 3 - USING PRE-DEFINED FUNCTION
We can also convert decimal to binary number system using String(_:radix:). This method creates a new value from the given string/number and radix.
Syntax
Following is the syntax −
String(value, radix: base)
Here, value is the ASCII representation of a number. Whereas radix is used to convert text into integer value. The default value of radix is 10 and it can be in the range from 2…36.
Example
The following program shows how to convert decimal to binary number.
import Foundation import Glibc // Decimal number var number = 5 print("Decimal Number:", number) // Converting decimal to binary let BinaryNumber = String(number, radix: 2) print("Binary Number:", BinaryNumber)
Output
Decimal Number: 5 Binary Number: 101
Here, we convert the decimal number 5 into binary number using the following code −
let BinaryNumber = String(number, radix: 2)
Where String(number, radix: 2) convert the given decimal number into binary number. Hence the resultant binary number is 101.
- Related Articles
- Swift Program to convert Binary to Decimal
- Swift program to convert the decimal number to binary using recursion
- Swift Program to convert Decimal to Octal
- Swift Program to convert Hexadecimal to Decimal
- Swift Program to convert Decimal to Hexadecimal
- C# Program to Convert Binary to Decimal
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal
- C# Program to Convert Decimal to Binary\n
- Java Program to convert from decimal to binary
- C++ Program To Convert Decimal Number to Binary
- Python program to convert decimal to binary number
- Java program to convert decimal number to binary value
- Java program to convert binary number to decimal value
- Java Program to convert binary number to decimal number
