题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
http://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
位运算运用:
int main(){
int s[4] = {0};
int i = 0;
unsigned int sum = 0;
unsigned int digt = 0;
for(i = 0; i < 4; i++){
scanf("%d.", s+i);
sum += (s[i] << (3-i)*8);
}
printf("%ld\n", sum);
scanf("%ld", &digt);
for(i = 0; i < 4; i++){
if (i == 3){
printf("%d\n", digt >> (3-i)*8 & 0xFF);
}else{
printf("%d.", digt >> (3-i)*8 & 0xFF);
}
}
return 0;
}