32-bit Binary Formatter
Dark Mode
Enter a decimal number between 0 and 4,294,967,295 to see its binary
representation
Segment Highlighting:
Real-World Example: Network Subnet Analysis
This example demonstrates how to use the binary formatter in a practical networking context. The code analyzes subnet configurations and provides detailed binary representations of network components.
Network Subnet Analysis
/**
* Analyzes a network subnet configuration and provides detailed information
* about the subnet mask, network address, and available host addresses.
*
* @param {number} ipAddress - The IP address in decimal format
* @param {number} subnetMask - The subnet mask in decimal format
* @returns {Object} Analysis results including binary representations
*/
function analyzeSubnet(ipAddress, subnetMask) {
// Format the IP address and subnet mask in binary
const ipBinary = formatBinary({
number: ipAddress,
textColor: "#FFFFFF", // White text for dark mode
mode: "dark"
});
const maskBinary = formatBinary({
number: subnetMask,
textColor: "#FFFFFF",
mode: "dark"
});
// Calculate network address (IP AND subnet mask)
const networkAddress = ipAddress & subnetMask;
const networkBinary = formatBinary({
number: networkAddress,
textColor: "#FFFFFF",
mode: "dark"
});
// Calculate broadcast address (network address OR inverted subnet mask)
const invertedMask = ~subnetMask >>> 0; // Convert to unsigned 32-bit
const broadcastAddress = networkAddress | invertedMask;
const broadcastBinary = formatBinary({
number: broadcastAddress,
textColor: "#FFFFFF",
mode: "dark"
});
// Calculate number of available host addresses
const numHosts = Math.pow(2, 32 - countSetBits(subnetMask)) - 2;
return {
ipAddress: {
decimal: ipAddress,
binary: ipBinary
},
subnetMask: {
decimal: subnetMask,
binary: maskBinary
},
networkAddress: {
decimal: networkAddress,
binary: networkBinary
},
broadcastAddress: {
decimal: broadcastAddress,
binary: broadcastBinary
},
numHosts: numHosts
};
}
/**
* Helper function to count the number of set bits in a number
* @param {number} num - The number to analyze
* @returns {number} Count of set bits
*/
function countSetBits(num) {
let count = 0;
while (num) {
count += num & 1;
num >>>= 1;
}
return count;
}
// Example usage:
// IP Address Conversion:
// 192.168.1.1 in decimal is converted to a single 32-bit number:
// 192 * (256^3) + 168 * (256^2) + 1 * (256^1) + 1 * (256^0)
// = 192 * 16,777,216 + 168 * 65,536 + 1 * 256 + 1 * 1
// = 3,221,225,472 + 11,010,048 + 256 + 1
// = 3,232,235,777
const ipAddress = 3232235777; // 192.168.1.1
// Subnet Mask Conversion:
// 255.255.255.0 in decimal is converted to a single 32-bit number:
// 255 * (256^3) + 255 * (256^2) + 255 * (256^1) + 0 * (256^0)
// = 255 * 16,777,216 + 255 * 65,536 + 255 * 256 + 0 * 1
// = 4,278,190,080 + 16,711,680 + 65,280 + 0
// = 4,294,967,040
// This mask represents a /24 network (24 bits for network, 8 bits for hosts)
const subnetMask = 4294967040; // 255.255.255.0
// The subnet mask 255.255.255.0 is chosen because:
// 1. It's a common subnet mask for small networks
// 2. It provides 254 usable host addresses (2^8 - 2)
// 3. It's easy to understand and visualize in binary
// 4. It's commonly used in home and small office networks
const subnetInfo = analyzeSubnet(ipAddress, subnetMask);
console.log("Subnet Analysis Results:");
// Subnet Analysis Results:
console.log("IP Address:", subnetInfo.ipAddress.binary);
// IP Address: 11000000 10101000 00000001 00000001
console.log("Subnet Mask:", subnetInfo.subnetMask.binary);
// Subnet Mask: 11111111 11111111 11111111 00000000
console.log("Network Address:", subnetInfo.networkAddress.binary);
// Network Address: 11000000 10101000 00000001 00000000
console.log("Broadcast Address:", subnetInfo.broadcastAddress.binary);
// Broadcast Address: 11000000 10101000 00000001 11111111
console.log("Available Hosts:", subnetInfo.numHosts);
// Available Hosts: 254