-1

I have seen some bitwise operation practice questions (in C language) and there was one I didn't understand: you had to swap the nth and the mth byte in a specifc integer "x" using only bitwise operations, and that n and m were both <=3. The solution involved masking the last 8 bits of the number resulting from xor based on x >> (n<<3) to x >> (m<<3), but the part I don't understand is why they shifted m/n right 3.

1

Because if n is a position within the word, in bytes, then 8*n is the same position in bits. And n<<3 is the same as 8*n (23 = 8).

With n=1, n<<3 is 8, and (x >> (n << 3)) & 0xff would move the second lowest byte to the bottom position, and mask the rest of the word out. (The bytes would numbered starting from zero, of course.)

(I'm assuming bytes of 8 bits, since I think that's by far the most common case at least nowadays.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.