Bitwise macro help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John.Doe.UK@gmail.com

    Bitwise macro help

    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.

  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: Bitwise macro help

    John.Doe.UK@gma il.com wrote:[color=blue]
    > 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[/color]
    [color=blue]
    > 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.[/color]

    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
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • Andrey Tarasevich

      #3
      Re: Bitwise macro help

      Jens.Toerring@p hysik.fu-berlin.de wrote:
      [color=blue]
      > John.Doe.UK@gma il.com wrote:[color=green]
      >> 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[/color]
      >[color=green]
      >> 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.[/color]
      >
      > 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))
      > ...[/color]

      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?

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Andrey Tarasevich

        #4
        Re: Bitwise macro help

        John.Doe.UK@gma il.com wrote:
        [color=blue]
        > 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.
        >[/color]

        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)))

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Jens.Toerring@physik.fu-berlin.de

          #5
          Re: Bitwise macro help

          Andrey Tarasevich <andreytarasevi ch@hotmail.com> wrote:[color=blue]
          > Jens.Toerring@p hysik.fu-berlin.de wrote:[/color]
          [color=blue][color=green]
          >> John.Doe.UK@gma il.com wrote:[color=darkred]
          >>> 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[/color]
          >>[color=darkred]
          >>> 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.[/color]
          >>
          >> 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))
          >> ...[/color][/color]
          [color=blue]
          > 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?[/color]

          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
          --
          \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
          \______________ ____________ http://www.toerring.de

          Comment

          • Peter Nilsson

            #6
            Re: Bitwise macro help

            John.Doe.UK@gma il.com wrote:[color=blue]
            > 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[/color]

            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) ...
            [color=blue]
            > 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.[/color]

            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

            Comment

            Working...