Am I an idiot?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Grigorenko

    Am I an idiot?

    That's a rethorical question. But shouldn't this statement store the value
    of three in the variable a:

    #include <iostream>

    int main()
    {
    int a = 1 & 2;
    if(a & 1)
    {
    std::cout << "One " << std::endl;
    }
    if(a & 2)
    {
    std::cout << "Two " << std::endl;
    }
    return 0;
    }

    I'm trying to use bit-masking to pass around flags with an integer, and then
    check them with if( integer & flagX )

    What is going on?

    Thanks a lot for your time,
    Kevin Grigorenko


  • Karl Heinz Buchegger

    #2
    Re: Am I an idiot?

    Kevin Grigorenko wrote:[color=blue]
    >
    > That's a rethorical question. But shouldn't this statement store the value
    > of three in the variable a:[/color]

    No. It should store the value 0.
    [color=blue]
    >
    > #include <iostream>
    >
    > int main()
    > {
    > int a = 1 & 2;[/color]

    You want

    int a = 1 | 2;


    Use | to set bits, use & to clear bits or to check if
    a bit is set.


    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Rob Williscroft

      #3
      Re: Am I an idiot?

      Kevin Grigorenko wrote in news:br7km3$ds0 $1@f04n12.cac.p su.edu:
      [color=blue]
      > That's a rethorical question. But shouldn't this statement store the
      > value of three in the variable a:
      >
      > #include <iostream>
      >
      > int main()
      > {
      > int a = 1 & 2;[/color]

      int a = 1 | 2;
      [color=blue]
      > if(a & 1)
      > {
      > std::cout << "One " << std::endl;
      > }
      > if(a & 2)
      > {
      > std::cout << "Two " << std::endl;
      > }
      > return 0;
      > }
      >
      > I'm trying to use bit-masking to pass around flags with an integer,
      > and then check them with if( integer & flagX )
      >
      > What is going on?
      >[/color]

      & (aka bitwise and aka bitand) both things must be true (or set in a
      bitwise sense).

      | (aka bitwise or aka bitor ) one (or both) must be true (or ...)

      HTH.

      Rob.
      --

      Comment

      • Kevin Grigorenko

        #4
        Re: Am I an idiot?

        "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
        news:3FD75759.F 3CE3DA2@gascad. at...[color=blue]
        > Kevin Grigorenko wrote:[color=green]
        > >
        > > That's a rethorical question. But shouldn't this statement store the[/color][/color]
        value[color=blue][color=green]
        > > of three in the variable a:[/color]
        >
        > No. It should store the value 0.
        >[color=green]
        > >
        > > #include <iostream>
        > >
        > > int main()
        > > {
        > > int a = 1 & 2;[/color]
        >
        > You want
        >
        > int a = 1 | 2;[/color]

        Ahhh, so I am an idiot. I knew that. Haha, wow, what is wrong with me
        today.
        [color=blue]
        >
        >
        > Use | to set bits, use & to clear bits or to check if
        > a bit is set.
        >
        >
        > --
        > Karl Heinz Buchegger
        > kbuchegg@gascad .at[/color]

        Appreciate it,
        Kevin Grigorenko


        Comment

        • E. Robert Tisdale

          #5
          Re: Am I an idiot?

          Kevin Grigorenko wrote:
          [color=blue]
          > Shouldn't this statement store the value
          > of three in the variable a:
          >
          > #include <iostream>
          >
          > int main()
          > {
          > int a = 1 & 2;[/color]

          You probably meant to write

          int a = 1 | 2;
          [color=blue]
          > if(a & 1)
          > {
          > std::cout << "One " << std::endl;
          > }
          > if(a & 2)
          > {
          > std::cout << "Two " << std::endl;
          > }
          > return 0;
          > }
          >
          > I'm trying to use bit-masking to pass around flags with an integer
          > and then check them with if( integer & flagX )
          >
          > What is going on?[/color]

          Nothing. You're just an idiot. ;-)

          Comment

          • Kevin Grigorenko

            #6
            Re: Am I an idiot?

            "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
            news:3FD757C2.9 010508@jpl.nasa .gov...[color=blue]
            > Kevin Grigorenko wrote:
            >[color=green]
            > > Shouldn't this statement store the value
            > > of three in the variable a:
            > >
            > > #include <iostream>
            > >
            > > int main()
            > > {
            > > int a = 1 & 2;[/color]
            >
            > You probably meant to write
            >
            > int a = 1 | 2;
            >[color=green]
            > > if(a & 1)
            > > {
            > > std::cout << "One " << std::endl;
            > > }
            > > if(a & 2)
            > > {
            > > std::cout << "Two " << std::endl;
            > > }
            > > return 0;
            > > }
            > >
            > > I'm trying to use bit-masking to pass around flags with an integer
            > > and then check them with if( integer & flagX )
            > >
            > > What is going on?[/color]
            >
            > Nothing. You're just an idiot. ;-)[/color]

            I agree :). Thanks for the quick help to all who responded. It is now
            confirmed that I am an idiot.

            Kevin Grigorenko


            Comment

            • jeffc

              #7
              Re: Am I an idiot?


              "Kevin Grigorenko" <kzg110@psu.edu > wrote in message
              news:br7km3$ds0 $1@f04n12.cac.p su.edu...[color=blue]
              >
              > I'm trying to use bit-masking to pass around flags with an integer, and[/color]
              then[color=blue]
              > check them with if( integer & flagX )[/color]

              But the bits for 1 are 01, and for 2 are 10. When you AND them together,
              there aren't any ones. Did you mean OR?


              Comment

              • David Fisher

                #8
                Re: Am I an idiot?

                "jeffc" <nobody@nowhere .com> wrote:
                [color=blue]
                > But the bits for 1 are 01, and for 2 are 10. When you AND them together,
                > there aren't any ones. Did you mean OR?[/color]

                The bit patterns for 1 and 2 are not absolutely guaranteed to be "000...01"
                and "000...10" - the "two's complement" format represents them this way,
                but theoretically they could be anything they like (I'm not sure if any
                computers actually represent them a different way, but I've heard of "one's
                complement" where negative numbers are stored differently).

                0x1 and 0x2 ought to be pretty safe, though ...

                David F

                PS. Pretty sure about this, but correct me if I'm wrong - it's kind of
                unintuitive for the expression (0x0F == 15) to not always be true, since 0F
                _is_ 15 in hexadecimal ...


                Comment

                • Ron Natalie

                  #9
                  Re: Am I an idiot?


                  "David Fisher" <nospam@nospam. nospam.nospam> wrote in message
                  news:doOBb.5286 $xm.150993@nasa l.pacific.net.a u...[color=blue]
                  > "jeffc" <nobody@nowhere .com> wrote:
                  >[color=green]
                  > > But the bits for 1 are 01, and for 2 are 10. When you AND them[/color][/color]
                  together,[color=blue][color=green]
                  > > there aren't any ones. Did you mean OR?[/color]
                  >
                  > The bit patterns for 1 and 2 are not absolutely guaranteed to be[/color]
                  "000...01"[color=blue]
                  > and "000...10" - the "two's complement" format represents them this way,[/color]
                  ..
                  Actually, it has nothing to do with two's complement. The C standard
                  REQUIRES
                  the positive numbers to be simply straight forward binary. The rules (for
                  C++ and
                  C89) say that unsigned values are pretty much straight forward binary and
                  that the
                  positive signed variables have the same representation as their unsigned
                  counterparts.
                  The C99 standard goes even further and says that there is only three legal
                  integer
                  encodings: 2's complement, 1's complement, and signed-magnitude. All of
                  which
                  obey the earlier constraint.

                  -> 0x1 and 0x2 ought to be pretty safe, though ...

                  This is a pretty bizarre statement give your earlier statement. 1 and 0x1
                  are
                  exactly the same value as var as the language is concerned (the hex
                  specified
                  numbers only differ in that they will have type unsigned int if int can't
                  represent them).



                  Comment

                  • Andrey Tarasevich

                    #10
                    Re: Am I an idiot?

                    David Fisher wrote:[color=blue]
                    > ...[color=green]
                    >> But the bits for 1 are 01, and for 2 are 10. When you AND them together,
                    >> there aren't any ones. Did you mean OR?[/color]
                    >
                    > The bit patterns for 1 and 2 are not absolutely guaranteed to be "000...01"
                    > and "000...10" - the "two's complement" format represents them this way,
                    > but theoretically they could be anything they like (I'm not sure if any
                    > computers actually represent them a different way, but I've heard of "one's
                    > complement" where negative numbers are stored differently).
                    > ...[/color]

                    Positive integral values are represented in the same way in all binary
                    formats supported by C/C++ specifications. In other words, 1 is
                    guaranteed to be '0..001' and 2 is guaranteed to be '0..010'.

                    Note, that "bits" that are mentioned in the language specification are
                    language-level abstractions. The actual underlying hardware might even
                    be non-binary (ternary, for example) and have nothing that can be
                    regarded as physical "bits".

                    --
                    Best regards,
                    Andrey Tarasevich

                    Comment

                    • David Fisher

                      #11
                      Re: Am I an idiot?

                      "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote:
                      [color=blue]
                      > Positive integral values are represented in the same way in all binary
                      > formats supported by C/C++ specifications. In other words, 1 is
                      > guaranteed to be '0..001' and 2 is guaranteed to be '0..010'.
                      >
                      > Note, that "bits" that are mentioned in the language specification are
                      > language-level abstractions. The actual underlying hardware might even
                      > be non-binary (ternary, for example) and have nothing that can be
                      > regarded as physical "bits".[/color]

                      OK, my mistake ...

                      Thanks for the info :-)

                      David F


                      Comment

                      • Tron Thomas

                        #12
                        Re: Am I an idiot?

                        It looks like you used the wrong logical operator to initialize the
                        variable "a".

                        If you logically AND the value 1 with the value 2, the result is zero.

                        You probably want to OR the two values together.

                        Replace:
                        int a = 1 & 2;

                        With:
                        int a = 1 | 2;

                        Kevin Grigorenko wrote:
                        [color=blue]
                        > That's a rethorical question. But shouldn't this statement store the value
                        > of three in the variable a:
                        >
                        > #include <iostream>
                        >
                        > int main()
                        > {
                        > int a = 1 & 2;
                        > if(a & 1)
                        > {
                        > std::cout << "One " << std::endl;
                        > }
                        > if(a & 2)
                        > {
                        > std::cout << "Two " << std::endl;
                        > }
                        > return 0;
                        > }
                        >
                        > I'm trying to use bit-masking to pass around flags with an integer, and then
                        > check them with if( integer & flagX )
                        >
                        > What is going on?
                        >
                        > Thanks a lot for your time,
                        > Kevin Grigorenko
                        >
                        >[/color]

                        Comment

                        Working...