@@ -143,8 +143,13 @@ function endianness() {
143
143
}
144
144
endianness [ Symbol . toPrimitive ] = ( ) => kEndianness ;
145
145
146
- function countOnes ( n ) {
146
+ // Returns the number of ones in the binary representation of the decimal
147
+ // number.
148
+ function countBinaryOnes ( n ) {
147
149
let count = 0 ;
150
+ // Remove one "1" bit from n until n is the power of 2. This iterates k times
151
+ // while k is the number of "1" in the binary representation.
152
+ // For more check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
148
153
while ( n !== 0 ) {
149
154
n = n & ( n - 1 ) ;
150
155
count ++ ;
@@ -166,17 +171,17 @@ function getCIDR({ address, netmask, family }) {
166
171
}
167
172
168
173
const parts = netmask . split ( split ) ;
169
- for ( const part of parts ) {
170
- if ( part !== '' ) {
171
- const binary = parseInt ( part , range ) ;
172
- const tmp = countOnes ( binary ) ;
174
+ for ( var i = 0 ; i < parts . length ; i ++ ) {
175
+ if ( parts [ i ] !== '' ) {
176
+ const binary = parseInt ( parts [ i ] , range ) ;
177
+ const tmp = countBinaryOnes ( binary ) ;
173
178
ones += tmp ;
174
179
if ( hasZeros ) {
175
180
if ( tmp !== 0 ) {
176
181
return null ;
177
182
}
178
183
} else if ( tmp !== groupLength ) {
179
- if ( binary % 2 !== 0 ) {
184
+ if ( ( binary & 1 ) !== 0 ) {
180
185
return null ;
181
186
}
182
187
hasZeros = true ;
@@ -190,9 +195,11 @@ function getCIDR({ address, netmask, family }) {
190
195
function networkInterfaces ( ) {
191
196
const interfaceAddresses = getInterfaceAddresses ( ) ;
192
197
193
- for ( const val of Object . values ( interfaceAddresses ) ) {
194
- for ( const v of val ) {
195
- v . cidr = getCIDR ( v ) ;
198
+ const keys = Object . keys ( interfaceAddresses ) ;
199
+ for ( var i = 0 ; i < keys . length ; i ++ ) {
200
+ const arr = interfaceAddresses [ keys [ i ] ] ;
201
+ for ( var j = 0 ; j < arr . length ; j ++ ) {
202
+ arr [ j ] . cidr = getCIDR ( arr [ j ] ) ;
196
203
}
197
204
}
198
205
0 commit comments