Skip to content

Commit 59060f8

Browse files
committed
Added more info on bits readme
1 parent 932f210 commit 59060f8

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/main/java/com/ctci/treesandgraphs/RouteBetweenNodes.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class Graph {
3232

3333
private static final Map<Integer, GraphNode> nodes = new HashMap<>();
3434

35+
/**
36+
* Adds an edge from a node with value {@code v1} to another node with value {@code v2}.
37+
* Note: This code doesn't work for nodes having duplicate values.
38+
*
39+
* @param v1
40+
* @param v2
41+
*/
3542
void addEdge(int v1, int v2) {
3643
GraphNode n1 = nodes.get(v1);
3744
GraphNode n2 = nodes.get(v2);
@@ -50,7 +57,7 @@ void addEdge(int v1, int v2) {
5057

5158
/**
5259
* Checks for a path from a node with value {@code v1} to another node with value {@code v2} in a breadth-first
53-
* manner.
60+
* manner. Note: This code doesn't work for nodes having duplicate values.
5461
*
5562
* @param v1 the value of the first node or starting node.
5663
* @param v2 the value of the ending node.

src/main/java/com/rampatra/bits/README.md

+39-4
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,53 @@ _Disclaimer: We are taking `byte` (8 bits) as our datatype to explain the
7070
#### 1. Set the 4th bit from right:
7171

7272
```java
73-
int mask = 1 << 3;
73+
byte mask = 1 << 3;
7474
```
7575

76-
For example,
76+
Explanation,
7777

7878
```
79-
00100000 | mask = 00101000
79+
00000001 << 3 = 00001000
8080
```
8181

8282
#### 2. Set the first 3 bits from right:
8383

8484
```java
85-
int mask = (1 << 4) - 1;
85+
byte mask = (1 << 3) - 1;
8686
```
8787

88+
Explanation,
89+
90+
```
91+
00000001 << 3 = 00001000
92+
93+
00001000 - 00000001 = 00000111
94+
```
95+
96+
#### 3. Mask with alternating 1010...10
97+
98+
```java
99+
byte mask = 0x55;
100+
```
101+
102+
#### 4. Mask with alternating 0101...01
103+
104+
```java
105+
byte mask = 0xaa;
106+
```
107+
108+
#### 5. Unset the 4th bit
109+
110+
```java
111+
byte mask = 1 << 3;
112+
113+
byte num = num & ~mask;
114+
```
115+
116+
Explanation,
117+
118+
```
119+
00000001 << 3 = 00001000
120+
121+
~(00001000) = 11110111
122+
```

0 commit comments

Comments
 (0)