Finding Rightmost Set Bit For an Number

Правка en2, от Mohsina_Shaikh, 2022-08-16 07:49:12

Is finding rightmost set bit by this approach wrong.

int ans=xorsum,int rightmost=0;
 while(ans){
            int mask=1<<i;
            if(ans&mask){
                break;
            }
            ans=ans>>1; 
            i++;
        }
   rightmost=1<<i;

My code gave WA for some test cases when I used this code snippet to find rightmost set bit.It worked when I removed this with rightmost=xorsum&(-xorsum) can anyone please why the above got snippet may not work.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский Mohsina_Shaikh 2022-08-16 07:49:12 24
en1 Английский Mohsina_Shaikh 2022-08-16 07:48:16 534 Initial revision (published)