For example:

• IP Address: `130.45.34.36`
• Mask: `255.255.240.0`

Ajay
• 18,086
• 12
• 59
• 105
Apprentice
• 329
• 1
• 4
• 4
• [This two-part answer](https://networkengineering.stackexchange.com/a/53994/8499) explains it all. – Ron Maupin Mar 19 '19 at 17:42
• If you or anyone not looking to do it manually may check the IPv4 and IPv6 subnet tools on [this](https://network00.com) website. Almost all the tools on this website **automatically calculates (corrects)** the network address when an IP with subnet mask is provided which is not the network address for that subnet. For example check out [this tool](https://network00.com/NetworkTools/IPv4SubnetCalculator) . HTH someone. – Vivek Singh Aug 24 '20 at 11:09

Let's write both in binary:

`130.45.34.36 = 10000010.00101101.00100010.00100100`

`255.255.240.0 = 11111111.11111111.11110000.00000000`

A bitwise AND between the two would give us the network address:

``````10000010.00101101.00100010.00100100   (ip address)
AND
=
10000010.00101101.00100000.00000000 = 130.45.32.0 (the resulting network address)
``````

``````10000010.00101101.00100000.00000000   (netadress)
OR
=
``````
Malt
• 28,965
• 9
• 65
• 105
• Removed my answer, yours is a perfect! – CustomX Mar 13 '15 at 15:33
• I've seen configuration options that allow the "all zeros" address (the network address) to be used as the broadcast address. I've never seen anything actually configured this way, however. – Brian White Mar 13 '15 at 17:13
``````var network = calculateNetworkIP("192.168.0.101", "255.255.255.0");

var binaryNetwork = [];
for (var j = 0; j < maskBinaryIP.length; j++) {
}

var NetworkIPArr = convertBinaryIPToDecIP(binaryNetwork);

var NetworkIPStr = "";
for (var k = 0; k < NetworkIPArr.length; k++) {
NetworkIPStr += NetworkIPArr[k]+".";
}
return NetworkIPStr.slice(0, -1);
}

var invertedMark = [];
for (var i = 0; i < maskBinaryIP.length; i++) {
}

for (var j = 0; j < maskBinaryIP.length; j++) {
}

for (var k = 0; k < broadcastIPArr.length; k++) {
}
}

function invertedBinary(number){

var no = number+"";
var noArr = no.split("");
var newNo = "";
for(var i = 0; i < noArr.length; i++){
if(noArr[i] == "0"){
newNo += "1";
}else{
newNo += "0";
}
}
return newNo;
}

function bitwiseAND(firstBinary, secondBinary){

var firstArr = [];
var secondArr = [];
firstArr = firstBinary.split("");
secondArr = secondBinary.split("");
for(var i = 0; i < firstArr.length; i++){
if(firstArr[i]+"+"+secondArr[i] == "1+0"){
}else if(firstArr[i]+"+"+secondArr[i] == "0+1"){
}else if(firstArr[i]+"+"+secondArr[i] == "1+1"){
}else if(firstArr[i]+"+"+secondArr[i] == "0+0"){
}
}
}

function bitwiseOR(firstBinary, secondBinary){

var firstArr = [];
var secondArr = [];
firstArr = firstBinary.split("");
secondArr = secondBinary.split("");
for(var i = 0; i < firstArr.length; i++){
if(firstArr[i]+"+"+secondArr[i] == "1+0"){
}else if(firstArr[i]+"+"+secondArr[i] == "0+1"){
}else if(firstArr[i]+"+"+secondArr[i] == "1+1"){
}else if(firstArr[i]+"+"+secondArr[i] == "0+0"){
}
}
}

function convertBinaryIPToDecIP(binaryIPArr){

for (var i = 0; i < binaryIPArr.length; i++) {
}
}

var binaryIP = [];
for (var i = 0; i < ipArr.length; i++) {
var binaryNo = parseInt(ipArr[i]).toString(2);
if(binaryNo.length == 8){
binaryIP.push(binaryNo);
}else{
var diffNo = 8 - binaryNo.length;
var createBinary = '';
for (var j = 0; j < diffNo; j++) {
createBinary += '0';
}
createBinary += binaryNo;
binaryIP.push(createBinary);
}
}
return binaryIP;
}
``````
Pang
• 9,564
• 146
• 81
• 122

Code example based on Malt's answer:

``````const
subnet = '255.255.240.0',
subnets = subnet.split('.');

let networks = [],

for (let i in ipadrs) {
}

for (let i in networks) {
broadcasts[i] = networks[i] | ~subnets[i] + 256;
}

``````
Ben
• 780
• 7
• 10

1. calculate total no of hosts (in this case it is 2^12 = 4096)

2. Divide it by 256(in this case it is 16) and add the result - 1(in this case 15) in *corresponding octet(in this case second octet i.e. 32+15=47) and make other octet 255

*we can get the corresponding octet by looking at the no of hosts. e.g if the no of hosts are greater than 256 then we have to add it to the 2nd octet of network address and so on

Yogi
• 9,174
• 2
• 46
• 61

typescript version

``````function getBroadcastAddress({ address, netmask }: NetworkInterfaceInfo) {
);
(_, index) => subnetBytes[index] | (~netmaskBytes[index] + 256)
);