Task: Create a program that manages an IP address as a single 32 bit unsigned integer value, or as 4 8 bit unsigned integer values, or as 32 individual bit values which can be assigned into 2 variable...

1 answer below »
Task: Create a program that manages an IP address as a single 32 bit unsigned integer value, or as 4 8 bit unsigned integer values, or as 32 individual bit values which can be assigned into 2 variable sized groups (host group and network group) and outpu
Page 4 of 4
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 XXXXXXXXXX
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 XXXXXXXXXX
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 110000001010 XXXXXXXXXX
This lab deals with the following concepts:
· Bit fields
· Unions
· Bit masking
· IP addressing (for the sake of creating a useful program)
Bit Fields
The smallest memory size of any built in C data type is 1 byte. Even the Boolean data type requires one byte of memory. But C++ allows for a special type of variable field called a bit field, which can only be declared in a structure. A structure is a “data structure” which stores one to many consecutive values on any data type. For example, the following structure could be used to store a birth date, rather than just using three separate variables:
struct birth_date {
int day;
int month;
int year;
};
So structures are kind of like classes with no methods or constructors. You would declare a variable of the birth_date structure type and assign values to its members as follows:
birth_date b;
b.day=5;
b.month=12;
b.year=1973;
Members of a structure can have an extra parameter in there declaration and become bit fields. Rather than just using standard byte offsets for datatypes (e.g.,, int(4 bytes), short(2 bytes), char(1 byte), bool(1 byte)), members of a structure can be assigned any number of bits (as low as 1 bit!). For example, to store a day of the month the maximum number would be 31, which really only requires 5 bits. The month value should only require 4 bits. The year may require, let’s say, up to 16 bits. So you can declare “bit fields” within a structure as follows:
struct birth_date {
int day : 5;
int month : 4;
int year : 16;
};
The structure works the same way as before.
Unions
Unions are one of the most useful features of C++, especially to an engineer (we are in a computer engineering class). Unions allow the same memory space to be shared by multiple variables (NOT like pointers). Yes, you could do something simple like this:
union value {
unsigned int me;
unsigned int you;
};
value v;
v.you=5;
cout<In that example, me and you are basically the same thing. Just two different ways of referencing the same memory space and the same value. The cout statement would print 5. I suppose that could be useful, but the real use comes when you use different sized sequential datatypes in a union (this requires a structure for at least one union member):
struct youandme {
unsigned short you;
unsigned short me;
};
union value {
youandme you_me;
unsigned int us;
};
The union value is a 32 bit field. It can be referenced by a single 32 bit value called us, or by its two 16 bit halves called you (the 16 least significant bits) and me (the 16 most significant bits). The following sets the whole 32 bit value to 5:
value v;
v.us = 5;
If you look at the binary value of v it is 000000000000 XXXXXXXXXX
Now if you perform:
v.you_me.me = 5;
You have just changed the high order 16 bits to 5. So if you look at the binary value of v it is 000000000000 XXXXXXXXXX
So the value of v has changed to XXXXXXXXXXPretty cool right? You can also rewrite this structure using bit fields:
struct youandme {
unsigned int you : 16;
unsigned int me : 16;
};
union value {
youandme you_me;
unsigned int us;
};
Bit Masks
Unfortunately, you cannot use arrays of bit fields in a union, so if you wanted to reference all bits individually, you would need to do so with static bit fields:
struct bits {
unsigned int a1 : 1;
unsigned int a2 : 1;
...
unsigned int a32 : 1;
};
So if you want to reference each individual bit of a number, just create a mask for each bit and do a little math to get the bit. I will let you figure out your own way to do that.
IP Addressing
In an IP network, each host is assigned a 32 bit address. Typically, the address is written as 4 consecutive 1 byte unsigned integers ranging from 0 to 255 each. For example:
XXXXXXXXXX
There are many other useful ways to see this number though. For example, as a single 32 bit number. Another useful way to see the address is split into two numbers, a network number an host number, where some high order bits are assigned to the network, and the low order bits are assigned to the host. This assignment is based on a subnet mask. The mask is a sequence of 1’s followed by 0’s. The mask is typically represented in the same 4 part way as an IP address. For example, the subnet mask:
XXXXXXXXXX
If we look at this address in binary it is 111111111111 XXXXXXXXXX. This means that the high order 16 bits belong to the network address and the low order 16 bits (the zeros) belong to the host address. So for our example address of XXXXXXXXXX, the network address is XXXXXXXXXXand the host address is 1.5. This could also be expressed as two 16 bit integer values:
Network = XXXXXXXXXX = XXXXXXXXXX = 49320
Host = 1.5 = XXXXXXXXXX = 261
All this information is useful. Can you think of how you could use bit fields, structures and unions to make an IP address viewable as all of the following simultaneously:
· Four 1 byte integers
· One 32 bit integer
· Two integers split based on subnet mask
· Isolate any single bit from the 32 bit value
Answered 1 days AfterMay 05, 2021

Solution

Pulkit Aggarwal answered on May 06 2021
24 Votes

83086c++/lab-unions.doc
Page 4 of 4
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...

Submit New Assignment

Copy and Paste Your Assignment Here