Search code examples
cbit-manipulationbitwise-operators

Adding a byte to the first byte of an uint32


I'm playing little bit with bitwise operators in c.

I have the following:

uint32 myValue = 0x00ffffff;

I want to add a byte with the value 0x33 to the first byte of myValue to have at the end this:

myValue = 0x33ffffff;

I'm trying to do it with:

myValue = ((myValue & 0xff) << 0) | (0x33u & 0xff);

What I'm doing wrong here?


Solution

  • Try this instead :

    int main() {
        uint32_t myValue = 0x00ffffff;
        myValue = myValue | (0x33u << 24);
        printf("%x\n", myValue); //====> 0x33ffffff
    }