Skip to content

Commit 02c2fb7

Browse files
author
Deepak Malik
committed
Adding test cases for common operations on bits
1 parent 8833096 commit 02c2fb7

File tree

2 files changed

+137
-75
lines changed

2 files changed

+137
-75
lines changed

src/com/deepak/algorithms/BitManipulation/CommonOperations.java

Lines changed: 38 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author Deepak
1111
*/
1212
public class CommonOperations {
13-
13+
1414
/**
1515
* Returns the bit present at the position, either true(1) or false(0)
1616
*
@@ -22,19 +22,6 @@ public static boolean getBit(int num, int i) {
2222
return ((num & (1 << i)) != 0);
2323
}
2424

25-
/**
26-
* Returns the number after updating the bit to either 0 or 1
27-
*
28-
* @param num - Number
29-
* @param i - Position, for which bit has to be updated
30-
* @param v - Value of new bit
31-
* @return {@link int} - New updated number
32-
*/
33-
public static int updateBit(int num, int i, int v) {
34-
int mask = ~(1 << i);
35-
return (num & mask) | (v << i);
36-
}
37-
3825
/**
3926
* Returns the number after setting the bit to 1 at given position
4027
* If bit at that position is already 1, then it returns the same number
@@ -60,87 +47,63 @@ public static int clearBit(int num, int i) {
6047
return num & mask;
6148
}
6249

63-
public static int clearBitsMSBthroughI(int num, int i) {
50+
/**
51+
* Returns the number after updating the bit to either 0 or 1
52+
*
53+
* @param num - Number
54+
* @param i - Position, for which bit has to be updated
55+
* @param v - Value of new bit
56+
* @return {@link int} - New updated number
57+
*/
58+
public static int updateBit(int num, int i, int v) {
59+
int mask = ~(1 << i);
60+
return (num & mask) | (v << i);
61+
}
62+
63+
/**
64+
* Method to clear most significant bits through I
65+
*
66+
* @param num - Number
67+
* @param i - Position of the bit
68+
* @return {@link int} - New updated number
69+
*/
70+
public static int clearMSBthroughI(int num, int i) {
6471
int mask = (1 << i) - 1;
6572
return num & mask;
6673
}
6774

68-
public static int clearBitsIthrough0(int num, int i) {
75+
/**
76+
* Method to clear least significant bits through I
77+
*
78+
* @param num - Number
79+
* @param i - Position of the bit
80+
* @return {@link int} - New updated number
81+
*/
82+
public static int clearLSBthroughI(int num, int i) {
6983
int mask = ~((1 << (i + 1)) - 1);
7084
return num & mask;
7185
}
7286

7387
/**
74-
* Prints the full binary number in String format
88+
* Prints the integer number into binary string format
7589
*
76-
* @param num - Number
90+
* @param num
91+
* @return {@link String}
7792
*/
78-
private static void toFullBinaryString(int num) {
93+
public static String toFullBinaryString(int num) {
94+
StringBuilder builder = new StringBuilder();
7995
/* Create a display mask on the last bit */
8096
int displayMask = 1 << 7;
8197
/* Loop through all the bits and print accordingly */
8298
for (int bit = 1; bit <= 8; bit++) {
83-
System.out.print((num & displayMask) == 0 ? '0' : '1');
99+
builder.append((num & displayMask) == 0 ? '0' : '1');
84100
num <<= 1;
85101
/* Create space after 4 bits */
86102
if ((bit % 4) == 0) {
87-
System.out.print(' ');
88-
}
89-
}
90-
}
91-
92-
/**
93-
* Dealing with 8 bit representation to make it simple
94-
* @param args
95-
*/
96-
// TODO : Clean this up
97-
public static void main(String[] args) {
98-
int number = 21;
99-
System.out.println("Testing with number: " + number);
100-
toFullBinaryString(number);
101-
102-
/* Get bit */
103-
System.out.println();
104-
System.out.println("\nGet Bit (Original)");
105-
for (int i = 7; i >= 0; i--) {
106-
int res = getBit(number, i) ? 1 : 0;
107-
System.out.print(res);
108-
if ((i % 4) == 0) {
109-
System.out.print(' ');
103+
builder.append(' ');
110104
}
111105
}
112-
113-
/* Set bit */
114-
System.out.println("\n\nSetting Bit at position 3");
115-
int res1 = setBit(number, 3);
116-
toFullBinaryString(res1);
117-
118-
/* Update Bit */
119-
System.out.println("\n\nUpdating Bit at position 2 with 0");
120-
int result = updateBit(number, 2, 0);
121-
toFullBinaryString(result);
122-
123-
/* Clear Bit */
124-
System.out.println("\n\nClearing Bit at position 0");
125-
int clearedBitResult = clearBit(number, 0);
126-
toFullBinaryString(clearedBitResult);
127-
128-
/**/
129-
int number1 = 255;
130-
System.out.println("\n\nBinary representation of " + number1);
131-
toFullBinaryString(number1);
132-
System.out.println("\nClear bits MSB through 4");
133-
int num3 = clearBitsMSBthroughI(number1, 4);
134-
toFullBinaryString(num3);
135-
136-
137-
int number2 = 255;
138-
System.out.println("\n\nBinary representation of " + number2);
139-
toFullBinaryString(number2);
140-
System.out.println("\nClear bits 3 through 0");
141-
int num4 = clearBitsIthrough0(number2, 3);
142-
toFullBinaryString(num4);
143-
106+
return builder.toString().trim();
144107
}
145108

146109
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Algorithms-in-Java
3+
* CommonOperations.java
4+
*/
5+
package com.deepak.algorithms.BitManipulation;
6+
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
/**
12+
* Test cases for common operations on bits
13+
*
14+
* @author Deepak
15+
*/
16+
public class CommonOperations_Test {
17+
18+
/* Initialize variables */
19+
int eightBitNumber;
20+
int secondNumber;
21+
22+
/* Setup test cases */
23+
@Before
24+
public void setup() {
25+
eightBitNumber = 21;
26+
secondNumber = 255;
27+
}
28+
29+
/**
30+
* Test case to check get bit operation
31+
*/
32+
@Test
33+
public void testGetBitOperation() {
34+
Assert.assertEquals(CommonOperations.toFullBinaryString(eightBitNumber), "0001 0101");
35+
Assert.assertTrue(CommonOperations.getBit(21, 2));
36+
Assert.assertFalse(CommonOperations.getBit(21, 5));
37+
}
38+
39+
/**
40+
* Test case to check set bit operation
41+
*/
42+
@Test
43+
public void testSetBitOperation() {
44+
Assert.assertEquals(CommonOperations.toFullBinaryString(eightBitNumber), "0001 0101");
45+
/* It remains the same number, since bit at 2nd position is already 1 */
46+
Assert.assertEquals(CommonOperations.setBit(21, 2), 21);
47+
/* Number becomes 29 once we flip the bit at position three */
48+
Assert.assertEquals(CommonOperations.setBit(21, 3), 29);
49+
}
50+
51+
/**
52+
* Test case to check clear bit operation
53+
*/
54+
@Test
55+
public void testClearBitOperation() {
56+
Assert.assertEquals(CommonOperations.toFullBinaryString(eightBitNumber), "0001 0101");
57+
/* It remains the same number, since bit at 3rd position is already 1 */
58+
Assert.assertEquals(CommonOperations.clearBit(21, 3), 21);
59+
/* Number becomes 17 once we flip the bit to 0 at position 2 */
60+
Assert.assertEquals(CommonOperations.clearBit(21, 2), 17);
61+
}
62+
63+
/**
64+
* Test case to check update bit operation
65+
*/
66+
@Test
67+
public void testUpdateBitOperation() {
68+
Assert.assertEquals(CommonOperations.toFullBinaryString(eightBitNumber), "0001 0101");
69+
/* It remains the same, since at 1st position, it's already 0 */
70+
Assert.assertEquals(CommonOperations.updateBit(21, 0, 1), 21);
71+
/* Number became 20 once we flip the bit at 0th position to 0 */
72+
Assert.assertEquals(CommonOperations.updateBit(21, 0, 0), 20);
73+
/* Number became 29 once we flip the bit at 3rd position to 1 */
74+
Assert.assertEquals(CommonOperations.updateBit(21, 3, 1), 29);
75+
}
76+
77+
/**
78+
* Test case to clear most significant bits through I
79+
* NOTE : Count starts from 1 i.e 4 Bits means total 4 bits
80+
*/
81+
@Test
82+
public void testClearMSBThroughI() {
83+
Assert.assertEquals(CommonOperations.toFullBinaryString(secondNumber), "1111 1111");
84+
Assert.assertEquals(CommonOperations.toFullBinaryString(CommonOperations.clearMSBthroughI(255, 4)), "0000 1111");
85+
Assert.assertEquals(CommonOperations.clearMSBthroughI(255, 4), 15);
86+
}
87+
88+
/**
89+
* Test case to clear least significant bits through I
90+
* NOTE : Count starts from 0 i.e 4 means total of 5 bits
91+
*/
92+
@Test
93+
public void testClearLSBThroughI() {
94+
Assert.assertEquals(CommonOperations.toFullBinaryString(secondNumber), "1111 1111");
95+
Assert.assertEquals(CommonOperations.toFullBinaryString(CommonOperations.clearLSBthroughI(255, 3)), "1111 0000");
96+
Assert.assertEquals(CommonOperations.clearLSBthroughI(255, 4), 224);
97+
}
98+
99+
}

0 commit comments

Comments
 (0)