Find 1s complement of an Integer
The problem involves finding the one's complement of a given integer. The one's complement of an integer is obtained by changing all the 1s to 0s and all the 0s to 1s in its binary representation. In this article, we will explore the concept of finding the one's complement of an integer and using bitwise operations.

Problem Statement and Description
Given an integer num
, the goal is to find its one's complement. To do this, we need to invert each
bit in the binary representation of the integer. This means that every 0 should be changed to 1, and every 1
should be changed to 0.
Example
Consider the following examples to understand the concept:
-
For the number
9
(binary1001
): The one's complement is0110
, which is6
in decimal. -
For the number
50
(binary110010
): The one's complement is001101
, which is13
in decimal. -
For the number
32
(binary100000
): The one's complement is011111
, which is31
in decimal.
Idea to Solve the Problem
The idea to solve the problem involves the following steps:
-
If the given number
num
is negative, simply apply the bitwise NOT operation (~num
) to find the one's complement. -
If the given number
num
is positive, calculate the one's complement using bitwise operations.
Standard Pseudocode
function onesComplement(num):
if num is negative:
Print "Number :", num
Print "1s Complement :", ~num
else:
total_bits = log2(num) + 1
ones = ((1 << total_bits) - 1) XOR num
Print "Number :", num
Print "1s Complement :", ones
Algorithm Explanation
-
Check if the given number
num
is negative. If it is negative, apply the bitwise NOT operation (~num
) to find the one's complement and print the result. -
If the given number
num
is positive, calculate the total number of bits in the binary representation ofnum
usingtotal_bits = log2(num) + 1
. -
Calculate the one's complement of the positive number by performing a bitwise XOR operation between the integer obtained by performing bitwise OR operations on
1 << total_bits
andnum
. This effectively flips all the bits to find the one's complement. -
Print the original number and its corresponding one's complement.
Code Solution
-
1) Find 1s complement of a number in java
2) Find 1s complement of a number in c++
3) Find 1s complement of a number in c
4) Find 1s complement of a number in c#
5) Find 1s complement of a number in php
6) Find 1s complement of a number in node js
7) Find 1s complement of a number in python
8) Find 1s complement of a number in ruby
9) Find 1s complement of a number in scala
10) Find 1s complement of a number in swift
11) Find 1s complement of a number in kotlin
12) Find 1s complement of a number in golang
13) Find 1s complement of a number in typescript
Time Complexity
The time complexity of the algorithm depends on the number of bits in the binary representation of the given
number. The most significant factor is the calculation of the total number of bits, which is
O(log(num))
.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment