Skip to main content

Find 2s complement of a binary string

Here given code implementation process.

/*
  Java program
  Find 2s complement of a binary string
*/
public class Complement
{
	// Find 2s complement
	public void find2s(String text)
	{
		int n = text.length();
		int i = 0;
		boolean status = true;
		String one_s = "";
		String two_s = "";
		int add_one = 1;
		// Find 1s complement
		while (i < n)
		{
			if (text.charAt(i) == '1')
			{
				one_s = one_s + "0";
			}
			else
			{
				one_s = one_s + "1";
			}
			i++;
		}
		i = n-1;
		// Add one
		while (i>= 0)
		{
			if (one_s.charAt(i) == '0')
			{
				if (add_one == 1)
				{
					two_s = "1" + two_s;
                  	add_one = 0;
				}
              else{
                two_s = "0" + two_s;
              }
			}
			else
			{
				if (add_one == 1)
				{
					two_s = "0" + two_s;
				}
				else
				{
					two_s = "1" + two_s;
				}
			}
			i--;
		}
		if (add_one == 1)
		{
			two_s = "1" + two_s;
		}
		// Display given binary text
		System.out.print("\n Binary Text : " + text);
		// Show 1s Complement
		System.out.print("\n 1s Complement : " + one_s);
		// Display calculated result
		System.out.print("\n 2s Complement : " + two_s);
	}
	public static void main(String[] args)
	{
		Complement task = new Complement();
		
		task.find2s("1010100");
      	task.find2s("1000");
	}
}

Output

 Binary Text : 1010100
 1s Complement : 0101011
 2s Complement : 0101100
 Binary Text : 1000
 1s Complement : 0111
 2s Complement : 1000
// Include header file
#include <iostream>
#include <string>
using namespace std;

/*
  C++ program
  Find 2s complement of a binary string
*/
class Complement
{
	public:
		// Find 2s complement
		void find2s(string text)
		{
			int n = text.length();
			int i = 0;
			bool status = true;
			string one_s = "";
			string two_s = "";
			int add_one = 1;
			// Find 1s complement
			while (i < n)
			{
				if (text[i] == '1')
				{
					one_s = one_s + "0";
				}
				else
				{
					one_s = one_s + "1";
				}
				i++;
			}
			i = n - 1;
			// Add one
			while (i >= 0)
			{
				if (one_s[i] == '0')
				{
					if (add_one == 1)
					{
						two_s = "1" + two_s;
						add_one = 0;
					}
					else
					{
						two_s = "0" + two_s;
					}
				}
				else
				{
					if (add_one == 1)
					{
						two_s = "0" + two_s;
					}
					else
					{
						two_s = "1" + two_s;
					}
				}
				i--;
			}
			if (add_one == 1)
			{
				two_s = "1" + two_s;
			}
			// Display given binary text
			cout << "\n Binary Text : " << text;
			// Show 1s Complement
			cout << "\n 1s Complement : " << one_s;
			// Display calculated result
			cout << "\n 2s Complement : " << two_s;
		}
};
int main()
{
	Complement task = Complement();
	task.find2s("1010100");
	task.find2s("1000");
	return 0;
}

Output

 Binary Text : 1010100
 1s Complement : 0101011
 2s Complement : 0101100
 Binary Text : 1000
 1s Complement : 0111
 2s Complement : 1000
// Include namespace system
using System;
/*
  C# program
  Find 2s complement of a binary string
*/
public class Complement
{
	// Find 2s complement
	public void find2s(String text)
	{
		int n = text.Length;
		int i = 0;
		String one_s = "";
		String two_s = "";
		int add_one = 1;
		// Find 1s complement
		while (i < n)
		{
			if (text[i] == '1')
			{
				one_s = one_s + "0";
			}
			else
			{
				one_s = one_s + "1";
			}
			i++;
		}
		i = n - 1;
		// Add one
		while (i >= 0)
		{
			if (one_s[i] == '0')
			{
				if (add_one == 1)
				{
					two_s = "1" + two_s;
					add_one = 0;
				}
				else
				{
					two_s = "0" + two_s;
				}
			}
			else
			{
				if (add_one == 1)
				{
					two_s = "0" + two_s;
				}
				else
				{
					two_s = "1" + two_s;
				}
			}
			i--;
		}
		if (add_one == 1)
		{
			two_s = "1" + two_s;
		}
		// Display given binary text
		Console.Write("\n Binary Text   : " + text);
		// Show 1s Complement
		Console.Write("\n 1s Complement : " + one_s);
		// Display calculated result
		Console.Write("\n 2s Complement : " + two_s);
	}
	public static void Main(String[] args)
	{
		Complement task = new Complement();
		task.find2s("1010100");
		task.find2s("1000");
	}
}

Output

 Binary Text   : 1010100
 1s Complement : 0101011
 2s Complement : 0101100
 Binary Text   : 1000
 1s Complement : 0111
 2s Complement : 1000
<?php
/*
  Php program
  Find 2s complement of a binary string
*/
class Complement
{
	// Find 2s complement
	public	function find2s($text)
	{
		$n = strlen($text);
		$i = 0;
		$one_s = "";
		$two_s = "";
		$add_one = 1;
		// Find 1s complement
		while ($i < $n)
		{
			if ($text[$i] == '1')
			{
				$one_s = $one_s ."0";
			}
			else
			{
				$one_s = $one_s ."1";
			}
			$i++;
		}
		$i = $n - 1;
		// Add one
		while ($i >= 0)
		{
			if ($one_s[$i] == '0')
			{
				if ($add_one == 1)
				{
					$two_s = "1". $two_s;
					$add_one = 0;
				}
				else
				{
					$two_s = "0". $two_s;
				}
			}
			else
			{
				if ($add_one == 1)
				{
					$two_s = "0". $two_s;
				}
				else
				{
					$two_s = "1". $two_s;
				}
			}
			$i--;
		}
		if ($add_one == 1)
		{
			$two_s = "1". $two_s;
		}
		// Display given binary text
		echo "\n Binary Text   : ". $text;
		// Show 1s Complement
		echo "\n 1s Complement : ". $one_s;
		// Display calculated result
		echo "\n 2s Complement : ". $two_s;
	}
}

function main()
{
	$task = new Complement();
	$task->find2s("1010100");
	$task->find2s("1000");
}
main();

Output

 Binary Text   : 1010100
 1s Complement : 0101011
 2s Complement : 0101100
 Binary Text   : 1000
 1s Complement : 0111
 2s Complement : 1000
/*
  Node Js program
  Find 2s complement of a binary string
*/
class Complement
{
	// Find 2s complement
	find2s(text)
	{
		var n = text.length;
		var i = 0;
		var one_s = "";
		var two_s = "";
		var add_one = 1;
		// Find 1s complement
		while (i < n)
		{
			if (text.charAt(i) == '1')
			{
				one_s = one_s + "0";
			}
			else
			{
				one_s = one_s + "1";
			}
			i++;
		}
		i = n - 1;
		// Add one
		while (i >= 0)
		{
			if (one_s.charAt(i) == '0')
			{
				if (add_one == 1)
				{
					two_s = "1" + two_s;
					add_one = 0;
				}
				else
				{
					two_s = "0" + two_s;
				}
			}
			else
			{
				if (add_one == 1)
				{
					two_s = "0" + two_s;
				}
				else
				{
					two_s = "1" + two_s;
				}
			}
			i--;
		}
		if (add_one == 1)
		{
			two_s = "1" + two_s;
		}
		// Display given binary text
		process.stdout.write("\n Binary Text   : " + text);
		// Show 1s Complement
		process.stdout.write("\n 1s Complement : " + one_s);
		// Display calculated result
		process.stdout.write("\n 2s Complement : " + two_s);
	}
}

function main()
{
	var task = new Complement();
	task.find2s("1010100");
	task.find2s("1000");
}
main();

Output

 Binary Text   : 1010100
 1s Complement : 0101011
 2s Complement : 0101100
 Binary Text   : 1000
 1s Complement : 0111
 2s Complement : 1000
#   Python 3 program
#   Find 2s complement of a binary string

class Complement :
	#  Find 2s complement
	def find2s(self, text) :
		n = len(text)
		i = 0
		one_s = ""
		two_s = ""
		add_one = 1
		#  Find 1s complement
		while (i < n) :
			if (text[i] == '1') :
				one_s = one_s + "0"
			else :
				one_s = one_s + "1"
			
			i += 1
		
		i = n - 1
		#  Add one
		while (i >= 0) :
			if (one_s[i] == '0') :
				if (add_one == 1) :
					two_s = "1" + two_s
					add_one = 0
				else :
					two_s = "0" + two_s
				
			else :
				if (add_one == 1) :
					two_s = "0" + two_s
				else :
					two_s = "1" + two_s
				
			
			i -= 1
		
		if (add_one == 1) :
			two_s = "1" + two_s
		
		#  Display given binary text
		print("\n Binary Text   : ", text, end = "")
		#  Show 1s Complement
		print("\n 1s Complement : ", one_s, end = "")
		#  Display calculated result
		print("\n 2s Complement : ", two_s, end = "")
	

def main() :
	task = Complement()
	task.find2s("1010100")
	task.find2s("1000")

if __name__ == "__main__": main()

Output

 Binary Text   :  1010100
 1s Complement :  0101011
 2s Complement :  0101100
 Binary Text   :  1000
 1s Complement :  0111
 2s Complement :  1000
#   Ruby program
#   Find 2s complement of a binary string

class Complement 
	#  Find 2s complement
	def find2s(text) 
		n = text.length
		i = 0
		one_s = ""
		two_s = ""
		add_one = 1
		#  Find 1s complement
		while (i < n) 
			if (text[i] == '1') 
				one_s = one_s +"0"
			else 
				one_s = one_s +"1"
			end

			i += 1
		end

		i = n - 1
		#  Add one
		while (i >= 0) 
			if (one_s[i] == '0') 
				if (add_one == 1) 
					two_s = "1"+ two_s
					add_one = 0
				else 
					two_s = "0"+ two_s
				end

			else 
				if (add_one == 1) 
					two_s = "0"+ two_s
				else 
					two_s = "1"+ two_s
				end

			end

			i -= 1
		end

		if (add_one == 1) 
			two_s = "1"+ two_s
		end

		#  Display given binary text
		print("\n Binary Text   : ", text)
		#  Show 1s Complement
		print("\n 1s Complement : ", one_s)
		#  Display calculated result
		print("\n 2s Complement : ", two_s)
	end

end

def main() 
	task = Complement.new()
	task.find2s("1010100")
	task.find2s("1000")
end

main()

Output

 Binary Text   : 1010100
 1s Complement : 0101011
 2s Complement : 0101100
 Binary Text   : 1000
 1s Complement : 0111
 2s Complement : 1000
/*
  Scala program
  Find 2s complement of a binary string
*/
class Complement
{
	// Find 2s complement
	def find2s(text: String): Unit = {
		var n: Int = text.length();
		var i: Int = 0;
		var one_s: String = "";
		var two_s: String = "";
		var add_one: Int = 1;
		// Find 1s complement
		while (i < n)
		{
			if (text.charAt(i) == '1')
			{
				one_s = one_s + "0";
			}
			else
			{
				one_s = one_s + "1";
			}
			i += 1;
		}
		i = n - 1;
		// Add one
		while (i >= 0)
		{
			if (one_s.charAt(i) == '0')
			{
				if (add_one == 1)
				{
					two_s = "1" + two_s;
					add_one = 0;
				}
				else
				{
					two_s = "0" + two_s;
				}
			}
			else
			{
				if (add_one == 1)
				{
					two_s = "0" + two_s;
				}
				else
				{
					two_s = "1" + two_s;
				}
			}
			i -= 1;
		}
		if (add_one == 1)
		{
			two_s = "1" + two_s;
		}
		// Display given binary text
		print("\n Binary Text   : " + text);
		// Show 1s Complement
		print("\n 1s Complement : " + one_s);
		// Display calculated result
		print("\n 2s Complement : " + two_s);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Complement = new Complement();
		task.find2s("1010100");
		task.find2s("1000");
	}
}

Output

 Binary Text   : 1010100
 1s Complement : 0101011
 2s Complement : 0101100
 Binary Text   : 1000
 1s Complement : 0111
 2s Complement : 1000
/*
  Swift 4 program
  Find 2s complement of a binary string
*/
class Complement
{
	// Find 2s complement
	func find2s(_ t: String)
	{
      	let text = Array(t);
		let n: Int = t.count;
		var i: Int = 0;
		var one_s: String = "";
		var two_s: String = "";
		var add_one: Int = 1;
		// Find 1s complement
		while (i < n)
		{
			if (text[i] == "1")
			{
				one_s = one_s + "0";
			}
			else
			{
				one_s = one_s + "1";
			}
			i += 1;
		}
		i = n - 1;
		// Add one
		while (i >= 0)
		{
			if (Array(one_s)[i] == "0")
			{
				if (add_one == 1)
				{
					two_s = "1" + two_s;
					add_one = 0;
				}
				else
				{
					two_s = "0" + two_s;
				}
			}
			else
			{
				if (add_one == 1)
				{
					two_s = "0" + two_s;
				}
				else
				{
					two_s = "1" + two_s;
				}
			}
			i -= 1;
		}
		if (add_one == 1)
		{
			two_s = "1" + two_s;
		}
		// Display given binary text
		print("\n Binary Text   : ", t, terminator: "");
		// Show 1s Complement
		print("\n 1s Complement : ", one_s, terminator: "");
		// Display calculated result
		print("\n 2s Complement : ", two_s, terminator: "");
	}
}
func main()
{
	let task: Complement = Complement();
	task.find2s("1010100");
	task.find2s("1000");
}
main();

Output

 Binary Text   :  1010100
 1s Complement :  0101011
 2s Complement :  0101100
 Binary Text   :  1000
 1s Complement :  0111
 2s Complement :  1000
/*
  Kotlin program
  Find 2s complement of a binary string
*/
class Complement
{
	// Find 2s complement
	fun find2s(text: String): Unit
	{
		var n: Int = text.length;
		var i: Int = 0;
		var one_s: String = "";
		var two_s: String = "";
		var add_one: Int = 1;
		// Find 1s complement
		while (i < n)
		{
			if (text.get(i) == '1')
			{
				one_s = one_s + "0";
			}
			else
			{
				one_s = one_s + "1";
			}
			i += 1;
		}
		i = n - 1;
		// Add one
		while (i >= 0)
		{
			if (one_s.get(i) == '0')
			{
				if (add_one == 1)
				{
					two_s = "1" + two_s;
					add_one = 0;
				}
				else
				{
					two_s = "0" + two_s;
				}
			}
			else
			{
				if (add_one == 1)
				{
					two_s = "0" + two_s;
				}
				else
				{
					two_s = "1" + two_s;
				}
			}
			i -= 1;
		}
		if (add_one == 1)
		{
			two_s = "1" + two_s;
		}
		// Display given binary text
		print("\n Binary Text   : " + text);
		// Show 1s Complement
		print("\n 1s Complement : " + one_s);
		// Display calculated result
		print("\n 2s Complement : " + two_s);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Complement = Complement();
	task.find2s("1010100");
	task.find2s("1000");
}

Output

 Binary Text   : 1010100
 1s Complement : 0101011
 2s Complement : 0101100
 Binary Text   : 1000
 1s Complement : 0111
 2s Complement : 1000




Comment

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