Get 1 byte from a 4 byte variable

I need to get the last byte (bits 24:31 inclusive) from an unsigned int variable into a char variable. I have a variable to store all 32 bits from ecx. According to AMD; the value I want (L1 cache size) is stored in bits 24:31.

How can I put these into another variable? I don't want the rest of the information (TLB stuff) :(
For which endianness?
If you want to do tricky stuff with bytes maintaining the endianness of the computer you are working on, you can make a char pointer pointing to the integer.
If you want to preserve a mathematical meaning, you should use bitwise operations ( >> & )
Just x>>24. If the variable is signed, you'll have to do (x>>24)&0xFF.
Last edited on
Thanks!
Like this:
1
2
3
4
5
6
7
8
9
10
11
    unsigned ecx;

    asm volatile(
        "cpuid\n\t"
        :"=c"(ecx)
        :"a"(0x80000005)
    );

    /* The cache size is now stored in bytes 24:31 of ecx */

    char l1_sz = (ecx >> 24);

?
Last edited on
Topic archived. No new replies allowed.