Turn off the rightmost set bit | Set 2 | Java program to unset the rightmost set bit ; Unsets the rightmost set bit of n and returns the result ; Checking whether bit position is set or not ; If bit position is found set , we flip this bit by xoring given number and number with bit position set ; Driver code
import java . util . * ; class GFG { static void FlipBits ( int n ) { for ( int bit = 0 ; bit < 32 ; bit ++ ) { if ( ( n >> bit ) % 2 > 0 ) { n = n ^ ( 1 << bit ) ; break ; } } System . out . print ( " The ▁ number ▁ after ▁ unsetting ▁ the " ) ; System . out . print ( " ▁ rightmost ▁ set ▁ bit ▁ " + n ) ; } public static void main ( String [ ] args ) { int N = 12 ; FlipBits ( N ) ; } }