{"id":20370,"date":"2021-09-11T15:37:55","date_gmt":"2021-09-11T10:07:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=20370"},"modified":"2021-11-22T18:36:22","modified_gmt":"2021-11-22T13:06:22","slug":"python-program-to-get-nth-bit-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-get-nth-bit-of-a-number\/","title":{"rendered":"Python Program to Get nth Bit of a Number"},"content":{"rendered":"
In the previous article, we have discussed Program to Find All Non Repeated Characters in a String<\/a><\/p>\n Given a number and the bit position, the task is to get the bit that is present at that position (in the binary representation of a number).<\/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 get the bit that is present at that position (in the binary representation of a number) in python:<\/p>\n Approach:<\/strong><\/p>\n Note:<\/strong> ‘0’ indexing .Give the bit position in range(0-31).<\/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 wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples<\/a> existing and learn the approach used.<\/p>\n In the previous article, we have discussed Program to Find All Non Repeated Characters in a String Given a number and the bit position, the task is to get the bit that is present at that position (in the binary representation of a number). Bitwise & Operator: If both bits are 1, sets each bit …<\/p>\nGiven Number = 2\r\nBit position(in range 0-31)= 0<\/pre>\n
The bit present at the given position{ 0 } for a given number is 0<\/pre>\n
Given Number = 14\r\nBit position(in range 0-31)= 3<\/pre>\n
The bit present at the given position{ 3 } for a given number is 1<\/pre>\n
Program to Get nth Bit of a Number 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 = 2\r\n# Give the bit position that you need to get the bit value at that position as static input\r\n# and store it in another variable.\r\nbitpositin = 0\r\n# Apply the left shift operator to 1 and the above-given bit position and\r\n# store it in another variable.\r\nnumbr_bit = (1 << bitpositin)\r\n# Apply bitwise\u00a0& operation for the given number and the above result and\r\n# store it in another variable say bit_val.\r\nbit_val = gvn_numb & numbr_bit\r\n# Check if the above result bit_val is greater than 0 using the if conditional statement.\r\nif (bit_val > 0):\r\n # If the statement is true, then print \"The bit present at the given position is 1\".\r\n print(\r\n \"The bit present at the given position{\", bitpositin, \"} for a given number is 1\")\r\nelse:\r\n # Else print \"The bit present at the given position is 0\".\r\n print(\r\n \"The bit present at the given position{\", bitpositin, \"} for a given number is 0\")\r\n<\/pre>\n
The bit present at the given position{ 0 } for a given number is 0<\/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# Give the bit position that you need to get the bit value at that position as user input\r\n# using the int(input()) function and store it in another variable.\r\nbitpositin = int(input(\"Enter some random number = \"))\r\n# Apply the left shift operator to 1 and the above-given bit position and\r\n# store it in another variable.\r\nnumbr_bit = (1 << bitpositin)\r\n# Apply bitwise\u00a0& operation for the given number and the above result and\r\n# store it in another variable say bit_val.\r\nbit_val = gvn_numb & numbr_bit\r\n# Check if the above result bit_val is greater than 0 using the if conditional statement.\r\nif (bit_val > 0):\r\n # If the statement is true, then print \"The bit present at the given position is 1\".\r\n print(\r\n \"The bit present at the given position{\", bitpositin, \"} for a given number is 1\")\r\nelse:\r\n # Else print \"The bit present at the given position is 0\".\r\n print(\r\n \"The bit present at the given position{\", bitpositin, \"} for a given number is 0\")\r\n<\/pre>\n
Enter some random number = 14\r\nEnter some random number = 3\r\nThe bit present at the given position{ 3 } for a given number is 1<\/pre>\n
\n