{"id":8480,"date":"2021-08-16T14:13:33","date_gmt":"2021-08-16T08:43:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=8480"},"modified":"2021-11-22T18:39:25","modified_gmt":"2021-11-22T13:09:25","slug":"program-to-clear-the-rightmost-set-bit-of-a-number-in-cpp-and-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/program-to-clear-the-rightmost-set-bit-of-a-number-in-cpp-and-python\/","title":{"rendered":"Program to Clear the Rightmost Set Bit of a Number in C++ and Python"},"content":{"rendered":"
In the previous article, we have discussed about C++ Program to Check if it is Sparse Matrix or Not<\/a>. Let us learn Program to Clear the Rightmost Set Bit of a Number in C++ Program and Python.<\/p>\n Binary Representation of a Number:<\/strong><\/p>\n Binary is a base-2 number system in which a number is represented by two states: 0 and 1. We can also refer to it as a true and false state. A binary number is constructed in the same way that a decimal number is constructed.<\/p>\n Examples:<\/strong><\/p>\n Examples1:<\/strong><\/p>\n Input:<\/strong><\/p>\n given number=19<\/p>\n Output:<\/strong><\/p>\n Examples2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Examples3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to clear the rightmost set Bit of a Number in C++ and Python some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation of above approach:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In the previous article, we have discussed about C++ Program to Check if it is Sparse Matrix or Not. Let us learn Program to Clear the Rightmost Set Bit of a Number in C++ Program and Python. Binary Representation of a Number: Binary is a base-2 number system in which a number is represented by …<\/p>\nThe given number before removing right most set bit : \r\n19\r\nThe given number after removing right most set bit : \r\n18<\/pre>\n
given number =18<\/pre>\n
The given number before removing right most set bit : \r\n18\r\nThe given number after removing right most set bit : \r\n16<\/pre>\n
given number=512<\/pre>\n
The given number before removing right most set bit : \r\n512\r\nThe given number after removing right most set bit : \r\n0<\/pre>\n
Program to Clear the Rightmost Set Bit of a Number in C++ and Python<\/h2>\n
\n
Method #1: Using Bitwise Operators in C++<\/h3>\n
\n
#include <bits\/stdc++.h>\r\nusing namespace std;\r\n\/\/ function which removes the right most set bit in the\r\n\/\/ given number\r\nint clearRight(int numb)\r\n{\r\n \/\/ clearing the right most set bit from\r\n \/\/ the given number and store it in the result\r\n int reslt = (numb) & (numb - 1);\r\n \/\/ returing the calculated result\r\n return reslt;\r\n}\r\n\r\n\/\/ main function\r\nint main()\r\n{\r\n \/\/ given number\r\n int numb = 19;\r\n\r\n cout << \"The given number before removing right most \"\r\n \"set bit : \"\r\n << numb << endl;\r\n \/\/ passing the given number to clearRight function\r\n \/\/ to remove the clear the rightmost setbit\r\n cout << \"The given number after removing right most \"\r\n \"set bit : \"\r\n << clearRight(numb) << endl;\r\n return 0;\r\n}<\/pre>\n
The given number before removing right most set bit : 19\r\nThe given number after removing right most set bit : 18<\/pre>\n
Method #2: Using Bitwise Operators in Python<\/h3>\n
\n
# function which removes the right most set bit in the\r\n# given number\r\n\r\n\r\ndef clearRight(numb):\r\n # clearing the right most set bit from\r\n # the given number and store it in the result\r\n reslt = (numb) & (numb - 1)\r\n # returing the calculated result\r\n return reslt\r\n# Driver Code\r\n\r\n\r\n# given number\r\nnumb = 19\r\n\r\nprint(\"The given number before removing right most \"\r\n \"set bit : \")\r\nprint(numb)\r\n# passing the given number to clearRight function\r\n# to remove the clear the rightmost setbit\r\nprint(\"The given number after removing right most set bit : \")\r\nprint(clearRight(numb))\r\n<\/pre>\n
The given number before removing right most set bit : \r\n19\r\nThe given number after removing right most set bit : \r\n18<\/pre>\n
\n