TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: All
from: andrew clarke
date: 1996-06-26 21:06:32
subject: optimisation

typedef struct
{
    unsigned long thang;
}
MYSTRUCT;

void raw_to_mystruct(unsigned char *raw, MYSTRUCT * x)
{
    x->thang = 0L;
    x->thang |= raw[3];
    x->thang <<= 8;
    x->thang |= raw[2];
    x->thang <<= 8;
    x->thang |= raw[1];
    x->thang <<= 8;
    x->thang |= raw[0];
}

int main(void)
{
    unsigned char raw[4];
    MYSTRUCT mystruct;
    raw_to_mystruct(raw, &mystruct);
    return 0;
}

Anybody know how to simplify (or more specifically optimise) the code in
the raw_to_mystruct function?  I was thinking along the lines of

  x->thang = (raw[3] << 8) | (raw[2] << 8) | (raw[1]
<< 8) | raw[0];

but am unsure if that's a correct translation.  Thanks. 

--- Msged/2 3.40ac alpha 2
* Origin: Blizzard of Ozz, Melbourne, Australia (3:635/727.4{at}fidonet)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 728 711/401 409 410
SEEN-BY: 711/413 430 808 809 932 934 712/515 713/888 714/906 800/1
@PATH: 635/727 544 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.