0
# Bitwise Operations
1
2
Bitwise manipulation functions for IP addresses including NOT and OR operations with support for both IPv4 and IPv6 addresses and mixed-protocol operations.
3
4
## Capabilities
5
6
### Bitwise NOT Operation
7
8
#### not
9
10
Performs bitwise NOT (inversion) operation on an IP address, flipping all bits.
11
12
```javascript { .api }
13
/**
14
* Performs bitwise NOT operation on IP address
15
* @param {string} addr - IP address to invert
16
* @returns {string} Bitwise inverted IP address string
17
*/
18
function not(addr);
19
```
20
21
**Usage Examples:**
22
23
```javascript
24
const ip = require('ip');
25
26
// IPv4 bitwise NOT
27
console.log(ip.not('255.255.255.0')); // '0.0.0.255'
28
console.log(ip.not('192.168.1.0')); // '63.87.254.255'
29
console.log(ip.not('0.0.0.0')); // '255.255.255.255'
30
31
// IPv6 bitwise NOT
32
console.log(ip.not('ffff::')); // '::ffff:ffff:ffff:ffff'
33
console.log(ip.not('::1')); // 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe'
34
35
// Practical use case: Convert subnet mask to wildcard mask
36
const subnetMask = '255.255.255.192';
37
const wildcardMask = ip.not(subnetMask);
38
console.log(wildcardMask); // '0.0.0.63'
39
```
40
41
### Bitwise OR Operation
42
43
#### or
44
45
Performs bitwise OR operation between two IP addresses, supporting same-protocol and mixed IPv4/IPv6 operations.
46
47
```javascript { .api }
48
/**
49
* Performs bitwise OR operation on two IP addresses
50
* @param {string} a - First IP address
51
* @param {string} b - Second IP address
52
* @returns {string} Result of bitwise OR operation
53
*/
54
function or(a, b);
55
```
56
57
**Usage Examples:**
58
59
```javascript
60
const ip = require('ip');
61
62
// IPv4 bitwise OR
63
console.log(ip.or('192.168.1.0', '0.0.0.255')); // '192.168.1.255'
64
console.log(ip.or('10.0.0.0', '0.0.5.100')); // '10.0.5.100'
65
console.log(ip.or('255.255.255.0', '0.0.0.63')); // '255.255.255.63'
66
67
// IPv6 bitwise OR
68
console.log(ip.or('::ff', '::abcd:dcba:abcd:dcba'));
69
// '::abcd:dcba:abcd:dcff'
70
71
console.log(ip.or('ffff::', '::1'));
72
// 'ffff::1'
73
74
// Mixed IPv4/IPv6 operations
75
console.log(ip.or('0.0.0.255', '::abcd:dcba:abcd:dcba'));
76
// '::abcd:dcba:abcd:dcff'
77
78
console.log(ip.or('192.168.1.100', '::ff'));
79
// '::c0a8:16ff'
80
81
// Practical use case: Set specific bits in network address
82
const networkAddr = '192.168.1.0';
83
const hostBits = '0.0.0.100';
84
const fullAddress = ip.or(networkAddr, hostBits);
85
console.log(fullAddress); // '192.168.1.100'
86
```
87
88
## Advanced Usage Patterns
89
90
### Subnet Calculation with Bitwise Operations
91
92
```javascript
93
const ip = require('ip');
94
95
// Calculate broadcast address using OR operation
96
const networkAddress = '192.168.1.128'; // /26 network
97
const subnetMask = '255.255.255.192'; // /26 mask
98
const wildcardMask = ip.not(subnetMask); // '0.0.0.63'
99
const broadcastAddress = ip.or(networkAddress, wildcardMask);
100
console.log(broadcastAddress); // '192.168.1.191'
101
102
// Verify against subnet function
103
const subnet = ip.subnet('192.168.1.128', subnetMask);
104
console.log(subnet.broadcastAddress); // '192.168.1.191' (matches)
105
```
106
107
### Network Mask Manipulation
108
109
```javascript
110
const ip = require('ip');
111
112
// Create custom network masks using bitwise operations
113
const baseMask = '255.255.0.0'; // /16
114
const additionalBits = '0.0.255.0'; // Additional /24 subnet bits
115
const combinedMask = ip.or(baseMask, additionalBits);
116
console.log(combinedMask); // '255.255.255.0' (/24)
117
118
// Invert masks for wildcard operations
119
const accessListMask = ip.not('255.255.255.240'); // /28 inverted
120
console.log(accessListMask); // '0.0.0.15' (wildcard for /28)
121
```