Print Value Of Rsb Mask
1. You are given a number n.Input Format
2. You have to print the right-most set bit mask.
A number nOutput Format
A numberQuestion Video
1 <= n <= 10^9Sample Input
58Sample Output
10
-
Editorial
The problem here deals with getting the rightmost set bit of the given input number. For example, n = 58 => [ 1 1 1 0 1 0 ], the answer should be 2 => [ 0 0 0 0 1 0 ] as the rightmost set bit for n is at 1st index.
To achieve this we need to follow two steps firstly we need to flip the bits of the input number and store it in a mask. Thereafter we need to add 1 to the mask to get our desired mask. AND operation with this mask will yield us the RSB of the given input number.
Generalised Case for RSB:
Time Complexity: O(1)
The time complexity for the function is constant as basic bit operations are involved.
Space Complexity: O(1)
The space complexity for the function is constant.
-
Asked in Companies
-
Related Topics
Video Solution
Code Solution
{ }
{ }
Run