/***************************************************************************
* Author: Isai Damier
* Title: Set Bit
* Project: geekviewpoint
* Package: algorithms
*
* Statement:
* Given a bit sequence x, set the kth bit; where the zeroth (0th) bit is
* the least significant (rightmost) bit.
*
* Sample Input: 0010010010
* Sample Output: 0010010011
*
* Description:
*
* Technical Details: Let x = 0010010010. Then only the 1st, 4th,
* and the 7th bits are set. The function setBit(x,0) would
* return 0010010011.
*
* setBit is a two steps operation:
* step_1] Using the shift-left operator << create a bit
* sequence where only the kth bit is set.
* step_2] Take the bitwise OR of x and the sequence in step_1.
*
**************************************************************************/
public int setBit(int x, int kth) {
return x | (1 << kth);
}
import org.junit.Test;
import static org.junit.Assert.*;
public class BitwiseTest {
/**
* Test of setBit method, of class Bitwise.
*/
@Test
public void testSetBit() {
System.out.println(""setBit"");
Bitwise bits = new Bitwise();
int x = Integer.parseInt(""0010010010"",2);
int a = Integer.parseInt(""0010010011"",2);
int b = Integer.parseInt(""0010011010"",2);
int c = Integer.parseInt(""0110010010"",2);
assertEquals(a, bits.setBit(x, 0));
assertEquals(b, bits.setBit(x, 3));
assertEquals(c, bits.setBit(x, 8));
}
}