From 58d12aa25675fc1be9d52f3b2a553815dd5fed8e Mon Sep 17 00:00:00 2001 From: anebz Date: Tue, 17 Mar 2020 16:53:17 +0100 Subject: [PATCH] README update --- 05. Bit manipulation/README.md | 18 +++++++++++------- README.md | 7 ++++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/05. Bit manipulation/README.md b/05. Bit manipulation/README.md index 4df256e..d0d60e2 100644 --- a/05. Bit manipulation/README.md +++ b/05. Bit manipulation/README.md @@ -1,5 +1,7 @@ # Chapter 5. Bit manipulation +page 127. hints start at 673 + ## 5.1. Two's complement and negative numbers Positive numbers are represented as themselves, negative numbers as the two's complement of its absolute value, with a 1 in its sign bit, indicating it's negative. @@ -19,6 +21,14 @@ Indicated with a >>> operator, all bits are shifted, sign bit too. * -75: **1**0110101 * 90: **0**1011010 +```c++ +int repeatedLogicalShift(int x, int count) { + for (int i = 0; i < count; i++) { + x >>>= 1; // Logical shift by 1 + } +} +``` + ### 5.2.2. Arithmetic shift The sign bit is kept, and all bits (sign bit too) are shifted. @@ -32,12 +42,6 @@ int repeatedArithmeticShift(int x, int count) { x >>= 1; // Arithmetic shift by 1 } } - -int repeatedLogicalShift(int x, int count) { - for (int i = 0; i < count; i++) { - x >>>= 1; // Logical shift by 1 - } -} ``` ## 5.3. Common bit tasks: getting and setting @@ -54,7 +58,7 @@ boolean getBit(int num, int i) { ### Set bit -This function shifts 1 over by i bits, creating a value like 00010000. By performing an OR with `num`, only the value at bit i will change. All other bits of the mask are zero, and won't affect `num`. +This function shifts 1 over by i bits, creating a value like 00010000. By performing an OR with `num`, only the value at bit i will change. All other bits of the mask are zero, and won't affect `num`. It sets the bit to 1 if it's 0, otherwise leaves the number unchanged. ```c++ boolean setBit(int num, int i) { diff --git a/README.md b/README.md index 69c56cf..90ebae8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Cracking the coding interview exercises and notes -If you can't afford to buy the book, you can find a free pdf [here](https://leonmercanti.com/books/personal-development/Cracking%20the%20Coding%20Interview%20189%20Programming%20Questions%20and%20Solutions.pdf). +If you can't afford to buy the book, you can find a free pdf [here](http://ahmed-badawy.com/blog/wp-content/uploads/2018/10/Cracking-the-Coding-Interview-6th-Edition-189-Programming-Questions-and-Solutions.pdf) (Updated as of 2020.03.14). ## Introduction @@ -49,6 +49,11 @@ If you can't afford to buy the book, you can find a free pdf [here](https://leon * Create minimal binary search tree from array * Create a linked list for each depth in the tree +## Chapter 5 + +* Insert bit +* Binary to string + ## Chapter 7 Object-oriented design * Hash tables