# Find 1s complement of an Integer

Here given code implementation process.

``````// C Program
// Find 1s complement of an Integer
#include <stdio.h>
#include <math.h>

// Returns the number of bits in given num
int totalBits(int num)
{
return (int)(log(num) / log(2)) + 1;
}
// Find one's complement of given number
void onesComplement(int num)
{
// Display given number
printf("\n Number : %d",num);
if(num < 0)
{
printf("\n 1s Complement : %d", (~num));
}
else
{
// Calculate 1s complement
int ones = ((1 << totalBits(num)) -1) ^ num;

// Display calculated result
printf("\n 1s Complement : %d",ones);
}

}
int main(int argc, char const *argv[])
{

// 50 (1001) => (0110) (6)
onesComplement(9);
// 50 (110010) => (001101) (13)
onesComplement(50);
// (32) = 100000 => 11111 (31)
onesComplement(32);
return 0;
}``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````
``````/*
Java program
Find 1s complement of an Integer
*/
public class Complement
{
// Returns the number of bits in given num
public int totalBits(int num)
{
return (int)(Math.log(num) / Math.log(2)) + 1;
}
// Find one's complement of given number
public void onesComplement(int num)
{
// Display given number
System.out.print("\n Number : " + num);
if (num < 0)
{
System.out.print("\n 1s Complement : " + (~num));
}
else
{
// Calculate 1s complement
int ones = ((1 << totalBits(num)) - 1) ^ num;
// Display calculated result
System.out.print("\n 1s Complement : " + ones);
}
}
public static void main(String[] args)
{
// 50 (1001) => (0110) (6)
// 50 (110010) => (001101) (13)
// (32) = 100000 => 11111 (31)
}
}``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````
``````// Include header file
#include <iostream>
#include <math.h>

using namespace std;
/*
C++ program
Find 1s complement of an Integer
*/
class Complement
{
public:
// Returns the number of bits in given num
int totalBits(int num)
{
return (int)(log(num) / log(2)) + 1;
}
// Find one's complement of given number
void onesComplement(int num)
{
// Display given number
cout << "\n Number : " << num;
if (num < 0)
{
cout << "\n 1s Complement : " << (~num);
}
else
{
// Calculate 1s complement
int ones = ((1 << this->totalBits(num)) - 1) ^ num;
// Display calculated result
cout << "\n 1s Complement : " << ones;
}
}
};
int main()
{
// 50 (1001) => (0110) (6)
// 50 (110010) => (001101) (13)
// (32) = 100000 => 11111 (31)
return 0;
}``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````
``````// Include namespace system
using System;
/*
C# program
Find 1s complement of an Integer
*/
public class Complement
{
// Returns the number of bits in given num
public int totalBits(int num)
{
return (int)(Math.Log(num) / Math.Log(2)) + 1;
}
// Find one's complement of given number
public void onesComplement(int num)
{
// Display given number
Console.Write("\n Number : " + num);
if (num < 0)
{
Console.Write("\n 1s Complement : " + (~num));
}
else
{
// Calculate 1s complement
int ones = ((1 << totalBits(num)) - 1) ^ num;
// Display calculated result
Console.Write("\n 1s Complement : " + ones);
}
}
public static void Main(String[] args)
{
// 50 (1001) => (0110) (6)
// 50 (110010) => (001101) (13)
// (32) = 100000 => 11111 (31)
}
}``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````
``````<?php
/*
Php program
Find 1s complement of an Integer
*/
class Complement
{
// Returns the number of bits in given num
public	function totalBits(\$num)
{
return (int)(intval(log(\$num) / log(2))) + 1;
}
// Find one's complement of given number
public	function onesComplement(\$num)
{
// Display given number
echo "\n Number : ". \$num;
if (\$num < 0)
{
echo "\n 1s Complement : ". (~\$num);
}
else
{
// Calculate 1s complement
\$ones = ((1 << \$this->totalBits(\$num)) - 1) ^ \$num;
// Display calculated result
echo "\n 1s Complement : ". \$ones;
}
}
}

function main()
{
// 50 (1001) => (0110) (6)
// 50 (110010) => (001101) (13)
// (32) = 100000 => 11111 (31)
}
main();``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````
``````/*
Node Js program
Find 1s complement of an Integer
*/
class Complement
{
// Returns the number of bits in given num
totalBits(num)
{
return parseInt((parseInt(Math.log(num) / Math.log(2)))) + 1;
}
// Find one's complement of given number
onesComplement(num)
{
// Display given number
process.stdout.write("\n Number : " + num);
if (num < 0)
{
process.stdout.write("\n 1s Complement : " + (~num));
}
else
{
// Calculate 1s complement
var ones = ((1 << this.totalBits(num)) - 1) ^ num;
// Display calculated result
process.stdout.write("\n 1s Complement : " + ones);
}
}
}

function main()
{
// 50 (1001) => (0110) (6)
// 50 (110010) => (001101) (13)
// (32) = 100000 => 11111 (31)
}
main();``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````
``````import math
#   Python 3 program
#   Find 1s complement of an Integer

class Complement :
#  Returns the number of bits in given num
def totalBits(self, num) :
return int((int(math.log(num) / math.log(2)))) + 1

#  Find one's complement of given number
def onesComplement(self, num) :
#  Display given number
print("\n Number : ", num, end = "")
if (num < 0) :
print("\n 1s Complement : ", (~num), end = "")
else :
#  Calculate 1s complement
ones = ((1 << self.totalBits(num)) - 1) ^ num
#  Display calculated result
print("\n 1s Complement : ", ones, end = "")

def main() :
#  50 (1001) => (0110) (6)
#  50 (110010) => (001101) (13)
#  (32) = 100000 => 11111 (31)

if __name__ == "__main__": main()``````

#### Output

`````` Number :  9
1s Complement :  6
Number :  50
1s Complement :  13
Number :  32
1s Complement :  31``````
``````#   Ruby program
#   Find 1s complement of an Integer

class Complement
#  Returns the number of bits in given num
def totalBits(num)
return ((Math.log(num) / Math.log(2))).to_i + 1
end

#  Find one's complement of given number
def onesComplement(num)
#  Display given number
print("\n Number : ", num)
if (num < 0)
print("\n 1s Complement : ", (~num))
else
#  Calculate 1s complement
ones = ((1 << self.totalBits(num)) - 1) ^ num
#  Display calculated result
print("\n 1s Complement : ", ones)
end

end

end

def main()
#  50 (1001) => (0110) (6)
#  50 (110010) => (001101) (13)
#  (32) = 100000 => 11111 (31)
end

main()``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````
``````/*
Scala program
Find 1s complement of an Integer
*/
class Complement
{
// Returns the number of bits in given num
def totalBits(num: Int): Int = {
return (((Math.log(num) / Math.log(2)).toInt)).toInt + 1;
}
// Find one's complement of given number
def onesComplement(num: Int): Unit = {
// Display given number
print("\n Number : " + num);
if (num < 0)
{
print("\n 1s Complement : " + (~num));
}
else
{
// Calculate 1s complement
var ones: Int = ((1 << this.totalBits(num)) - 1) ^ num;
// Display calculated result
print("\n 1s Complement : " + ones);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Complement = new Complement();
// 50 (1001) => (0110) (6)
// 50 (110010) => (001101) (13)
// (32) = 100000 => 11111 (31)
}
}``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````
``````import Foundation
/*
Swift 4 program
Find 1s complement of an Integer
*/
class Complement
{
// Returns the number of bits in given num
func totalBits(_ num: Int)->Int
{
return Int(log(Double(num)) / log(Double(2))) + 1;
}
// Find one"s complement of given number
func onesComplement(_ num: Int)
{
// Display given number
print("\n Number : ", num, terminator: "");
if (num < 0)
{
print("\n 1s Complement : ", (~num), terminator: "");
}
else
{
// Calculate 1s complement
let ones: Int = ((1 << self.totalBits(num)) - 1) ^ num;
// Display calculated result
print("\n 1s Complement : ", ones, terminator: "");
}
}
}
func main()
{
// 50 (1001) => (0110) (6)
// 50 (110010) => (001101) (13)
// (32) = 100000 => 11111 (31)
}
main();``````

#### Output

`````` Number :  9
1s Complement :  6
Number :  50
1s Complement :  13
Number :  32
1s Complement :  31``````
``````/*
Kotlin program
Find 1s complement of an Integer
*/
class Complement
{
// Returns the number of bits in given num
fun totalBits(num: Int): Int
{
return (Math.log(num.toDouble()) / Math.log(2.0)).toInt() + 1;
}
// Find one's complement of given number
fun onesComplement(num: Int): Unit
{
// Display given number
print("\n Number : " + num);
if (num < 0)
{
print("\n 1s Complement : " + (num.inv()));
}
else
{
// Calculate 1s complement
var ones: Int = ((1 shl this.totalBits(num)) - 1) xor num;
// Display calculated result
print("\n 1s Complement : " + ones);
}
}
}
fun main(args: Array < String > ): Unit
{
// 50 (1001) => (0110) (6)
// 50 (110010) => (001101) (13)
// (32) = 100000 => 11111 (31)
}``````

#### Output

`````` Number : 9
1s Complement : 6
Number : 50
1s Complement : 13
Number : 32
1s Complement : 31``````

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.