Skip to content

Enter a IP address #107

Answered by palonza
workshop748 asked this question in Q&A
May 28, 2023 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

hi @workshop748,

in this example, 'calculateSubnetMask' takes prefix length as input and generates the corresponding subnet mask using bitwise operations and main function prompts the user to enter IP and prefix... is basic but is working... please let me know if work to you... regards

#include <iostream>
#include <string>
#include <sstream>
#include <bitset>

// Function to calculate the subnet mask based on the prefix length
std::string calculateSubnetMask(int prefixLength) {
    std::bitset<32> bits;
    for (int i = 0; i < prefixLength; ++i) {
        bits.set(i, true);
    }
    std::stringstream ss;
    ss << (bits.to_ulong() >> 24) << '.' << ((bits.to_ulong() >> 16) & 255) << '.'

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@workshop748
Comment options

@palonza
Comment options

@workshop748
Comment options

@palonza
Comment options

Answer selected by ThusharaX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants