C preprocessor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • selvesteen@hotmail.com

    #1

    C preprocessor

    Hello All,

    I came across the following macro in a program, could someone explain
    what it actually means.

    #define BUFFER_MAX_HPN_ LEN (2>>21)-1

    Is there any other way to achieve the same

    Thanks for the help.
    Mike

  • Chris Dollin

    #2
    Re: C preprocessor

    selvesteen@hotm ail.com wrote:
    [color=blue]
    > Hello All,
    >
    > I came across the following macro in a program, could someone explain
    > what it actually means.
    >
    > #define BUFFER_MAX_HPN_ LEN (2>>21)-1[/color]

    It means that wherever BUFFER_MAX_HPN_ LEN appears in the
    following text, it is replaced by `(2>>21)-1`, which looks
    like a typo on someone's part to me, since that evaluates
    to -1. Are you sure it wasn't `(2<<21)-1`?

    The latter would be a way of writing a mask of 1's from
    position 21ish rightwards.

    <fx:thinks/>
    <fx:assume32Bit Ints>

    2 << 21 = 1 << 22 = 1 followed by 22 0's; -1 then gives
    us a mask of 21 ones, which is probably why the macro
    would be written using 2<<, as the right operand of <<
    would be the number of 1's in the mask.

    I'd write `((2 << 21) - 1)` myself.

    </>

    --
    Chris "electric hedgehog" Dollin
    It's called *extreme* programming, not *stupid* programming.

    Comment

    • pete

      #3
      Re: C preprocessor

      selvesteen@hotm ail.com wrote:[color=blue]
      >
      > Hello All,
      >
      > I came across the following macro in a program, could someone explain
      > what it actually means.
      >
      > #define BUFFER_MAX_HPN_ LEN (2>>21)-1
      >
      > Is there any other way to achieve the same[/color]

      It looks more like a typographical error, than anything else.

      BUFFER_MAX_HPN_ LEN * 5 is -5

      5 * BUFFER_MAX_HPN_ LEN is -1



      /* BEGIN new.c */

      #include <stdio.h>

      #define BUFFER_MAX_HPN_ LEN (2>>21)-1

      int main(void)
      {
      printf("\nBUFFE R_MAX_HPN_LEN * 5 is %d\n\n",
      BUFFER_MAX_HPN_ LEN * 5);
      printf("5 * BUFFER_MAX_HPN_ LEN is %d\n\n",
      5 * BUFFER_MAX_HPN_ LEN);
      return 0;
      }

      /* END new.c */

      --
      pete

      Comment

      • CBFalconer

        #4
        Re: C preprocessor

        Chris Dollin wrote:[color=blue]
        >[/color]
        .... snip ...[color=blue]
        >
        > 2 << 21 = 1 << 22 = 1 followed by 22 0's; -1 then gives
        > us a mask of 21 ones, which is probably why the macro
        > would be written using 2<<, as the right operand of <<
        > would be the number of 1's in the mask.
        >
        > I'd write `((2 << 21) - 1)` myself.[/color]

        <nit> or '((2uL << 21) -1)' so it works on 16 bitters. </nit>

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson


        Comment

        • snnn

          #5
          Re: C preprocessor

          selvesteen@hotm ail.com wrote:[color=blue]
          > Hello All,
          >
          > I came across the following macro in a program, could someone explain
          > what it actually means.
          >
          > #define BUFFER_MAX_HPN_ LEN (2>>21)-1
          >
          > Is there any other way to achieve the same
          >
          > Thanks for the help.
          > Mike
          >[/color]

          How should the following codes work?

          int x;
          x=5*BUFFER_MAX_ HPN_LEN
          x=BUFFER_MAX_HP N_LEN *5


          please define "BUFFER_MAX_HPN _LEN" as "((2>>21)-1)" instead.

          Comment

          • pete

            #6
            Re: C preprocessor

            snnn wrote:
            [color=blue]
            > please define "BUFFER_MAX_HPN _LEN" as "((2>>21)-1)" instead.[/color]

            That's a strange way to write negative one.

            --
            pete

            Comment

            Working...