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.

Updated on: 30-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements