[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.
Interview Questions and Answers :: Hewlett Packard Enterprise

2. How to find mask is valid or not.

Answer:


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.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");

}



Post Your Answer Here:      Public      Private

Rate This: +2 -0
Report     

Post Your Reply Here:     

Report Error

Report Error

Please Login First Click Here