- 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 find the 1's complement of the given number

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.

- Related Articles
- Haskell Program to find the 1's complement of the given number
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1's and 2's complement of 16-bit number
- Draw a Turing machine to find 1’s complement of a binary number
- 1’s and 2’s complement of a Binary Number?
- C Program to find two’s complement for a given number
- Swift program to find the reverse of a given number using recursion
- Swift Program to find the given number is strong or not
- 1's Complement vs 2's Complement
- 8085 program to find 2's complement of the contents of Flag Register
- Previous number same as 1’s complement in C++
- Program to find number of bit 1 in the given number in Python
- Swift Program to get the imaginary part of the given complex number
- Swift Program to find the hyperbolic arctangent of the given value
- Swift Program to find the sine of given radian value