a) give you some puzzless:
and
b) give you some ideas of what kind of traps to avoid

Here's the first one.
Relevant part of the task:
I needed to replace some bytes from a number. During the runtime, I would get a number type of unsigned int. Now I do not know what the number would be like. I'll mark that number as 0xXXXXXXXX. Now, I should maintain all other bytes as they are, but change specified bytes to be something else. One example could be that I should change third and fourth byte to be AB. Now resulting number would be 0xXXABXXXX. Simple task I said...
I decided to do a function, which takes following parameters:
1. original number.
2. Bytes which should be changed
4. What to change the bytes.
Now in example abowe, I would give parameters
1. 0xXXXXXXXX
2. 0x00FF0000 //change bits forming 2nd and 3rd bytes
3. 0x00AB0000
Then I decided to use bitwise operations to form the correct result. I wrote following function:
Code: Select all
unsigned int changebytes(unsigned int const &real, unsigned int const &changedbits, unsigned int &changeto)
{
unsigned int tmp,tmp2;
tmp=(changedbits^(TAaSysComSicad)0x11111111); //get the bits which are not changed (bit's value will be inverted when XORed with bit 1)
tmp2=(real & tmp); //null the bits which will be changed. (and will set result to zero, if one of the bits 'anded' is zero).
return (tmp2 | changeto); //add changeto bits in the tmp (OR will 'sum' the bytes.
}