'How to show desired packed values in the output in a packCharacters function program?

I have this code for an assignment in which I pack four hard-coded character values and pass them to the packCharacters function. I believe however that I am packing them incorrectly as I am not getting the desired output for each variable.

#include <stdio.h>

unsigned packCharacters(unsigned c1, char c2);
void display(unsigned val);

int main (void){
    char a; char b; char d; char e;
    unsigned result; unsigned result1; unsigned result2;
    
    printf("Enter the first character :");
    scanf("%c", &a);
    getchar();
    
    printf("Enter the second character :");
    scanf("%c", &b);
    getchar();
    
    printf("Enter the third character :");
    scanf("%c", &d);
    getchar();
    
    printf("Enter the fourth character :");
    scanf("%c", &e);
    getchar();
    
    printf("\nResult before shifting and replacing last 8 bits with K\n");
    display(a);
    printf("\n\nResult after shifting and replacing last 8 bits with K\n");
    display(a);
    printf("\n\n\nResult before shifting and replacing last 8 bits with L\n");
    display(b);
    printf("\n\nResult after shifting and replacing last 8 bits with L\n");
    display(b);
    printf("\n\n\nResult before shifting and replacing last 8 bits with M\n");
    display(d);
    printf("\n\nResult before shifting and replacing last 8 bits with M\n");
    display(d);
    printf("\n\n\nResult before shifting and replacing last 8 bits with N\n");
    display(e);
    
        unsigned ch = a;
        result = packCharacters(ch, b);
        result1 = packCharacters(result, d);
        result2 = packCharacters(result, e);
        printf("\n\nResult after shifting and replacing last 8 bits with N\n");
        display(result2);
        getchar();
        return 0;
}
    unsigned packCharacters(unsigned c1, char c2){
        unsigned pack = c1;
        pack <<= 8;
        pack |= c2;
        return pack;
    }
    
    void display(unsigned val){
        unsigned c;
        unsigned mask = 1 << 31;
        printf("%7u = ", val);
        
        for (c = 1; c <= 32; c++){
            val & mask ? putchar('1') : putchar('0');
            val <<= 1;
            if (c % 8 == 0){
                printf(" ");
            }
        }
    }

this program outputs this:

Enter the first character :K
Enter the second character :L
Enter the third character :M
Enter the fourth character :N

Result before shifting and replacing last 8 bits with K
     75 = 00000000 00000000 00000000 01001011

Result after shifting and replacing last 8 bits with K
     75 = 00000000 00000000 00000000 01001011


Result before shifting and replacing last 8 bits with L
     76 = 00000000 00000000 00000000 01001100

Result after shifting and replacing last 8 bits with L
     76 = 00000000 00000000 00000000 01001100


Result before shifting and replacing last 8 bits with M
     77 = 00000000 00000000 00000000 01001101

Result before shifting and replacing last 8 bits with M
     77 = 00000000 00000000 00000000 01001101


Result before shifting and replacing last 8 bits with N
     78 = 00000000 00000000 00000000 01001110

Result after shifting and replacing last 8 bits with N
4934734 = 00000000 01001011 01001100 01001110

But I would like it to output this:

Enter the first character :K
Enter the second character :L
Enter the third character :M
Enter the fourth character :N
    
Result before shifting and replacing last 8 bits with K
     0 = 00000000 00000000 00000000 00000000
    
Result after shifting and replacing last 8 bits with K
     75 = 00000000 00000000 00000000 01001011
    
    
Result before shifting and replacing last 8 bits with L
     75 = 00000000 00000000 00000000 01001011
    
Result after shifting and replacing last 8 bits with L
     19276 = 00000000 00000000 01001011 01001100
    
    
Result before shifting and replacing last 8 bits with M
     19276 = 00000000 00000000 01001011 01001100
    
Result before shifting and replacing last 8 bits with M
     4934733 = 00000000 01001011 01001100 01001101
    
    
Result before shifting and replacing last 8 bits with N
     4934733 = 00000000 01001011 01001100 01001101
    
Result after shifting and replacing last 8 bits with N
1263291726 = 01001011 01001100 01001101 01001110

How would I go about getting the wanted output?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source