什么是从网络位计数转换为网络掩码的最佳方式? [英] What's the best way to convert from network bitcount to netmask?
本文介绍了什么是从网络位计数转换为网络掩码的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,如果我有一个网络规范像172.20.10.0/24,24是位计数。什么是将其转换成像0xffffff00网络掩码的最佳方式?
For example, if I have a network spec like 172.20.10.0/24, "24" is the bitcount. What's the best way to convert that to a netmask like 0xffffff00 ?
推荐答案
假设32位掩码和32位int。
Assuming 32-bit mask and 32-bit int.
int keepBits = 24; /* actually get it from somewhere else? */
int mask = (0xffffffff >> (32 - keepBits )) << (32 - keepBits);
请注意:什么是信息获取接口网络掩码的最佳途径这不一定是问题的答案
Note: this isn't necessarily the answer to the question "What's the best way to get the network mask for an interface?"
这篇关于什么是从网络位计数转换为网络掩码的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文