
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find 1's Complement of a Given Number in Swift
1's complement of a binary number is the invert of the given number. For example, we have a number = 10101011, so the one's complement is 01010100. Here we using the following two methods to find one's complement ?
Replacing 1s with 0s and 0s with 1s
Using XOR
In this article, we will learn how to write a swift program to find the 1's complement of the given number.
Method 1: Replacing 1s with 0s and 0s with 1s
It is the smallest method to find ons complement of the given number. Here we simply convert the given number into binary string and replace 0s with 1s and 1s with 0s and convert the string back into number.
Algorithm
Step 1 ? Create a function.
Step 2 ? Convert the given number into binary string.
Step 3 ? Use for-in loop to flip the bits and store the result into new string.
Step 4 ? Convert the string back into number.
Step 5 ? Create a variable to store the input number and pass it to the function.
Step 6 ? Print the output.
Example
Following Swift program to find the 1's complement of the given number.
import Foundation import Glibc // Function to find one's complement func onesComplement(num: Int) -> Int { // Converting number into binary string let binaryString = String(number, radix: 2) var onecomplement = "" // Replacing 1s with 0s and 0s with 1s for char in binaryString { onecomplement += (char == "0") ? "1" : "0" } // Converting binary string back into number return Int(onecomplement, radix: 2)! } // Test input let number = 22 let result = onesComplement(num: number) print("One's complement of 22 is ", result)
Output
One's complement of 22 is 9
Here in the above code, we create a function to find one's complement. In this function, first we convert the input number into a binary string using String() initializer. Then we use for loop to iterate through each character of the string and replace 0s with 1s and 1s with 0s. Then convert the binary string back into the number using Int() initializer.
Method 2: Using XOR
In this method, we find the one's complement using XOR. So, here we first find the number of bits required to represent given number and then create a mask by adding 1 to the left shift of the number of bits. The we calculate the one's complement by performing XOR operation with the given number and the mask.
Algorithm
Step 1 ? Create a function.
Step 2 ? Convert the given number into binary string and then count the total number of bits.
-
Step 3 ? XOR the given integer.
let mask = (1 << count) - 1
let onecomplement = number ^ mask
Step 4 ? Create a variable to store the input number and pass it to the function.
Step 5 ? Print the output.
Example
Following Swift program to find the 1's complement of the given number.
import Foundation import Glibc // Function to find one's complement func onesComplement(number: Int) -> Int { let count = String(number, radix: 2).count // XOR the given number let mask = (1 << count) - 1 let onecomplement = number ^ mask return onecomplement } // Test input let value = 9 print("One's complement of \(value) is \(onesComplement(number: value))")
Output
One's complement of 9 is 6
Here in the above code, we create a function to find one's complement. In this function, first we calculate the total numbers of bits required to represent the given number int the binary form. Then calculate the mask using (1 << count) - 1 which is used to XOR the given number. Then calculate the ones's complement by XORing the given number with the mask(number ^ mask) and return the result. Now call this function and pass input number in it and display the result.
Conclusion
In this article, we have learned two different methods to find 1's complement of a given number. In the first method, we are simply inverting the 1's and 0's of the given number, whereas in the second method, we are using the XOR gate to find the 1's complement.