Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract four unsigned short ints from one long long int?

Suppose I have one long long int and want to take its bits and construct four unsigned short ints out of it.

Particular order doesn't matter much here.

I generally know that I need to shift bits and truncate to the size of unsigned short int. But I think I may make some weird mistake somewhere, so I ask.

like image 563
Paweł Hajdan Avatar asked Sep 29 '08 10:09

Paweł Hajdan


1 Answers

#include <stdint.h>
#include <stdio.h>

union ui64 {
    uint64_t one;
    uint16_t four[4];
};

int
main()
{
    union ui64 number = {0x123456789abcdef0};
    printf("%x %x %x %x\n", number.four[0], number.four[1],
                            number.four[2], number.four[3]);
    return 0;
}
like image 167
Chris Jester-Young Avatar answered Nov 13 '22 09:11

Chris Jester-Young