README update
This commit is contained in:
parent
2ea91783a4
commit
58d12aa256
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue