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 bit unsigned
integer values just use sequential CIN statements The program should output the IP address upon the users
request as any of the following. As a single bit unsigned integer value, or as four bit unsigned integer
values, or as individual bit values which can be requested as a single bit by the user by entering an integer
to Or as all bits assigned into variable sized groups host group and network group and outputted as
unsigned integer values from bit to bits each.
Example functionality:
Enter and IP address:
Scenario :
How would you like to see your IP address single value, four values, two values, a single bit
value:
Output should be
Scenario :
How would you like to see your IP address single value, four values, two values, a single bit
value:
Output should be
Scenario :
How would you like to see your IP address single value, four values, two values, a single bit
value:
How many bits in the network address:
Output should be
Scenario :
How would you like to see your IP address single value, four values, two values, a single bit
value:
Which bit would you like to see:
Output should be
Because the binary of the IP address is
This lab deals with the following concepts:
Bit fields
Unions
Page of
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 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 birthdate
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
birthdate structure type and assign values to its members as follows:
birthdate b;
bday;
bmonth;
byear;
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 eg int bytes short bytes char byte bool byte members
of a structure can be assigned any number of bits as low as bit! For example, to store a day of the month the
maximum number would be which really only requires bits. The month value should only require bits.
The year may require, lets say, up to bits. So you can declare bit fields within a structure as follows:
struct birthdate
int day : ;
int month : ;
int year : ;
;
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 of
unsigned int you;
;
value v;
vyou;
cout
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
