Jo*********@gmail.com wrote:
I have a macro which gets a bit from an unsigned char in a 2d array.
#define GET_PIX(x,y) (array[y][x / 8] >> (7 - x % 8)) & 1
You should 'protect' x and the whole expression with ( ) to avoid
problems, e.g. GET_PIX(1+2,y), if (GET_PIX(x,y) != 0) ...
I now need to work out a macro to set a pix to a value of 1 or 0
depending on what number I pass it.
This is all dealt with in the FAQ, but in what way is it
dependant? If b below is either 0 or 1...
#define SET_PIX(x,y,b) \
( array[y][(x)/8] |= (b) << (7 - (x) % 8 )
If x is required to be non-negative (likely), then...
#define SET_PIX(x,y,b) \
( array[y][(x) >> 3] |= (b) << (7 - ((x) & 7)) )
....may avoid compilation to innefficient code in the case
where x is a signed variable.
The behaviour of x % 8 and x & 7 is _not_ necessarily the
same if x is negative, but compilers are _required_ to
implement the correct semantics for the code supplied.
--
Peter