Re: union to get parts of integer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Tim Walberg wrote:

> >>	((char *)&i)[0].
> >>	
> >>	However, are you sure that you need this?  Don't you need "(i & 255)",
> >>	"((i >> 8) & 255)", etc. instead?
> 
> It's likely that either the union or direct pointer expressions yield
> more efficient code, as they can probably be compiled to direct byte-width
> load/store instructions, rather than shifts and logical ands... However,
> if the code's not in a critical path for performance, it probably won't
> matter. And on some architectures, there may not be byte-width operations,
> I guess...

The cast/union versions will give different results depending upon the
CPU's byte order. The shift/mask versions will give the same results
regardless of byte order.

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux