cppdev
cppdev

Reputation: 6973

bitwise operationd to write individual byte of an integer

How do I set the nth byte of an 64 bit unsigned integer regardless of endian type in c ? One of the possible methods I tried is set each bit in a loop.

Upvotes: 0

Views: 693

Answers (1)

mocj
mocj

Reputation: 1486

Assuming n = 0 is the least significant byte, why can't you just do the following:

x |= (0xffull << (n * 8));

If x = 0 and n = 2 this sets x to 0x0ff0000. Unless I am missing something? I don't see what endian-ness has to do with the problem.

Upvotes: 3

Related Questions