Interview Questions and Answers :: Hewlett Packard Enterprise
Home > Experience Archives > Hewlett Packard Enterprise > Interview Question Set 1 > Discussion
2. How to find mask is valid or not.
Answer:
Basically a valid sub-net mask, when written in binary, has to consist
Basically a valid sub-net mask, when written in binary, has to consist
of only consecutive 1's and then 0's, but no intermittent mixing. I.e.:
255.255.255.128 -> 11111111.11111111.11111111.10000000 is valid
255.255.255.0 -> 11111111.11111111.11111111.00000000 is valid
255.255.255.0 -> 11111111.11111111.11111111.00000000 is valid
255.255.255.144 -> 11111111.11111111.11111111.10010000 is not valid <<<<<<<<
#include <stdio.h>
int main()
{
char ip[4] = {255,255,255,128};
int count = 0, valid = 0,i,check,j;
for (j=3;j>=0;j--) {
count = 0;
if (valid)
check++;
for (i = 0;i < 8;i++)
{
if ((ip[j] & 1<<i) && count !=1)
{
count = 1;
}
if ((count ==1) && !(ip[j] & 1<<i))
valid = 1;
}
}
if (valid)
printf("Invalid subnet");
else
printf("Valid subnet");
}
Amit
10 Aug, 2018 5:45 PM
Basically a valid sub-net mask, when written in binary, has to consist
of only consecutive 1's and then 0's, but no intermittent mixing. I.e.:
255.255.255.128 -> 11111111.11111111.11111111.10000000 is valid
255.255.255.0 -> 11111111.11111111.11111111.00000000 is valid
255.255.255.0 -> 11111111.11111111.11111111.00000000 is valid
255.255.255.144 -> 11111111.11111111.11111111.10010000 is not valid <<<<<<<<
#include <stdio.h>
int main()
{
char ip[4] = {255,255,255,128};
int count = 0, valid = 0,i,check,j;
for (j=3;j>=0;j--) {
count = 0;
if (valid)
check++;
for (i = 0;i < 8;i++)
{
if ((ip[j] & 1<<i) && count !=1)
{
count = 1;
}
if ((count ==1) && !(ip[j] & 1<<i))
valid = 1;
}
}
if (valid)
printf("Invalid subnet");
else
printf("Valid subnet");
}
Report Error
Report Error
Please Login First Click Here