긴 긴 긴 INT에서 서명되지 않은 짧은 int 4 개를 추출하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/148225

문제

내가 긴 긴 INT가 하나 있고 비트를 가져 와서 서명되지 않은 짧은 INT를 만들고 싶다고 가정 해 봅시다.

특별한 순서는 여기에별로 중요하지 않습니다.

나는 일반적으로 비트를 바꾸고 서명되지 않은 짧은 int의 크기로 잘라야한다는 것을 알고 있습니다. 그러나 나는 어딘가에 이상한 실수를 할 수 있다고 생각합니다.

도움이 되었습니까?

해결책

#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;
}

다른 팁

(unsigned short)((((unsigned long long int)value)>>(x))&(0xFFFF))

어디 value 너의 long long int, 그리고 x 4 개의 반바지의 경우 0, 16, 32 또는 48입니다.

union LongLongIntToThreeUnsignedShorts {
   long long int long_long_int;
   unsigned short int short_ints[sizeof(long long int) / sizeof(short int)];
};

That should do what you are thinking about, without having to mess around with bit shifting.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top