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.
Asked
Active
Viewed 689 times
0
-
What is the `nth byte ... regardless of endian type`? The `nth` most significant? Least significant? `nth` greatest? – twalberg Jul 09 '13 at 16:41
-
Your question title and explanation in question are quit different, As your Question: you have correct answer below by @mocj , You need to explain more. additionally if you need to set any n-continue bits in a number my this answer will help you [set the m-bit to n-bi](http://stackoverflow.com/questions/15917454/set-the-m-bit-to-n-bit?answertab=votes#tab-top) – Grijesh Chauhan Jul 09 '13 at 18:05
1 Answers
3
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.

mocj
- 1,456
- 1
- 9
- 9
-
1`0xff` will be an `int`, that can overflow, you should use `0xFFull` to make it an `unsigned long long`. – Daniel Fischer Jul 09 '13 at 18:26