How to implement this?

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

    How to implement this?

    Use a variable of char type and each bit of it indicates if a button is
    pressed or not. For example,

    0010010

    tells that 3rd and 7th buttons are pressed down.

    How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
    set to 1 or 0?

    Thanks!


  • John Harrison

    #2
    Re: How to implement this?


    "ad" <adconn@168.com > wrote in message
    news:5KGbc.1731 7$vo5.530939@bg tnsc05-news.ops.worldn et.att.net...[color=blue]
    > Use a variable of char type and each bit of it indicates if a button is
    > pressed or not. For example,
    >
    > 0010010
    >
    > tells that 3rd and 7th buttons are pressed down.
    >
    > How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
    > set to 1 or 0?
    >
    > Thanks!
    >
    >[/color]

    x |= (1 << n); // set bit n to 1

    x &= ~(1 << n); // set bit n to 0

    if (x&(1 << n)) // true if bit n is 1

    john


    Comment

    • John Harrison

      #3
      Re: How to implement this?


      "ad" <adconn@168.com > wrote in message
      news:5KGbc.1731 7$vo5.530939@bg tnsc05-news.ops.worldn et.att.net...[color=blue]
      > Use a variable of char type and each bit of it indicates if a button is
      > pressed or not. For example,
      >
      > 0010010
      >
      > tells that 3rd and 7th buttons are pressed down.
      >
      > How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
      > set to 1 or 0?
      >
      > Thanks!
      >
      >[/color]

      x |= (1 << n); // set bit n to 1

      x &= ~(1 << n); // set bit n to 0

      if (x&(1 << n)) // true if bit n is 1

      john


      Comment

      • John Harrison

        #4
        Re: How to implement this?

        > >[color=blue][color=green]
        > > 0010010
        > >
        > > tells that 3rd and 7th buttons are pressed down.
        > >[/color][/color]

        Actually to me that looks like the 1st and 4th buttons

        0000001 // zeroth button
        0000010 // first button
        0000100 // second button

        etc.

        I'm not trying to quibble semantics, but read my previous reply in light of
        how I define zeroth bit, first bit, second bit etc.

        john


        Comment

        • John Harrison

          #5
          Re: How to implement this?

          > >[color=blue][color=green]
          > > 0010010
          > >
          > > tells that 3rd and 7th buttons are pressed down.
          > >[/color][/color]

          Actually to me that looks like the 1st and 4th buttons

          0000001 // zeroth button
          0000010 // first button
          0000100 // second button

          etc.

          I'm not trying to quibble semantics, but read my previous reply in light of
          how I define zeroth bit, first bit, second bit etc.

          john


          Comment

          • Till Crueger

            #6
            Re: How to implement this?

            On Sat, 03 Apr 2004 22:17:37 +0000, ad wrote:
            [color=blue]
            > Use a variable of char type and each bit of it indicates if a button is
            > pressed or not. For example,
            >
            > 0010010
            >
            > tells that 3rd and 7th buttons are pressed down.
            >
            > How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit
            > is set to 1 or 0?[/color]

            Define the Flags for the buttons you need. Something like

            #define FIRST 1
            #define SECOND 2
            #define THIRD 4

            [...]

            then you can simply do an AND with the variable you want to check, and see
            wether the result is zero or not. For setting you can do an OR.

            Alternatively to the defines you could produce an array which will contain
            the appropriate flags.

            Till

            --
            Please add "Salt and Peper" to the subject line to bypass my spam filter

            Comment

            • Till Crueger

              #7
              Re: How to implement this?

              On Sat, 03 Apr 2004 22:17:37 +0000, ad wrote:
              [color=blue]
              > Use a variable of char type and each bit of it indicates if a button is
              > pressed or not. For example,
              >
              > 0010010
              >
              > tells that 3rd and 7th buttons are pressed down.
              >
              > How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit
              > is set to 1 or 0?[/color]

              Define the Flags for the buttons you need. Something like

              #define FIRST 1
              #define SECOND 2
              #define THIRD 4

              [...]

              then you can simply do an AND with the variable you want to check, and see
              wether the result is zero or not. For setting you can do an OR.

              Alternatively to the defines you could produce an array which will contain
              the appropriate flags.

              Till

              --
              Please add "Salt and Peper" to the subject line to bypass my spam filter

              Comment

              • Siemel Naran

                #8
                Re: How to implement this?

                "Till Crueger" <TillFC@gmx.net > wrote in message
                news:c4ng1l$nq8 $1@f1node01.rhr z.uni-
                [color=blue]
                > #define FIRST 1
                > #define SECOND 2
                > #define THIRD 4[/color]

                Why not const int?

                [color=blue]
                > then you can simply do an AND with the variable you want to check, and see
                > wether the result is zero or not. For setting you can do an OR.[/color]

                How about flipping a bit? For that we can use exclusive or, for which the
                symbol is ^. Please let me know if this is correct.

                // suppose a byte is 4 bits, CHAR_BITS = 4
                int a=0x03 // 0011
                a ^= 0x01; // 0011 ^ 0001 = 0010
                a ^= 0x01; // 0010 ^ 0001 = 0011


                Comment

                • Siemel Naran

                  #9
                  Re: How to implement this?

                  "Till Crueger" <TillFC@gmx.net > wrote in message
                  news:c4ng1l$nq8 $1@f1node01.rhr z.uni-
                  [color=blue]
                  > #define FIRST 1
                  > #define SECOND 2
                  > #define THIRD 4[/color]

                  Why not const int?

                  [color=blue]
                  > then you can simply do an AND with the variable you want to check, and see
                  > wether the result is zero or not. For setting you can do an OR.[/color]

                  How about flipping a bit? For that we can use exclusive or, for which the
                  symbol is ^. Please let me know if this is correct.

                  // suppose a byte is 4 bits, CHAR_BITS = 4
                  int a=0x03 // 0011
                  a ^= 0x01; // 0011 ^ 0001 = 0010
                  a ^= 0x01; // 0010 ^ 0001 = 0011


                  Comment

                  • Old Wolf

                    #10
                    Re: How to implement this?

                    "John Harrison" <john_andronicu s@hotmail.com>:[color=blue]
                    > "ad" <adconn@168.com > wrote:[color=green]
                    > > How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
                    > > set to 1 or 0?[/color]
                    >
                    > x |= (1 << n); // set bit n to 1
                    >
                    > x &= ~(1 << n); // set bit n to 0
                    >
                    > if (x&(1 << n)) // true if bit n is 1[/color]

                    These should probably be 1UL instead of 1, because of the case where
                    x is [unsigned] 'long' and sizeof(long) > sizeof(int).

                    Comment

                    • Old Wolf

                      #11
                      Re: How to implement this?

                      "John Harrison" <john_andronicu s@hotmail.com>:[color=blue]
                      > "ad" <adconn@168.com > wrote:[color=green]
                      > > How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
                      > > set to 1 or 0?[/color]
                      >
                      > x |= (1 << n); // set bit n to 1
                      >
                      > x &= ~(1 << n); // set bit n to 0
                      >
                      > if (x&(1 << n)) // true if bit n is 1[/color]

                      These should probably be 1UL instead of 1, because of the case where
                      x is [unsigned] 'long' and sizeof(long) > sizeof(int).

                      Comment

                      • Rolf Magnus

                        #12
                        Re: How to implement this?

                        Siemel Naran wrote:
                        [color=blue]
                        > "Till Crueger" <TillFC@gmx.net > wrote in message
                        > news:c4ng1l$nq8 $1@f1node01.rhr z.uni-
                        >[color=green]
                        >> #define FIRST 1
                        >> #define SECOND 2
                        >> #define THIRD 4[/color]
                        >
                        > Why not const int?[/color]

                        Why not enum?
                        [color=blue][color=green]
                        >> then you can simply do an AND with the variable you want to check,
                        >> and see wether the result is zero or not. For setting you can do an
                        >> OR.[/color]
                        >
                        > How about flipping a bit? For that we can use exclusive or, for which
                        > the symbol is ^. Please let me know if this is correct.
                        >
                        > // suppose a byte is 4 bits, CHAR_BITS = 4[/color]

                        CHAR_BITS is never 4. It is at least 8.
                        [color=blue]
                        > int a=0x03 // 0011
                        > a ^= 0x01; // 0011 ^ 0001 = 0010
                        > a ^= 0x01; // 0010 ^ 0001 = 0011[/color]

                        Yes, that's correct.

                        Comment

                        • Rolf Magnus

                          #13
                          Re: How to implement this?

                          Siemel Naran wrote:
                          [color=blue]
                          > "Till Crueger" <TillFC@gmx.net > wrote in message
                          > news:c4ng1l$nq8 $1@f1node01.rhr z.uni-
                          >[color=green]
                          >> #define FIRST 1
                          >> #define SECOND 2
                          >> #define THIRD 4[/color]
                          >
                          > Why not const int?[/color]

                          Why not enum?
                          [color=blue][color=green]
                          >> then you can simply do an AND with the variable you want to check,
                          >> and see wether the result is zero or not. For setting you can do an
                          >> OR.[/color]
                          >
                          > How about flipping a bit? For that we can use exclusive or, for which
                          > the symbol is ^. Please let me know if this is correct.
                          >
                          > // suppose a byte is 4 bits, CHAR_BITS = 4[/color]

                          CHAR_BITS is never 4. It is at least 8.
                          [color=blue]
                          > int a=0x03 // 0011
                          > a ^= 0x01; // 0011 ^ 0001 = 0010
                          > a ^= 0x01; // 0010 ^ 0001 = 0011[/color]

                          Yes, that's correct.

                          Comment

                          • Kevin Goodsell

                            #14
                            Re: How to implement this?

                            Rolf Magnus wrote:[color=blue]
                            > Siemel Naran wrote:
                            >[color=green]
                            >>
                            >>// suppose a byte is 4 bits, CHAR_BITS = 4[/color]
                            >
                            >
                            > CHAR_BITS is never 4. It is at least 8.
                            >[/color]

                            CHAR_BITS could be 4. But CHAR_BIT (at least, the CHAR_BIT that is
                            #defined in <climits>) cannot be. ;)

                            I'm sure he knows it cannot actually be 4. I believe he was just
                            simplifying.

                            -Kevin
                            --
                            My email address is valid, but changes periodically.
                            To contact me please use the address from a recent posting.

                            Comment

                            • Kevin Goodsell

                              #15
                              Re: How to implement this?

                              Rolf Magnus wrote:[color=blue]
                              > Siemel Naran wrote:
                              >[color=green]
                              >>
                              >>// suppose a byte is 4 bits, CHAR_BITS = 4[/color]
                              >
                              >
                              > CHAR_BITS is never 4. It is at least 8.
                              >[/color]

                              CHAR_BITS could be 4. But CHAR_BIT (at least, the CHAR_BIT that is
                              #defined in <climits>) cannot be. ;)

                              I'm sure he knows it cannot actually be 4. I believe he was just
                              simplifying.

                              -Kevin
                              --
                              My email address is valid, but changes periodically.
                              To contact me please use the address from a recent posting.

                              Comment

                              Working...