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

(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 theyre 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: ", a);

display(a);

//2nd char in bits

printf(" Representation of '%c' in bits as an unsigned integer is: ", b);

display(b);

//3rd char in bits

printf(" Representation of '%c' in bits as an unsigned integer is: ", d);

display(d);

//4th char in bits

printf(" Representation of '%c' in bits as an unsigned integer is: ", 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(" Representation of '%c\''%c\''%c\' and '%c\' packed in an unsigned integer is: ", 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 <<= 8;

//using or operator pack c2

pack |= c2;

return pack;

}

void display(unsigned val)

{

//bit counter

unsigned c;

unsigned mask = 1<<31;

printf("%7u = ", val);

//loop through bits

for (c = 1; c <= 32; c++)

{

//shift 1 bit to the left

val & mask ? putchar('1') : putchar('0');

val <<= 1;

if (c % 8 == 0)

{

//print blank space

printf("");

}

}

//print new line character

putchar(' ');

}

how do I get the bits to show in sets of 8 for example : 120 = 00000000 00000000 000000000 01111000

right now mine shows as 120=00000000000000000000000001111000

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!