2
2
3
3
#### Get Bit
4
4
5
- This method shifts the relevant bit to the zeroth position.
6
- Then we perform ` AND ` operation with one which has bit
5
+ This method shifts the relevant bit to the zeroth position.
6
+ Then we perform ` AND ` operation with one which has bit
7
7
pattern like ` 0001 ` . This clears all bits from the original
8
- number except the relevant one. If the relevant bit is one,
8
+ number except the relevant one. If the relevant bit is one,
9
9
the result is ` 1 ` , otherwise the result is ` 0 ` .
10
10
11
11
> See [ getBit.js] ( getBit.js ) for further details.
@@ -24,7 +24,7 @@ other bits of the number.
24
24
This method shifts ` 1 ` over by ` bitPosition ` bits, creating a
25
25
value that looks like ` 00100 ` . Than it inverts this mask to get
26
26
the number that looks like ` 11011 ` . Then ` AND ` operation is
27
- being applied to both the number and the mask. That operation
27
+ being applied to both the number and the mask. That operation
28
28
unsets the bit.
29
29
30
30
> See [ clearBit.js] ( clearBit.js ) for further details.
@@ -35,21 +35,35 @@ This method is a combination of "Clear Bit" and "Set Bit" methods.
35
35
36
36
> See [ updateBit.js] ( updateBit.js ) for further details.
37
37
38
+ #### isEven
39
+
40
+ This method determines if the number provided is even.
41
+
42
+ ```
43
+ Number: 5
44
+ isEven: false
45
+
46
+ Number: 4
47
+ isEven: true
48
+ ```
49
+
50
+ > See [ isEven.js] ( isEven.js ) for further details.
51
+
38
52
#### Multiply By Two
39
53
40
54
This method shifts original number by one bit to the left.
41
55
Thus all binary number components (powers of two) are being
42
- multiplying by two and thus the number itself is being
56
+ multiplying by two and thus the number itself is being
43
57
multiplied by two.
44
58
45
59
```
46
60
Before the shift
47
61
Number: 0b0101 = 5
48
- Powers of two: 0 + 2^2 + 0 + 2^0
62
+ Powers of two: 0 + 2^2 + 0 + 2^0
49
63
50
64
After the shift
51
65
Number: 0b1010 = 10
52
- Powers of two: 2^3 + 0 + 2^1 + 0
66
+ Powers of two: 2^3 + 0 + 2^1 + 0
53
67
```
54
68
55
69
> See [ multiplyByTwo.js] ( multiplyByTwo.js ) for further details.
@@ -58,17 +72,17 @@ Powers of two: 2^3 + 0 + 2^1 + 0
58
72
59
73
This method shifts original number by one bit to the right.
60
74
Thus all binary number components (powers of two) are being
61
- divided by two and thus the number itself is being
75
+ divided by two and thus the number itself is being
62
76
divided by two without remainder.
63
77
64
78
```
65
79
Before the shift
66
80
Number: 0b0101 = 5
67
- Powers of two: 0 + 2^2 + 0 + 2^0
81
+ Powers of two: 0 + 2^2 + 0 + 2^0
68
82
69
83
After the shift
70
84
Number: 0b0010 = 2
71
- Powers of two: 0 + 0 + 2^1 + 0
85
+ Powers of two: 0 + 0 + 2^1 + 0
72
86
```
73
87
74
88
> See [ divideByTwo.js] ( divideByTwo.js ) for further details.
@@ -87,11 +101,29 @@ inverting all of the bits of the number and adding 1 to it.
87
101
0001 1
88
102
0010 2
89
103
0011 3
90
- ```
104
+ ```
91
105
92
106
> See [ switchSign.js] ( switchSign.js ) for further details.
93
107
94
- #### Multiply Two Numbers
108
+ #### Multiply Two Signed Numbers
109
+
110
+ This method multiplies two signed integer numbers using bitwise operators.
111
+ This method is based on the following :
112
+
113
+ ``` text
114
+ a * b can be written in the below formats
115
+ 0 if a is zero or b is zero or both a and b are zeroes
116
+ 2a * (b/2) if b is even
117
+ 2a * (b - 1)/2 + a if b is odd and positive
118
+ 2a * (b + 1)/2 - a if b is odd and negative
119
+ ```
120
+
121
+ The advantage of this approach is that in each recursive step one of the operands reduces to half its original value.
122
+ Hence, the run time complexity is O(log b) where b is the operand that reduces to half on each recursive step.
123
+
124
+ > See [ multiply.js] ( multiply.js ) for further details.
125
+
126
+ #### Multiply Two Unsigned Numbers
95
127
96
128
This method multiplies two integer numbers using bitwise operators.
97
129
This method is based on that "Every number can be denoted as the sum of powers of 2".
@@ -111,7 +143,7 @@ Then multiplying number `x` by `19` is equivalent of:
111
143
x * 19 = x * 2^4 + x * 2^1 + x * 2^0
112
144
```
113
145
114
- Now we need to remember that ` x * 2^4 ` is equivalent of shifting ` x ` left
146
+ Now we need to remember that ` x * 2^4 ` is equivalent of shifting ` x ` left
115
147
by ` 4 ` bits (` x << 4 ` ).
116
148
117
149
> See [ multiplyUnsigned.js] ( multiplyUnsigned.js ) for further details.
@@ -158,7 +190,7 @@ When we shift 1 four times it will become bigger than 5.
158
190
159
191
#### Is Power of Two
160
192
161
- This method checks if a number provided is power of two. It uses the following
193
+ This method checks if a number provided is power of two. It uses the following
162
194
property. Let's say that ` powerNumber ` is a number that has been formed as a power
163
195
of two (i.e. 2, 4, 8, 16 etc.). Then if we'll do ` & ` operation between ` powerNumber `
164
196
and ` powerNumber - 1 ` it will return ` 0 ` (in case if number is power of two).
0 commit comments