1010 * @author Deepak
1111 */
1212public 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 ("\n Get 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 \n Setting Bit at position 3" );
115- int res1 = setBit (number , 3 );
116- toFullBinaryString (res1 );
117-
118- /* Update Bit */
119- System .out .println ("\n \n Updating 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 \n Clearing Bit at position 0" );
125- int clearedBitResult = clearBit (number , 0 );
126- toFullBinaryString (clearedBitResult );
127-
128- /**/
129- int number1 = 255 ;
130- System .out .println ("\n \n Binary representation of " + number1 );
131- toFullBinaryString (number1 );
132- System .out .println ("\n Clear bits MSB through 4" );
133- int num3 = clearBitsMSBthroughI (number1 , 4 );
134- toFullBinaryString (num3 );
135-
136-
137- int number2 = 255 ;
138- System .out .println ("\n \n Binary representation of " + number2 );
139- toFullBinaryString (number2 );
140- System .out .println ("\n Clear bits 3 through 0" );
141- int num4 = clearBitsIthrough0 (number2 , 3 );
142- toFullBinaryString (num4 );
143-
106+ return builder .toString ().trim ();
144107 }
145108
146109}
0 commit comments