{"id":20382,"date":"2021-09-11T15:37:59","date_gmt":"2021-09-11T10:07:59","guid":{"rendered":"https:\/\/python-programs.com\/?p=20382"},"modified":"2021-11-22T18:36:22","modified_gmt":"2021-11-22T13:06:22","slug":"python-program-to-check-even-or-odd-using-bitwise-operator","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-even-or-odd-using-bitwise-operator\/","title":{"rendered":"Python Program to Check Even or Odd Using Bitwise Operator"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Set nth Bit of a Number<\/a><\/p>\n Given a Number and the task is to check if the given number is even or odd using the bitwise operator.<\/p>\n Bitwise & Operator:<\/strong><\/p>\n If both bits are 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 check if the given number is even or odd using the bitwise operator 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 Grab the opportunity and utilize the Python Program Code Examples<\/a> over here to prepare basic and advanced topics too with ease and clear all your doubts.<\/p>\n In the previous article, we have discussed Python Program to Set nth Bit of a Number Given a Number and the task is to check if the given number is even or odd using the bitwise operator. Bitwise & Operator: If both bits are 1, sets each bit to 1. Examples: Example1: Input: Given Number …<\/p>\nGiven Number = 251<\/pre>\n
The Number given is an Odd Number<\/pre>\n
Given Number = 54<\/pre>\n
The Number given is an Even Number<\/pre>\n
Program to Check Even or Odd Using Bitwise Operator in Python:<\/h2>\n
\n
Method #1: Using Bitwise <\/strong>&(and) Operator (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 251\r\n# Apply bitwise\u00a0& operation for the given number and 1 and store it in another variable\r\n# say evn_or_od.\r\nevn_or_od = gvn_numb & 1\r\n# Pass the above result to the if conditional statement.\r\nif (evn_or_od):\r\n # If the statement is true, then print \"The Number given is an Odd Number\".\r\n print(\"The Number given is an Odd Number\")\r\nelse:\r\n # Else print \"The Number given is an Even Number\".\r\n print(\"The Number given is an Even Number\")\r\n<\/pre>\n
The Number given is an Odd Number<\/pre>\n
Method #2: Using Bitwise <\/strong>&(and) Operator (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and \r\n# store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Apply bitwise\u00a0& operation for the given number and 1 and store it in another variable\r\n# say evn_or_od.\r\nevn_or_od = gvn_numb & 1\r\n# Pass the above result to the if conditional statement.\r\nif (evn_or_od):\r\n # If the statement is true, then print \"The Number given is an Odd Number\".\r\n print(\"The Number given is an Odd Number\")\r\nelse:\r\n # Else print \"The Number given is an Even Number\".\r\n print(\"The Number given is an Even Number\")\r\n\r\n<\/pre>\n
Enter some random number = 32\r\nThe Number given is an Even Number<\/pre>\n
\n