File tree 2 files changed +47
-5
lines changed
2 files changed +47
-5
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,13 @@ class Graph {
32
32
33
33
private static final Map <Integer , GraphNode > nodes = new HashMap <>();
34
34
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
+ */
35
42
void addEdge (int v1 , int v2 ) {
36
43
GraphNode n1 = nodes .get (v1 );
37
44
GraphNode n2 = nodes .get (v2 );
@@ -50,7 +57,7 @@ void addEdge(int v1, int v2) {
50
57
51
58
/**
52
59
* 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.
54
61
*
55
62
* @param v1 the value of the first node or starting node.
56
63
* @param v2 the value of the ending node.
Original file line number Diff line number Diff line change @@ -70,18 +70,53 @@ _Disclaimer: We are taking `byte` (8 bits) as our datatype to explain the
70
70
#### 1. Set the 4th bit from right:
71
71
72
72
``` java
73
- int mask = 1 << 3 ;
73
+ byte mask = 1 << 3 ;
74
74
```
75
75
76
- For example ,
76
+ Explanation ,
77
77
78
78
```
79
- 00100000 | mask = 00101000
79
+ 00000001 << 3 = 00001000
80
80
```
81
81
82
82
#### 2. Set the first 3 bits from right:
83
83
84
84
``` java
85
- int mask = (1 << 4 ) - 1 ;
85
+ byte mask = (1 << 3 ) - 1 ;
86
86
```
87
87
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
+ ```
You can’t perform that action at this time.
0 commit comments