Most efficient way O(1) for finding Right Most Bit No (i.e, integer whose binary representation has that bit 'ON' i.e, set to 1) Formula is a) num & ~(num-1) or can also be written as b) num & (num * -1) // lets understand with a) and example below num = 5 in binary (0000 0101) num-1 = 4 in binary (0000 0100) ~(num-1) = -5 in binary (1111 1011) // Negation results in all bits changing to opposite values 0 to 1 and 1 to 0 num & ~(num-1) = (0000 0101) & (1111 1011) = (0000 0001) = 1 in Decimal system
@7akon3 жыл бұрын
Still in order to find the rightmost set bit from the result (eg: 0010000) , we have to iterate all the way to the left. So I believe in terms of efficiency, Brian Kernighan's method, upon which your solution is based is on par with Methods 1 and 3 of this video.
@naive-fleek7420 Жыл бұрын
@@7akon just take log with base 2 and 1 . u will get the answer
@akshatpandey0074 жыл бұрын
Log2(n^(n&(n-1))) can compute in O(log N) time
@HarshGupta-if2xg4 жыл бұрын
love your explanation thank you so much
@AniketSomwanshi-ll7mz3 жыл бұрын
How is the last method any better than the previous one?
@kanchiseekers2 жыл бұрын
Time complexity is same in all the 3 cases right.
@abhishek24leo4 жыл бұрын
Just do n & (-1 * n). Most efficient.
@sourabhkumar61944 жыл бұрын
hey bro , i know when we do this get rightmost set bit , but didn't able to understand what calculations are done in binary..if you can tell then please share it..
@bishtman122 жыл бұрын
Try for 6636. The answer should be 3. But it will return 4
@agraa3 жыл бұрын
Good explanation. Just in doubt about time complexity. As we need to check bits for max 2 max 64 or 32 or 16 as per processor architecture. So i think it should be O(1)
@bishtman122 жыл бұрын
No, at every step we are basically dividing the number by 2. So, it's log2N.
@jaatharsh4 жыл бұрын
thanks fr the video :)
@dc69404 жыл бұрын
Algorithm with mask is still O(n) where n is no of bits in the number. Lets suppose we have 1 in MSB, alg runs for every bit by right shift.
@techdose4u4 жыл бұрын
Yea correct, it will be O(log N base 2). But since no of bits are generally fixed like 32 or 64 etc therefore, you can say O(1) for for fixed lower values.
@shishpalvishnoi79744 жыл бұрын
Your some playlist contain same video multiple time. e.g in Bit operation playlist Counting Bits is repeated 18 times. please fix it.
@techdose4u4 жыл бұрын
😅 That's youtube error. Dunno why this is happening. But will fix it.
@kirtigera22255 жыл бұрын
Sir which software you r using for this?
@techdose4u5 жыл бұрын
For teaching? It's Ink2Go
@kirtigera22255 жыл бұрын
@@techdose4u sir you r using it in Laptop?
@kirtigera22255 жыл бұрын
@@techdose4u sir means for writing in it we need a tab or we can use mouse for it?
@techdose4u5 жыл бұрын
You will need a tablet
@venkateshthirunagiri855 жыл бұрын
Good 👨🏫👨🏫👨🏫👨🏫
@techdose4u5 жыл бұрын
Thanks :)
@SHIVAMTIWARI-we9jq Жыл бұрын
I think in 3rd method we can also reduce number by n=n>>1
@good-ty8zp3 жыл бұрын
it will also work n&(~n+1)
@kanishkumar61764 жыл бұрын
Rightmostbit = n &(-n)
@RAHUDAS Жыл бұрын
N^[N&(N-1)] this is the most efficient way not the one u share
@iammiturajАй бұрын
What do you even mean by "4 will be converted to binary and takes lot of time for large number"? Every number and computation is done at binary level at the lowest level by any computing device. There is no "decimal in computers", that's only for humans. Misleading video.