Bitwise macro help

J

John.Doe.UK

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

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. Any help would be very much
appreciated.
 
J

Jens.Toerring

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
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. Any help would be very much
appreciated.

I assume you mean that when the value is non non-zero you want to
set the pixel, otherwise reset it. So what about

#define SET_PIX(x,y,v) (array[y][x / 8] |= (v ? 1U : 0) << (7 - x % 8))

Regards, Jens
 
A

Andrey Tarasevich

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
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. Any help would be very much
appreciated.

I assume you mean that when the value is non non-zero you want to
set the pixel, otherwise reset it. So what about

#define SET_PIX(x,y,v) (array[y][x / 8] |= (v ? 1U : 0) << (7 - x % 8))
...

But for zero value of 'v' the right-hand side will evaluate to 0. What's
the point of doing '|=' with 0 as a rhs operand?
 
A

Andrey Tarasevich

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

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. Any help would be very much
appreciated.

One way to do it is

#define SET_PIX(x,y,v) ((v) ?\
array[y][(x) / 8] |= 1 << (7 - (x) % 8) :\
array[y][(x) / 8] &= ~(1 << (7 - (x) % 8)))
 
J

Jens.Toerring

Andrey Tarasevich said:
(e-mail address removed)-berlin.de 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
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. Any help would be very much
appreciated.

I assume you mean that when the value is non non-zero you want to
set the pixel, otherwise reset it. So what about

#define SET_PIX(x,y,v) (array[y][x / 8] |= (v ? 1U : 0) << (7 - x % 8))
...
But for zero value of 'v' the right-hand side will evaluate to 0. What's
the point of doing '|=' with 0 as a rhs operand?

You're right, I forgot about unsetting the bit. So back to the
drawing board...

#define SET_PIX(x,y,v) \
do { if ( v ) \
array[y][x / 8] |= 1U << (7 - x % 8); \
else \
array[y][x / 8] &= ~ (1U << (7 - x % 8)); \
} while ( 0 )

I hope this makes more sense.
Regards, Jens
 
P

Peter Nilsson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top