Question

In: Computer Science

Isolating bits and bytes Task: Create a program that manages an IP address. Allow the user...

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:

  • Bit fields
  • Unions
  • Bit masking
  • IP addressing (for the sake of creating a useful program)

Solutions

Expert Solution

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:


Related Solutions

CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
Create design document for a program that will allow the user: Convert a number to binary....
Create design document for a program that will allow the user: Convert a number to binary. Convert from binary to number. Display the hexadecimal representation of a binary number. Given an hexadecimal display its binary representation. Convert a number to IEEE single precision and vice versa. More to come. PLEASE ADD PSEUDOCODE AND USE C PROGRAMMING USE FUNCTIONS IF POSSIBLE
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
create a program that asks user math questions and keeps track of answers... Python Allow the...
create a program that asks user math questions and keeps track of answers... Python Allow the user to decide whether or not to keep playing after each math challenge. Ensure the user’s answer to each math problem is greater than or equal to zero. Keep track of how many math problems have been asked and how many have been answered correctly. When finished, inform the user how they did by displaying the total number of math problems, the number they...
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments....
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to do the following: 1....
In Kali Linux Write a script that ask the user to enter an IP address and...
In Kali Linux Write a script that ask the user to enter an IP address and a port number, then use the provided entries from the user to perform a query on your local network and determine if the given port is open on the provide network. Need to submit solutions for both below. 1.A short report showing the functionality of your code 2. your bash script
Write a python program that will allow a user to draw by inputting commands. The program...
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing. Must include the following: change attributes: color [red | green | blue] width [value] heading [value] position [xval] [yval] drawing: draw_axes draw_tri [x1] [y1] [x2] [y2] [x3] [y3 draw_rect [x] [y] [b] [h] draw_poly [x] [y] [n] [s] draw_path [path] random random [color | width...
: PHP Please read carefully - display the USER info for IP address, web browser and...
: PHP Please read carefully - display the USER info for IP address, web browser and operating system - do not hard code these parts    1. Create a new PHP document (nit Objective 1)    2. Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1)    3. Assign your name as a string value into the variable labeled myName (Unit Objective 2)    4. Assign...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT