Packing & Unpacking the num in Bitwise Operation

I want to convert the 64 bit to 32 bit or vice versa without using memcpy or union. Before jumping to this, please read these posts 

32bit to 64bit masking

Set/Clear/Toggle/Swap bits

32 bit & 31 bit explanation

Endianness of the system

Click here to debug

#include <stdio.h>

/* This function will print the values in hex format */
void conversion(long long int num)
{
    unsigned int a, b;

    printf("input:    %016llx\n", (long long int) num);

    a = ((unsigned int *) &num)[0];
    b = ((unsigned int *) &num)[1];

    printf("unpacked: %08x %08x\n", b, a);

    ((unsigned int *) &num)[0] = a;
    ((unsigned int *) &num)[1] = b;

    printf("repacked: %016llx\n\n", (long long int) num);
}


int main()
{
    unsigned int num = 5;
    long long int masked = 0;
    unsigned int unmasked = 0;
    
    /* Packing */
    masked = ((long long int)num) << 32;
    printf("Masked to long long int %lld\n", masked);
    /* Unpacking */
    unmasked = (masked & 0xFFFFFFFF00000000) >> 32; 
    printf("UnMasked to long long int %d\n", unmasked);
    
    
    conversion(masked);
    
    return 0; 

} 

Output:

Masked to long long int 21474836480
UnMasked to long long int 5
input:    0000000500000000
unpacked: 00000005 00000000
repacked: 0000000500000000

Printing Tricks

Click here to debug
#include <stdio.h>

int main()
{
    long long int masked = 5;
    printf ("0 bit %0x\n", masked);
    printf ("4 bits %1x\n", masked);
    printf ("8 bits %2x\n", masked);
    printf ("12 bits %3x\n", masked);
    printf ("16 bits %4x\n", masked);
    printf ("20 bits %5x\n", masked);
    printf ("24 bits %6x\n", masked);
    printf ("28 bits %7x\n", masked);
    printf ("28 bits %8x\n", masked);
    
    printf ("0 bit with trailing zeros %0x\n", masked);
    printf ("4 bits with trailing zeros %01x\n", masked);
    printf ("8 bits with trailing zeros %02x\n", masked);
    printf ("12 bits with trailing zeros %03x\n", masked);
    printf ("16 bits with trailing zeros %04x\n", masked);
    printf ("20 bits with trailing zeros %05x\n", masked);
    printf ("24 bits with trailing zeros %06x\n", masked);
    printf ("28 bits with trailing zeros %07x\n", masked);
    printf ("32 bits with trailing zeros %08x\n", masked); 
    
    printf ("0 bit with trailing zeros %0llx\n", masked);
    printf ("4 bits with trailing zeros %01llx\n", masked);
    printf ("8 bits with trailing zeros %02llx\n", masked);
    printf ("12 bits with trailing zeros %03llx\n", masked);
    printf ("16 bits with trailing zeros %04llx\n", masked);
    printf ("20 bits with trailing zeros %05llx\n", masked);
    printf ("24 bits with trailing zeros %06llx\n", masked);
    printf ("28 bits with trailing zeros %07llx\n", masked);
    printf ("32 bits with trailing zeros %08llx\n", masked); 
    
    return 0; 
} 

Output:

0 bit 5
4 bits 5
8 bits  5
12 bits   5
16 bits    5
20 bits     5
24 bits      5
28 bits       5
28 bits        5
0 bit with trailing zeros 5
4 bits with trailing zeros 5
8 bits with trailing zeros 05
12 bits with trailing zeros 005
16 bits with trailing zeros 0005
20 bits with trailing zeros 00005
24 bits with trailing zeros 000005
28 bits with trailing zeros 0000005
32 bits with trailing zeros 00000005
0 bit with trailing zeros 5
4 bits with trailing zeros 5
8 bits with trailing zeros 05
12 bits with trailing zeros 005
16 bits with trailing zeros 0005
20 bits with trailing zeros 00005
24 bits with trailing zeros 000005
28 bits with trailing zeros 0000005
32 bits with trailing zeros 00000005






Comments