I'm having difficulty in the following exercise
Using the right shift operator, the AND operator on bits, and a mask, write a function called uncompressingCharacters that receives the integer unsigned and unzip it into two characters from an unsigned integer, combine the unsigned integer with the 65280 mask (00000000 00000000 11111111 00000000) and shift the result in bits to the right. Assign the resulting value to a char. Then, merge the unsigned integer with the 255 mask (00000000 00000000 00000000 11111111). Assign the result to another char variable. The program should print the integer unsigned in bits before it is uncompressed and then print the characters in bits to confirm that they have been uncompressed correctly. :
The first character I can unpack, but the second one does not have the correct value (it is the same as the first uncompressed character).
Next I put the functions that I am using to try to solve the exercise
#include <stdio.h>
#include <stdlib.h>
void compactaCaracteres(char a,char b);
void descompactaCaracteres(unsigned valor);
void mostrarBits(unsigned valor);
int main(void){
char var1,var2;
printf("Digite um caractere:");
scanf("%c",&var1);
setbuf(stdin,NULL);
printf("Digite um caractere:");
scanf("%c",&var2);
compactaCaracteres(var1,var2);
return 0;
}
void compactaCaracteres(char a,char b){
unsigned compacta = a;
compacta <<= 8;
compacta |= b;
descompactaCaracteres(compacta);
}
void descompactaCaracteres(unsigned valor){
mostrarBits(valor);
valor &= 65280;
valor >>= 8;
char a = valor;
mostrarBits(a);
char b = valor & 255;
mostrarBits(b);
}
// Função utilizada para imprimir os bits
void mostrarBits(unsigned valor){
unsigned contador;
unsigned mascara = 1 << 31;
printf("%10u = ",valor);
for(contador = 1 ; contador <= 32; contador++){
putchar(valor & mascara ? '1' : '0');
valor <<= 1;
if(contador % 8 == 0){
putchar(' ');
}
}
putchar('\n');
}