Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

One 32bit Long into 4 8bit Chars

Name: Anonymous 2010-06-28 7:43

In C code

I'm gonna ask a stupid question here, but I'm not sure I trust my code so I want to hear if you can tell me what's wrong with my approach

Right now, the code goes

long data

char data0
char data1
char data2
char data3

data0 = data;
data1 = data >> 8;
data2 = data >> 16;
data3 = data >> 24;

but I'm not sure that's the best approach, or even a good approach. It works, which is most important obviously, but I have this nagging feeling that there's something very not Best Practices about this approach and I can't shake that feeling.

Name: Anonymous 2010-06-28 13:48

>>24,18
What's your point? That >>1 had the right way all along? Or that >>2 depends on endianness? The number doesn't `look like' anything until you have a look at it in memory (OH SHIT! THAT'S EXACTLY NOT WHAT YOU SAID.) or printf it, in which case it looks like a number does to a human.
I can tell you that ``people'' are indeed not ``going to suddenly use a different endiant for storing data in longs'', because it's not people who decide that shit. The processor's endianness decides that for the compiler.
``Left shift'' and ``right shift'' are platform-independent, because they don't actually shift left or right, but towards the least or most significant bits respectively.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List