21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Solution<br />

Merging multiple scalar values into a single, larger scalar value can make simple,<br />

unrelated values appear to be a large value or bit field. Two 8-bit values can be<br />

merged into a single 16-bit value, and two 16-bit values can be merged into a single<br />

32-bit value.<br />

Discussion<br />

Merging scalar variables is a light obfuscation. When used in a loop, a debugger can<br />

set a watch on the counter variable and make obvious the fact that the upper or<br />

lower half of the variable is being incremented with each iteration of the loop.<br />

The following macros merge two char values into a single short value, and two short<br />

values into a single int value. This is accomplished by shifting the shorter values into<br />

the larger value that contains them, and by masking half of the larger value and shifting<br />

as appropriate to retrieve the shorter value.<br />

/* x and y are chars, returns a short */<br />

/* x is in position 0, y is in position 1 */<br />

#define MERGE_CHAR(x, y) (((y) > (8 * (c))) & 0x00FF)<br />

/* s is a short, c is a position, and val is a char value */<br />

#define SET_CHAR(s, c, val) (((s) & (0xFF00 >> (8 * (c)))) | ((val) (16 * (s))) & 0x0FFFF)<br />

/* i is an int, s is position, and val is a short value */<br />

#define SET_SHORT(i, s, val) (((i) & (0xFFFF0000 >> (16 * (s)))) | \<br />

((val)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!