{"id":20570,"date":"2021-09-15T19:49:17","date_gmt":"2021-09-15T14:19:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=20570"},"modified":"2021-11-22T18:36:19","modified_gmt":"2021-11-22T13:06:19","slug":"python-program-print-bitwise-and-set-of-a-number-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-print-bitwise-and-set-of-a-number-n\/","title":{"rendered":"Python Program Print Bitwise AND set of a Number N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Toggle Bits of a Number Except First and Last bits<\/a><\/p>\n Given a number and the task is to print all the bitwise AND set of a given number.<\/p>\n For some number I the bitwise AND set of a number N is all feasible numbers x smaller than or equal to N such that N & I equals x.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print all the bitwise AND set of a given number 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 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 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 <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program to Toggle Bits of a Number Except First and Last bits Given a number and the task is to print all the bitwise AND set of a given number. For some number I the bitwise AND set of a number N is all feasible numbers x …<\/p>\nGiven Number = 6<\/pre>\n
The all bitwise AND set of a given number{ 6 } : \r\n0 2 4 6<\/pre>\n
Iterating from 0 to 6 so\r\n0 & 6 = 0\r\n1 & 6 = 0 \r\n2 & 6 = 2\r\n3 & 6 = 2\r\n4 & 6 = 4\r\n5 & 6 = 4\r\n6 & 6 = 6\r\nHence the all bitwise AND set of a given number = 0 2 4 6 (removing duplicates)<\/pre>\n
Given Number = 3<\/pre>\n
The all bitwise AND set of a given number{ 3 } : \r\n0 1 2 3<\/pre>\n
Program Print Bitwise AND set of a Number N in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 6\r\n# Loop till the given number using the for loop.\r\nprint(\"The all bitwise AND set of a given number{\", gvn_numb, \"} : \")\r\nfor itr in range(gvn_numb + 1):\r\n # Apply AND operation for the given number and the iterator value and store it in\r\n # another variable.\r\n p = gvn_numb & itr\r\n # Check if the above result is equal to the iterator value using the if conditional\r\n # statement.\r\n if (p == itr):\r\n # If the statement is true, then print the iterator value separated by spaces.\r\n print(itr, \" \", end=\"\")\r\n<\/pre>\n
The all bitwise AND set of a given number{ 6 } : \r\n0 2 4 6<\/pre>\n
Method #2: Using For loop (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# Loop till the given number using the for loop.\r\nprint(\"The all bitwise AND set of a given number{\", gvn_numb, \"} : \")\r\nfor itr in range(gvn_numb + 1):\r\n # Apply AND operation for the given number and the iterator value and store it in\r\n # another variable.\r\n p = gvn_numb & itr\r\n # Check if the above result is equal to the iterator value using the if conditional\r\n # statement.\r\n if (p == itr):\r\n # If the statement is true, then print the iterator value separated by spaces.\r\n print(itr, \" \", end=\"\")\r\n<\/pre>\n
Enter some random number = 3\r\nThe all bitwise AND set of a given number{ 3 } : \r\n0 1 2 3<\/pre>\n
Method #3: Using While Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 10\r\n# Assign the given number to the variable say tempry.\r\ntempry = gvn_numb\r\nprint(\"The all bitwise AND set of a given number{\", gvn_numb, \"} : \")\r\n# Loop till the above variable tempry is not equal to 0 using the while loop.\r\nwhile(tempry != 0):\r\n # Inside the Loop, Print the value of tempry separated by spaces.\r\n print(tempry, end=\" \")\r\n # Apply AND operation for the tempry -1 and given number and store it in the same variable\r\n # tempry.\r\n tempry = (tempry - 1) & gvn_numb\r\n # Exit the loop.\r\n# Print 0.\r\nprint(\"0\")\r\n<\/pre>\n
The all bitwise AND set of a given number{ 10 } : \r\n10 8 2 0<\/pre>\n
Method #4: Using While loop (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Assign the given number to the variable say tempry.\r\ntempry = gvn_numb\r\nprint(\"The all bitwise AND set of a given number{\", gvn_numb, \"} : \")\r\n# Loop till the above variable tempry is not equal to 0 using the while loop.\r\nwhile(tempry != 0):\r\n # Inside the Loop, Print the value of tempry separated by spaces.\r\n print(tempry, end=\" \")\r\n # Apply AND operation for the tempry -1 and given number and store it in the same variable\r\n # tempry.\r\n tempry = (tempry - 1) & gvn_numb\r\n # Exit the loop.\r\n# Print 0.\r\nprint(\"0\")\r\n<\/pre>\n
Enter some random number = 7\r\nThe all bitwise AND set of a given number{ 7 } : \r\n7 6 5 4 3 2 1 0<\/pre>\n
\n