diff --git a/05. Bit manipulation/5.3. Flip Bit to Win.md b/05. Bit manipulation/5.3. Flip Bit to Win.md index 1831312..7aa0842 100644 --- a/05. Bit manipulation/5.3. Flip Bit to Win.md +++ b/05. Bit manipulation/5.3. Flip Bit to Win.md @@ -1,6 +1,6 @@ # 5.3. Flip Bit to Win -> You have an integer and you can flip exactly one bit from a 0 to a 1. Write code to find the length of the longest sequence of 1s you could create. Input: 1775, or 11011101111, Output: 8 +> You have an integer and you can flip exactly one bit from a 0 to a 1. Write code to find the length of the longest sequence of 1s you could create. Input: 1775, or 11011101111, Output: 8 Brute force idea: find all 0s, flip all of them, count all 1s sequences (how?), output max. diff --git a/05. Bit manipulation/5.5. Debugger.md b/05. Bit manipulation/5.5. Debugger.md new file mode 100644 index 0000000..1bf97c9 --- /dev/null +++ b/05. Bit manipulation/5.5. Debugger.md @@ -0,0 +1,57 @@ +# 5.5. Debugger + +> Explain what the following code does: (( n & (n-1)) == 0) + +```c++ + 00000010 (n) +& 00000001 (n - 1) +---------- + 00000000 +``` + +n should be odd. And if it's bigger than 2, it'll have some 1s higher up. Which won't be cleared when doing n-1. so n must be 2. + +## Hints + +> Start with a brute force solution. Can you try all possibilities? + +n=2, but there must be something more. + +> What does it mean if A & B == 0? + +That A and B don't have any same bits in any position. + +> If A & B == 0, then it means that A and B never have a 1 at the same spot. Apply this to the equation in the problem. + +A and B are separated by 1 bit in this case, A = B + 1. It could be all 0s and 10 in the end (n=2), or 100000. So that n = pow(2,x). A power of 2. + +```c++ + 10000000 (n) +& 01111111 (n - 1) +---------- + 00000000 +``` + +> If ( n & ( n-1)) == 0, then this means that n and n - 1 never have a 1 in the same spot. Why would that happen? + +When n is a power of 2. + +> What is the relationship between how n looks and how n - 1 looks? Walk through a binary subtraction. + +Yes. + +> When you do a binary subtraction, you flip the rightmost 0s to a 1, stopping when you get to a 1 (which is also flipped). Everything (all the 1 s and Os) on the left will stay put. + +Same answer. + +> Picture n and n - 1. To subtract 1 from n, you flipped the rightmost 1 to a 0 and all the 0s on its right to 1s. If n & n -1 == 0, then there are no 1 s to the left of the first 1. What does that mean about n? + +I'm getting lost. + +> We know that n must have only one 1 ifn & ( n -1) == 0. What sorts ofnumbers have only one 1? + +Powers of 2! Or the binary base number. + +## Solution + +Correct! n = a power of 2, or n=0. diff --git a/05. Bit manipulation/5.6. Conversion.md b/05. Bit manipulation/5.6. Conversion.md new file mode 100644 index 0000000..8ba3a98 --- /dev/null +++ b/05. Bit manipulation/5.6. Conversion.md @@ -0,0 +1,32 @@ +# 5.6. Conversion + +> Write a function to determine the number of bits you would need to flip to convert integer A to integer B + +Input: 29 (or: 11101), 15 (or: 01111) +Output: 2 + +First need to convert to array of bits, right? Levenstein distance between them. Or just iterate the bits by doing num >> 1, get rightmost bit of both nums and do XOR. Sum up all numbers. + +## Hints + +> How would you figure out how many bits are different between two numbers? + +Levenstein distance between the strings, but we're working with numbers. Iterate all bits and add up the differences. + +> Think about what an XOR indicates. If you do a XOR b, where does the result have 1s? Where does it have Os? + +XOR has result 0 if bits are different, 1 if bits are same. + +## Solution + +a ^ b makes XOR between a and b, then just iterate a c and increase count if the value at that point is 1. Then shift c to right, keep deleting the right most bit. + +```c++ +int bitSwapRequired(int a, int b) { + int count = 0; + for (int c = a ^ b; c != 0; c = c >> 1) { + count += c & 1; + } + return count; +} +``` diff --git a/Missing CS semester/README.md b/Missing CS semester/README.md index 2238cf8..5120b9a 100644 --- a/Missing CS semester/README.md +++ b/Missing CS semester/README.md @@ -186,7 +186,7 @@ To enter **Command line mode**, type `:`. * o / O insert line below / above * `A` append to line (in the end of line) * `d{motion}` delete {motion} - * e.g. dw is delete word, d$ is delete to end of line, d0 is* delete to beginning of line, d$ delete until end of line + * e.g. dw is delete word, d$ is delete to end of line, d0 is delete to beginning of line, d$ delete until end of line * `dd` delete whole line. 2dd, delete this and next line * `rx` to replace the character at the cursor by x. `ra`, deletes current character and writes a. * c{motion} change {motion} @@ -199,7 +199,7 @@ To enter **Command line mode**, type `:`. * visual mode + manipulation * select text, d to delete it or c to change it * `u` to undo, `U` to undo whole line, ` + R` to redo -* y to copy / “yank” (some other commands like d also copy) +* `y` to copy / “yank” (some other commands like d also copy) * `p` to paste under the cursor * Lots more to learn: e.g. ~ flips the case of a character diff --git a/README.md b/README.md index 90ebae8..c923a30 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ If you can't afford to buy the book, you can find a free pdf [here](http://ahmed * Insert bit * Binary to string +* Flip bit to create longest sequence of 1s +* (( n & (n-1)) == 0). n=? +* How many different bits do two numbers have. 11001 vs. 11010 = 2. Levenstein distance ## Chapter 7 Object-oriented design