Shift 'em bits

Nov 19, 2004 21 Replies

For the sake of discussion, could someone comment this seriously non-portable code (works with gcc on intel):

typedef unsigned int thirtytwo; typedef struct { thirtytwo but:31; thirtytwo bit:1; } msbsliced; typedef struct { thirtytwo bit:1; thirtytwo but:31; } lsbsliced; typedef thirtytwo unsliced; typedef union { unsliced all; msbsliced msb; lsbsliced lsb; } sliced; typedef struct { sliced high; sliced low; } sixtyfour;

void shl ( sixtyfour *pdata ) { pdata->high.lsb.but=pdata->high.msb.but; pdata->high.lsb.bit=pdata->low.msb.bit; pdata->low.lsb.but=pdata->low.msb.but; pdata->low.lsb.bit=0; }

While we're writing long delayed follow-ups, let me point out a nitpick: the above absolutely has to be changed to unsigned. Otherwise hell will break loose.

I still think

void lshift64(unsigned long *v) { v[0] = (v[0] > 31); v[1] = v[1] For the sake of discussion, could someone comment this

No. This isn't just seriously non-portable, it's nonsensical. There's nothing to be gained from discussing the finer points of what is, for all important means and purposes, pure voodoo. That routine might just as well be called 'abakadabra'.

Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required