简体   繁体   English

我不明白以下C代码行

[英]I don't understand the following C code line

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 从ip和子网掩码计算广播地址,并链接到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)) 特别是mask & (1L<<(32-maskbits))

<< 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. 因此, 1L<<(32-maskbits)将值1移位到左32-maskbits时间。

& is the bitwise AND operator . &按位AND运算符

So the loop expression mask & (1L<<(32-maskbits)) == 0 tests all the bits within the value of mask , from lower to higher. 所以循环表达式mask & (1L<<(32-maskbits)) == 0测试mask值中的所有位,从低到高。 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. 循环将停止在第一(最低)非零的位mask ,在该点maskbits将包含的比特数以上(包括)该位。

Eg 例如

  • if mask == 0xFFFF mask == 0xFFFFFFFF (== binary 11111111111111111111111111111111) , the loop will stop on the first iteration, and maskbits will be 32 如果 mask == 0xFFFF mask == 0xFFFFFFFF (== binary 11111111111111111111111111111111) ,循环将在第一次迭代时停止,并且maskbits将为32
  • if mask == 0x0001 mask == 0x00000001 (== binary 00000000000000000000000000000001) , the loop will again stop on the first iteration, and maskbits will be 32 如果 mask == 0x0001 mask == 0x00000001 (== binary 00000000000000000000000000000001) ,循环将在第一次迭代时再次停止,并且maskbits将为32
  • if mask == 0x1000 mask == 0x01000000 (== binary 00000001000000000000000000000000) , the loop will stop on the 24th iteration, and maskbits will be 8 如果 mask == 0x1000 mask == 0x01000000 (== binary 00000001000000000000000000000000) ,循环将在第24次迭代时停止,并且maskbits将为8

Have a look at bitwise operators, specifically left shift. 看看按位运算符,特别是左移。

http://en.wikipedia.org/wiki/Bitwise_operation#Shifts_in_C.2C_C.2B.2B_and_Java http://en.wikipedia.org/wiki/Bitwise_operation#Shifts_in_C.2C_C.2B.2B_and_Java

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 http://ideone.com/eB8Kp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM