计算掩码后IP地址
template<class T>
class SingleInstance {
public:
static inline T instance() {
static T obj;
return obj;
}
private:
SingleInstance() = default;
~SingleInstance() = default;
};
#include <stdint.h>
#include "single_instance.hpp"
class net_utility {
public:
inline uint64_t get_mask(uint64_t ip, int mask) {
if (ip > 0xFFFFFFFF) {
return 0;
}
if (mask <= 0 || mask > 32) {
return 0;
}
return ip & ((0xFFFFFFFF << (32 - mask)));
}
};
#define G_NET_UTILITY SingleInstance<net_utility>::instance()
#include <iostream>
#include "net_utility.hpp"
int main(int argc, const char** argv) {
uint64_t ip = 168432899;
int mask = 24;
uint64_t rs = G_NET_UTILITY.get_mask(ip, mask);
std::cout << rs << std::endl;
return 0;
}
标签:return,int,ip,mask,地址,SingleInstance,IPV4,掩码,include From: https://blog.51cto.com/u_15899033/5902977