0

I have this function called byte swap I am supposed to implement. The idea is that the function takes 3 integers (int x, int y, int z) and the function will swap the y and z bytes of the int x. The restrictions are pretty much limited to bit wise operations (no loops, and no if statements or logical operators such as ==).

I don't believe that I presented this problem adequately so Im going to re attempt

I now understand that

byte 1 is referring to bits 0-7 byte 2 is referring to bits 8-15 byte 3 16-23 byte 4 24-31 

My function is supposed to take 3 integer inputs, x, y and z. The y byte and z byte on the x then would have to get switched

int byteSwap(int x, int y, int z) 

ex of the working function

byteSwap(0x12345678, 1, 3) = 0x56341278 byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD 

My original code had some huge errors in it, namely the fact that I was considering a byte to be 2 bits instead of 8. The main problem that I'm struggling with is that I do not know how to access the bits inside of the given byte. For example, when I'm given byte 4 and 5, how do I access their respected bits? As far as I can tell I can't find a mathematical relationship between the given byte, and its starting bit. I'm assuming I have to shift and then mask, and save those to variables.Though I cannot even get that far.

4
  • 3
    It's unclear to me how exactly is it supposed to work. It'd be useful if you posted 1-2 examples of x, y and z meeting your criteria. Commented Jan 28, 2015 at 22:55
  • Casting on the left side of an = operator is usually a sign you're doing something wrong. Commented Jan 28, 2015 at 22:59
  • added edits to show examples on how it is supposed to work. Hopefully everything is more clear now, sorry for the confusion Commented Jan 28, 2015 at 23:10
  • Um, bytes have 8 bits each, not 2. Where did that idea come from? Commented Jan 28, 2015 at 23:27

2 Answers 2

1

Extract the ith byte by using ((1ll << ((i + 1) * 8)) - 1) >> (i * 8). Swap using the XOR operator, and put the swapped bytes in their places.

int x, y, z; y = 1, z = 3; x = 0x12345678; int a, b; /* bytes to swap */ a = (x & ((1ll << ((y + 1) * 8)) - 1)) >> (y * 8); b = (x & ((1ll << ((z + 1) * 8)) - 1)) >> (z * 8); /* swap */ a = a ^ b; b = a ^ b; a = a ^ b; /* put zeros in bytes to swap */ x = x & (~((0xff << (y * 8)))); x = x & (~((0xff << (z * 8)))); /* put new bytes in place */ x = x | (a << (y * 8)); x = x | (b << (z * 8)); 
Sign up to request clarification or add additional context in comments.

Comments

0

When you say the 'the y and z bytes of x' this implies x is an array of bytes, not an integer. If so:

x[z] ^= x[y]; x[y] ^= x[z]; x[z] ^= x[y]; 

will do the trick, by swapping x[y] and x[z]

After your edit, it appears you want to swap individual bytes of a 32 bit integer:

On a little-endian machine:

int swapbytes (int x, int y, int z) { char *b = (char *)&x; b[z] ^= b[y]; b[y] ^= b[z]; b[z] ^= b[y]; return x; } 

On a big-endian machine:

int swapbytes (int x, int y, int z) { char *b = (char *)&x; b[3-z] ^= b[3-y]; b[3-y] ^= b[3-z]; b[3-z] ^= b[3-y]; return x; } 

With a strict interpretation of the rules, you don't even need the xor trick:

int swapbytes (int x, int y, int z) { char *b = (char *)&x; char tmp = b[z]; b[z] = b[y]; b[y] = tmp; return x; } 

On a big-endian machine:

int swapbytes (int x, int y, int z) { char *b = (char *)&x; char tmp = b[3-z]; b[3-z] = b[3-y]; b[3-y] = tmp; return x; } 

If you want to do it using bit shifts (note <<3 multiplies by 8):

int swapbytes (unsigned int x, int y, int z) { unsigned int masky = 0xff << (y<<3); unsigned int maskz = 0xff << (z<<3); unsigned int origy = (x & masky) >> (y<<3); unsigned int origz = (x & maskz) >> (z<<3); return (x & ~masky & ~maskz) | (origz << (y<<3)) | (origy << (z<<3)); } 

2 Comments

X is supposed to be an integer. I added some edits to hopefully make it more clear, I apologize for the confusion
... and, dear downvoters, the problem here is precisely what? Yes, it's artificial, but so is the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.