Question: 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

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
Page 2 of 5
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, lets 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;
Page 3 of 5
unsigned int you;
};
value v;
v.you=5;
cout<

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!