(Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt…

(Packing Characters into an Integer) The
left-shift operator can be used to pack four character values into
a four-byte unsigned int variable. Write a program that inputs four
characters from the keyboard and passes them to function
packCharacters. To pack four characters into an unsigned int
variable, assign the first character to the unsigned intvariable,
shift the unsigned int variable left by 8 bit positions and combine
the unsigned variable with the second character using the bitwise
inclusive OR operator. Repeat this process for the third and fourth
characters. The program should output the characters in their bit
format before and after they’re packed into the unsigned int to
prove that the characters are in fact packed correctly in the
unsigned int variable.
//Program needs to accept character input from keyboard and
store in packCharacters
//Output should be the characters in their bit format before and
after they are packed in to
//the unsigned int to prove they are packed correctly.
#include
unsigned packCharacters(unsigned c1, char c2);
void display(unsigned val);
int main(void)
{
//Define variables
char a;
char b;
char d;
char e;
unsigned result;
unsigned result1;
unsigned result2;
//Prompt user to enter 4 characters
printf(“Enter any four characters:”);
//Read 4 characters
scanf(“%c%c%c%c”,&a, &b, &d, &e);
//display 1st char in bits
printf(“Representation of ‘%c’ in bits as an unsigned integer
is:n”, a);
display(a);
//2nd char in bits
printf(“nRepresentation of ‘%c’ in bits as an unsigned integer
is:n”, b);
display(b);
//3rd char in bits
printf(“nRepresentation of ‘%c’ in bits as an unsigned integer
is:n”, d);
display(d);
//4th char in bits
printf(“nRepresentation of ‘%c’ in bits as an unsigned integer
is:n”, e);
display(e);
unsigned ch = a;
// Call function “packCharacters()” and display resutls
result = packCharacters(ch, b);
result1 = packCharacters(result, d);
result2 = packCharacters(result1, e);
printf(“nRepresentation of ‘%c”%c”%c’ and ‘%c’ packed in
an unsigned integer is:n”, a, b, d, e);
//call the function
display(result2);
return 0;
}
// function to pack 4 characters in an unsigned integer
unsigned packCharacters(unsigned c1, char c2)
{
unsigned pack = c1;
//shift 8 bits to the left
pack