We can unset
nth bit by using <<, ~ and & operators.
public class UnsetNthBit { public static int unsetNthBit(int num, int n) { num = num & ~(1 << n); return num; } }
Following is
the junit test case for above program.
import static org.junit.Assert.assertEquals; import org.junit.Test; public class UnsetNthBitTest { @Test public void test1() { assertEquals(UnsetNthBit.unsetNthBit(85, 0), 84); assertEquals(UnsetNthBit.unsetNthBit(84, 2), 80); assertEquals(UnsetNthBit.unsetNthBit(80, 4), 64); } }
No comments:
Post a Comment