ComputerNetworksAndSecurity/Codes/Code-B5.cpp

36 lines
805 B
C++
Raw Normal View History

2024-10-09 20:14:30 +05:30
#include <iostream>
using namespace std;
void printSubnetMask(int prefixLength) {
int mask[4] = {0, 0, 0, 0};
for (int i = 0; i < prefixLength; ++i) {
mask[i / 8] |= (1 << (7 - (i % 8)));
}
cout << "Subnet Mask for /" << prefixLength << ": ";
cout << mask[0] << "." << mask[1] << "." << mask[2] << "." << mask[3] << std::endl;
}
int main() {
int prefixLength;
cout << "Enter the prefix length: ";
cin >> prefixLength;
if (prefixLength >= 0 && prefixLength <= 32) {
printSubnetMask(prefixLength);
}
else {
cout << "Invalid prefix length!" << std::endl;
}
return 0;
}
/*
OUTPUT:
Enter the prefix length: 22
Subnet Mask for /22: 255.255.252.0
Enter the prefix length: 0
Subnet Mask for /0: 0.0.0.0
Enter the prefix length: 32
Subnet Mask for /32: 255.255.255.255
*/