Convert 0x1234 to 0x11223344

前端 未结 13 1156
我在风中等你
我在风中等你 2021-01-30 13:07

How do I expand the hexadecimal number 0x1234 to 0x11223344 in a high-performance way?

unsigned int c = 0x1234, b;
b = (c & 0xff) << 4 | c & 0xf |          


        
13条回答
  •  野性不改
    2021-01-30 13:32

    Perhaps this could be more simpler & efficient.

    unsigned int g = 0x1234;
    unsigned int ans = 0;
    
    ans = ( ( g & 0xf000 ) << 16) + ( (g & 0xf00 ) << 12)
        + ( ( g&0xf0 ) << 8) + ( ( g&0xf ) << 4);
    
    ans  = ( ans | ans>>4 );
    
    printf("%p -> %p\n", g, ans);
    

提交回复
热议问题