In: Computer Science
Isolating bits and bytes
Task: Create a program that manages an IP address. Allow the user to enter the IP address as four 8 bit unsigned integer values (just use 4 sequential CIN statements). The program should output the IP address upon the users request as any of the following. As a single 32 bit unsigned integer value, or as four 8 bit unsigned integer values, or as 32 individual bit values which can be requested as a single bit by the user (by entering an integer 0 to 31). Or as all 32 bits assigned into 2 variable sized groups (host group and network group) and outputted as 2 unsigned integer values from 1 bit to 31 bits each.
Example functionality:
Enter and IP address:
192
168
1
5
Scenario 1:
How would you like to see your IP address ((1) single value, (2) four values, (3), two values, (4) a single bit value): 1
Output should be 3232235781
Scenario 2:
How would you like to see your IP address ((1) single value, (2) four values, (3), two values, (4) a single bit value): 2
Output should be 192.168.1.5
Scenario 3:
How would you like to see your IP address ((1) single value, (2) four values, (3), two values, (4) a single bit value): 3
How many bits in the network address: 16
Output should be 49320, 261
Scenario 4:
How would you like to see your IP address ((1) single value, (2) four values, (3), two values, (4) a single bit value): 4
Which bit would you like to see: 21
Output should be 1
(Because the binary of the IP address is 11000000101010000000000100000101
This lab deals with the following concepts:
Below is your code :)
#include <iostream>
using namespace std;
int ip[32]; // array to store the binary representation
of whole ip addresss
// function to return the decimal of whole ip address
long long BinaryToDecimal()
{
long long decimalNumber = 0;
long long base = 1;
int i=31;
while (i>=0)
{
decimalNumber += ip[i]*base;
base = base*2;
i--;
}
return decimalNumber;
}
// function to return the decimal of given part of whole ip
address
long long TwoPartAddress(int end,int bits)
{
long long decimalNumber = 0;
long long base = 1;
int i=bits;
while (i>=end)
{
decimalNumber += ip[i]*base;
base = base*2;
i--;
}
return decimalNumber;
}
// main function
int main()
{
int num[4]; // array to store 4
parts of ip address
int n[4][8]; // 2D array to store binary representation
of 4 parts of ip address
int choice; // for storing user
choice
// taking input from user
cout<<"Enter an IP address: ";
cin>>num[0]>>num[1]>>num[2]>>num[3];
// filling 2d array with 0
for(int i=0;i<4;i++)
for(int j=0;j<8;j++)
n[i][j]=0;
// filling binary representation of each number in 2d array
for(int j=0;j<4;j++)
{
int octate = num[j];
for(int i=0; octate>0; i++)
{
n[j][i]=octate%2;
octate = octate/2;
}
}
int i=0;
// filling ip array with combined binary values
for(int j=0;j<4;j++)
{
for(int k=7;k>=0;k--)
ip[i++] = n[j][k];
}
cout<<"\nHow would you like to see your IP address ((1)
single value, (2) four values, (3), two values, (4) a single bit
value): ";
cin>>choice;
cout<<endl;
switch(choice)
{
// in case user want to see the single decimal value
of ip address
case 1: cout<<"Output should be
"<<BinaryToDecimal()<<endl;
break;
// in case user want to see ip address in 4 decimal
values
case 2: cout<<"Output should be
"<<num[0]<<"."<<num[1]<<"."<<num[2]<<"."<<num[3]<<endl;
break;
// in case user want to see network and host values
seperately in decimal form
case 3: int bits;
cout<<"How
many bits in the network address: ";
cin>>bits;
cout<<"Output should be
"<<TwoPartAddress(0,bits-1)<<",
"<<TwoPartAddress(bits,31)<<endl;
break;
// in case user want to see a particular bit of an ip
address
case 4: int bit;
cout<<"Which bit would you like to see: ";
cin>>bit;
cout<<"Output should be
"<<ip[31-bit]<<endl;
cout<<"(Because the binary of the IP address
is"<<endl;
for(int
i=0;i<31;i++)
{
if(i == 31-bit)
{
cout<<"
"<<ip[i]<<" ";
continue;
}
cout<<ip[i];
}
break;
default: cout<<"Wrong
choice."<<endl;
}
return 0;
}
Sample Output:
1. Scenario :
2. Scenario:
3. Scenario:
4. Scenario: