So when I try to AND the octetsIPBits and octetsMaskBits why am I getting the wrong number netID? - I might be push_back functioning wrong on the original octetsIPBits and octetsMaskBits vectors (in the code below this post)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
vector<int> netID;
for (int j=0; j < octetsIP.size(); j++)
{
if (j>0)
cout << ".";
for (int i=0; i < 8; i++){
netID.push_back(octetsIPBits[i] & octetsMaskBits[i]);
cout << netID[j];
}
}
cout << " : Network ID" << endl;
Please enter four octets in dot notation.
Enter IPv4 Address to be subnetted -> 192.168.0.1
IP in Range: YES
IP Class: Private block, Class 'C'
Please enter four octets in dot notation.
Enter subnet mask for IP address 192.168.0.1 -> 255.255.255.0
Mask in Range: YES
11000000.10101000.00000000.00000001 : IP Address
11111111.11111111.11111111.00000000 : Subnet Address
11111111.11111111.00000000.00000000 : Network ID // THIS IS WRONG
Also once this is done. How do I convert these back to decimals??? - I'm in over my head with this program but I'm determined to finish it and learn from trial.
The way i see the problem, conversion to binary is purely for human-legibility, it helps us to see and understand the data. But the computer is already working in binary, so it can handle that data just fine as it is.
// Perform ANDing of IP and Subnet Mask to generate Network ID //
vector<int> netID;
for (int j=0; j < octetsIP.size(); j++)
{
if (j>0)
cout << ".";
for (int i=0; i < 8; i++){
netID.push_back(octetsIPBits[j] & octetsMaskBits[j]);
cout << netID[j];
}
}
with this:
1 2 3 4 5 6 7 8 9
// Perform ANDing of IP and Subnet Mask to generate Network ID //
vector<int> netID;
for (int j=0; j < octetsIPBits.size(); j++)
{
if ((j > 0) && (j%8 == 0))
cout << ".";
netID.push_back(octetsIPBits[j] & octetsMaskBits[j]);
cout << netID[j];
}
The first version is accessing bit number [j], where j is a value from 0 to 3. That is, it uses the first four bits only, (and repeats the same identical value 8 times).
The results for the conversion of the dotted notation of the given IP can be verified at http://www.ip-details.com/ip-search/ . Where it provides the various dotted Binary, Octal, Decimal, Hexadecimal notations for the given IP. Adding to that IP location, ISP, Country, Latitude & Longitude also can be viewed.