Discussion

Ask HN: Why is this C code not working as intended?

These two functions

    unsigned int M1ByteMirror(unsigned int iNumber) {
        unsigned int lVal = M1GetN3(iNumber);
        lVal |= M1GetN2(iNumber) << 8;
        lVal |= M1GetN1(iNumber) << 16;
        lVal |= M1GetN0(iNumber) << 24;
        return lVal;
    }

    unsigned int M1ByteMirrorSingle(unsigned int iNumber) {
        unsigned int lVal = (M1GetN3(iNumber) | (M1GetN2(iNumber) << 8) | (M1GetN1(iNumber) << 16) | (M1GetN0(iNumber << 24)));
        return lVal;
    }
are logically the same, yet the output is:

  random data: baa206c, mirror: 6c20aa0b, single: 20aa0b
Why is the compiler/C ignoring the last function call in the single line declaration? Is there a typo/mistake I missed or is there something deeper going on?

EDIT: Or is this just my setup? MinGW32, Win7 x64 Ult SP1, i5 6600K.

EDIT2: I had -O2 on, turned it off, same results, but holy crap, is optimization effective.

2 points klingonopera 7 years ago 4 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton