Write a C program: The left-shift operator can be used to pack two character val
ID: 3606707 • Letter: W
Question
Write a C program: The left-shift operator can be used to pack two character values into a 2-byte unsigned int variable. Write a program that inputs two characters from the keyboard and passes them to function packCharacters. To pack two characters into an unsigned int variable, assign the first character to the unsigned int variable, 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. 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.
Explanation / Answer
unsigned int packing(char ch1, char ch2) {
unsigned int res = ch1;
res = res << 8;
res = res | ch2;
return res;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.