我不明白以下 C 代码行

发布于 2024-09-16 05:57:37 字数 459 浏览 5 评论 0 原文

我发现了以下线程:
根据 IP 和子网掩码计算广播地址,链接如下到 http://lpccomp.bc.ca/netmask/netmask.c

有人可以吗请解释以下行,我不明白:

for ( maskbits=32 ; (mask & (1L<<(32-maskbits))) == 0 ; maskbits-- )

尤其是 mask & (1L<<(32-掩码位))

I found the following thread:
Calculate broadcast address from ip and subnet mask and there the link to http://lpccomp.bc.ca/netmask/netmask.c

Could someone please explain the following line, I don't understand:

for ( maskbits=32 ; (mask & (1L<<(32-maskbits))) == 0 ; maskbits-- )

especially mask & (1L<<(32-maskbits))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-09-23 05:57:37

<<按位左移运算符;它将值的位向左移动给定量。因此,1L<<(32-maskbits) 将值 1 向左移动 32-maskbits 次。

&按位 AND 运算符

所以循环表达式 mask & (1L<<(32-maskbits)) == 0 从低到高测试 mask 值内的所有位。循环将在 mask 的第一个(最低)非零位处停止,此时 maskbits 将包含该位上方(并包括该位)的位数。

例如,

  • 如果 mask == 0xFFFF mask == 0xFFFFFFFF (== binary 11111111111111111111111111111111),循环将在第一次迭代时停止,并且 mask == 0x0001 mask == 0x00000001 (== binary 00000000000000000000000000000001), >maskbits将为32
  • ,循环将再次 maskbits 将为 32
  • 在第一次迭代时停止,如果 mask == 0x1000 mask == 0x01000000(== 二进制 00000001000000000000000000000000), ,循环将在第 24 次迭代时停止,并且 maskbits 将为 8

<< is the bitwise left shift operator; it shifts the bits of a value left by the given amount. Thus 1L<<(32-maskbits) shifts the value 1 to the left 32-maskbits times.

& is the bitwise AND operator.

So the loop expression mask & (1L<<(32-maskbits)) == 0 tests all the bits within the value of mask, from lower to higher. The loop will stop on the first (lowest) nonzero bit of mask, at which point maskbits will contain the number of bits above (and including) that bit.

E.g.

  • if mask == 0xFFFF mask == 0xFFFFFFFF (== binary 11111111111111111111111111111111), the loop will stop on the first iteration, and maskbits will be 32
  • if mask == 0x0001 mask == 0x00000001 (== binary 00000000000000000000000000000001), the loop will again stop on the first iteration, and maskbits will be 32
  • if mask == 0x1000 mask == 0x01000000 (== binary 00000001000000000000000000000000), the loop will stop on the 24th iteration, and maskbits will be 8
听你说爱我 2024-09-23 05:57:37

要查看发生了什么:运行它。

#include <stdio.h> 
#include <iostream>
using namespace std;

char *binary (unsigned int v) {
static char binstr[33] ;
int i ;

binstr[32] = '\0' ;
for (i=0; i<32; i++) {
binstr[31-i] = v & 1 ? '1' : '0' ;
v = v / 2 ;
}

return binstr ;
}

int main(void){  

  unsigned long maskbits,mask;  

mask = 0x01000000;
cout << "MASK IS: " << binary(mask) << "\n";
cout << "32 is: " << binary(32) << "\n\n";
for ( maskbits=32 ; (mask & (1L<<(32-maskbits))) == 0 ; maskbits-- ) {
cout << "maskbits: " << binary(maskbits) << "\n";
cout << "\t(32-maskbits): " << binary((32-maskbits)) << "\n";
cout << "\t1L<<(32-maskbits): " << binary((1L<<(32-maskbits))) << "\n";
cout << "\t(mask & (1L<<(32-maskbits))): " << binary((mask & (1L<<(32-maskbits)))) << "\n\n";

}

cout << "\nFinal maskbits: " << maskbits;

return 0;
}

http://ideone.com/eB8Kp

To see what's happening: run it.

#include <stdio.h> 
#include <iostream>
using namespace std;

char *binary (unsigned int v) {
static char binstr[33] ;
int i ;

binstr[32] = '\0' ;
for (i=0; i<32; i++) {
binstr[31-i] = v & 1 ? '1' : '0' ;
v = v / 2 ;
}

return binstr ;
}

int main(void){  

  unsigned long maskbits,mask;  

mask = 0x01000000;
cout << "MASK IS: " << binary(mask) << "\n";
cout << "32 is: " << binary(32) << "\n\n";
for ( maskbits=32 ; (mask & (1L<<(32-maskbits))) == 0 ; maskbits-- ) {
cout << "maskbits: " << binary(maskbits) << "\n";
cout << "\t(32-maskbits): " << binary((32-maskbits)) << "\n";
cout << "\t1L<<(32-maskbits): " << binary((1L<<(32-maskbits))) << "\n";
cout << "\t(mask & (1L<<(32-maskbits))): " << binary((mask & (1L<<(32-maskbits)))) << "\n\n";

}

cout << "\nFinal maskbits: " << maskbits;

return 0;
}

http://ideone.com/eB8Kp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文