{"id":20483,"date":"2021-09-13T14:51:27","date_gmt":"2021-09-13T09:21:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=20483"},"modified":"2021-11-22T18:36:20","modified_gmt":"2021-11-22T13:06:20","slug":"python-program-to-find-xor-of-two-numbers-without-using-xor-operator","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-xor-of-two-numbers-without-using-xor-operator\/","title":{"rendered":"Python Program to Find XOR of Two Numbers Without Using XOR operator"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Position of Rightmost Set Bit<\/a><\/p>\n Give two numbers the task is to find the XOR result of the given two numbers in Python.<\/p>\n Bitwise & Operator:<\/strong><\/p>\n If both bits are 1, sets each bit to 1.<\/p>\n Bitwise or (|) operator:<\/strong><\/p>\n If one of two bits is 1, sets each bit to 1.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the XOR result of the given two numbers in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners<\/a> as our expert team has designed them from scratch.<\/p>\n In the previous article, we have discussed Python Program to Find Position of Rightmost Set Bit Give two numbers the task is to find the XOR result of the given two numbers in Python. Bitwise & Operator: If both bits are 1, sets each bit to 1. Bitwise or (|) operator: If one of two …<\/p>\nGiven First Number = 4\r\nGiven Second Number = 2<\/pre>\n
The XOR result of the given first and second numbers{ 4 , 2 } = 6<\/pre>\n
Given First Number = 7\r\nGiven Second Number = 9<\/pre>\n
The XOR result of the given first and second numbers{ 7 , 9 } = 14<\/pre>\n
Program to Find XOR of Two Numbers Without Using XOR operator in Python<\/h2>\n
\n
Method #1: Using Bitwise And(&), Or(|) Operators (Static Input)<\/h3>\n
\n
# Create a function XOR_result() which accepts the given two numbers as the argument and\r\n# returns the XOR result of the given first and second numbers.\r\n\r\n\r\ndef XOR_result(gvnfst_numb, gvnscnd_numb):\r\n # Inside the XOR_result() function.\r\n # Calculate and the value of (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb)\r\n # which gives the XOR result of the given first and second numbers and store it in a\r\n # variable say xor_rslt.\r\n\r\n xor_rslt = (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb)\r\n # Return the value of xor_rslt (Which is the XOR result of the given first and\r\n # second numbers).\r\n return(xor_rslt)\r\n\r\n\r\n# Inside the main code.\r\n# Give the first number as static input and store it in a variable.\r\ngvnfst_numb = 4\r\n# Give the second number as static input and store it in another variable.\r\ngvnscnd_numb = 2\r\n# Pass the given first and second numbers as the arguments to XOR_result()\u00a0function and\r\n# store the result in a variable (xorreslt_val).\r\nxorreslt_val = XOR_result(gvnfst_numb, gvnscnd_numb)\r\n# Print the xorreslt_val value.\r\nprint(\"The XOR result of the given first and second numbers{\",\r\n gvnfst_numb, \",\", gvnscnd_numb, \"} =\", xorreslt_val)\r\n<\/pre>\n
The XOR result of the given first and second numbers{ 4 , 2 } = 6<\/pre>\n
Method #2: Using Bitwise And(&), Or(|) Operators (User Input)<\/h3>\n
\n
# Create a function XOR_result() which accepts the given two numbers as the argument and\r\n# returns the XOR result of the given first and second numbers.\r\n\r\n\r\ndef XOR_result(gvnfst_numb, gvnscnd_numb):\r\n # Inside the XOR_result() function.\r\n # Calculate and the value of (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb)\r\n # which gives the XOR result of the given first and second numbers and store it in a\r\n # variable say xor_rslt.\r\n\r\n xor_rslt = (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb)\r\n # Return the value of xor_rslt (Which is the XOR result of the given first and\r\n # second numbers).\r\n return(xor_rslt)\r\n\r\n\r\n# Inside the main code.\r\n# Give the first number as user input using the int(input()) function and store it in a variable.\r\ngvnfst_numb = int(input('Enter some random number = '))\r\n# Give the second number using the int(input()) function and store it in another variable.\r\ngvnscnd_numb = int(input('Enter some random number = '))\r\n# Pass the given first and second numbers as the arguments to XOR_result()\u00a0function and\r\n# store the result in a variable (xorreslt_val).\r\nxorreslt_val = XOR_result(gvnfst_numb, gvnscnd_numb)\r\n# Print the xorreslt_val value.\r\nprint(\"The XOR result of the given first and second numbers{\",\r\n gvnfst_numb, \",\", gvnscnd_numb, \"} =\", xorreslt_val)\r\n<\/pre>\n
Enter some random number = 7\r\nEnter some random number = 9\r\nThe XOR result of the given first and second numbers{ 7 , 9 } = 14<\/pre>\n
\n