Skip to content

Commit

Permalink
Problem 01 - Ch 05
Browse files Browse the repository at this point in the history
  • Loading branch information
deepak-malik committed Feb 8, 2017
1 parent ec661aa commit 691102a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Java Solutions for Cracking the Coding Interview - 6th Edition
- [X] [12 - Paths with Sum](../master/src/com/deepak/ctci/Ch04_Trees_And_Graphs/Problem_12.java)

**Ch 05 - Bit Manipulation**
- [ ] 01 - Insertion
- [X] [01 - Insertion](../master/src/com/deepak/ctci/Ch05_Bit_Manipulation/Problem_01.java)
- [ ] 02 - Binary to String
- [ ] 03 - Flip Bit to Win
- [ ] 04 - Next Number
Expand Down
32 changes: 27 additions & 5 deletions src/com/deepak/ctci/Ch05_Bit_Manipulation/Problem_01.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,35 @@
* @author Deepak
*/
public class Problem_01 {

public int mergeMIntoN(int M, int N, int i, int j) {
if (i < 0 || i >= 32 || j < i) {

/**
* Method to merge M into N
*
* @param N
* @param M
* @param i
* @param j
* @return {@link int}
*/
public static int mergeMIntoN(int N, int M, int i, int j) {
/* Since we are dealing with 32 bits,
* i and j can't be greater then 32 */
if (i > 32 || i > j) {
return 0;
}

return 0;
/* We need to clear the bits from i to j.
* For that, we need a mask. Let's take all 1's
* and keep only those 1, where M can fit in, rest
* can be made 0 */
int allOnes = ~0;
int left = allOnes << (j + 1);
int right = ((1 << i) - 1);
int mask = left | right;

/* Clear i through j */
int n_cleared = N & mask;
int m_shifted = M << i;
return n_cleared | m_shifted;
}

}
22 changes: 22 additions & 0 deletions src/com/deepak/ctci/Library/HelperMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,27 @@ public static int randomIntInRange(int min, int max) {
Random random = new Random();
return random.nextInt(max + 1 - min) + min;
}

/**
* Method to print a full binary string
*
* @param input
* @return {@link String}
*/
public static String toFullBinaryString(int input, int numberOfBits) {
StringBuilder builder = new StringBuilder();
/* Create a display mask on the last bit */
int displayMask = 1 << (numberOfBits - 1);
/* Loop through all the bits and print accordingly */
for (int bit = 1; bit <= numberOfBits; bit++) {
builder.append((input & displayMask) == 0 ? '0' : '1');
input <<= 1;
/* Create space after 4 bits */
if ((bit % 4) == 0) {
builder.append(' ');
}
}
return builder.toString().trim();
}

}
31 changes: 31 additions & 0 deletions test/com/deepak/ctci/Ch05_Bit_Manipulation/Problem_01_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Cracking-The-Coding-Interview
* Problem_01_Test.java
*/
package com.deepak.ctci.Ch05_Bit_Manipulation;

import org.junit.Assert;
import org.junit.Test;

import com.deepak.ctci.Library.HelperMethods;

/**
* Test cases for Problem 01
*
* @author Deepak
*/
public class Problem_01_Test {

/**
* Method to test merging of bits from M to N
*/
@Test
public void testMergeOfBits() {
int N = 103217;
int M = 13;
Assert.assertEquals(HelperMethods.toFullBinaryString(N, 32), "0000 0000 0000 0001 1001 0011 0011 0001");
Assert.assertEquals(HelperMethods.toFullBinaryString(M, 32), "0000 0000 0000 0000 0000 0000 0000 1101");
Assert.assertEquals(HelperMethods.toFullBinaryString(Problem_01.mergeMIntoN(103217, 13, 4, 12), 32), "0000 0000 0000 0001 1000 0000 1101 0001");
}

}

0 comments on commit 691102a

Please sign in to comment.