bit position

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

    bit position

    Hi all,

    What is an easy and general way to convert the bit number into its position?
    Let say that I want to set bit 25 I would do something like:

    void Setbit(arg)
    {
    switch(arg)
    ...
    case 25:
    flag |= 0x02000000;
    ....
    }

    Thanks
    Ivan


  • Chris Dollin

    #2
    Re: bit position

    Ivan wrote:
    What is an easy and general way to convert the bit number into its position?
    Let say that I want to set bit 25 I would do something like:
    >
    void Setbit(arg)
    {
    switch(arg)
    ..
    case 25:
    flag |= 0x02000000;
    ...
    }
    I'd use the << operator.

    --
    "It's a hat as long as I /say/ it's a hat." /A College of Magics/

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN

    Comment

    • Chris Thomasson

      #3
      Re: bit position

      "Ivan" <idiprima@hotma il.comwrote in message
      news:g2325v$e6u $1@news.al.sw.e ricsson.se...
      Hi all,
      >
      What is an easy and general way to convert the bit number into its
      position?
      Let say that I want to set bit 25 I would do something like:
      [...]

      Perhaps something like:
      _______________ _______________ _______________ __________
      #include <stdio.h>

      #define BITVAL(mp_bit) (1 << (mp_bit))

      int main() {
      printf("bit(25) == int(%d)\n", BITVAL(25));
      return 0;
      }

      _______________ _______________ _______________ __________


      Comment

      • rahul

        #4
        Re: bit position

        void setBit (int *arg, int n) {
        unsigned int flag = 1;
        flag = flag << (n-1);
        *arg = *arg | flag;
        }

        This will set the n bit for arg. I am taking the rightmost bit as bit
        1. Of course, some validation is required for it to work in production
        environment.

        Comment

        • Chris Thomasson

          #5
          Re: bit position


          "Chris Thomasson" <cristom@comcas t.netwrote in message
          news:xoWdnRwIOb 21r9jVnZ2dnUVZ_ hGdnZ2d@comcast .com...
          "Ivan" <idiprima@hotma il.comwrote in message
          news:g2325v$e6u $1@news.al.sw.e ricsson.se...
          >Hi all,
          >>
          >What is an easy and general way to convert the bit number into its
          >position?
          >Let say that I want to set bit 25 I would do something like:
          [...]
          >
          Perhaps something like:
          _______________ _______________ _______________ __________
          #include <stdio.h>
          >
          #define BITVAL(mp_bit) (1 << (mp_bit))
          >
          int main() {
          printf("bit(25) == int(%d)\n", BITVAL(25));
          return 0;
          }
          >
          _______________ _______________ _______________ __________

          I should point out that the macro works from a bit index starting at 0-to-N.
          So a 32-bit number goes from bit 0 to bit 31.

          Comment

          • Chris Thomasson

            #6
            Re: bit position

            "rahul" <rahulsinner@gm ail.comwrote in message
            news:f1d41950-b07c-4f85-b21f-dadf21d9a148@j3 3g2000pri.googl egroups.com...
            void setBit (int *arg, int n) {
            unsigned int flag = 1;
            flag = flag << (n-1);
            *arg = *arg | flag;
            }
            >
            This will set the n bit for arg. I am taking the rightmost bit as bit
            1. Of course, some validation is required for it to work in production
            environment.
            This does not give the same result as your original example:

            void Setbit(arg)
            {
            switch(arg)
            ...
            case 25:
            flag |= 0x02000000;
            ....
            }

            When you invoke it as:

            int i = 0;
            setBit(&i, 25);




            BTW, why are you using a pointer to an int? Don't you think it would be
            simpler and more functional if you had something like:


            #include <limits.h>
            #include <assert.h>

            #define BITVAL(mp_bit) (1 << (mp_bit))

            int or_bit(int val, unsigned bit) {
            assert(bit >= 0 && bit < (sizeof(int) * CHAR_BIT));
            return val | BITVAL(bit);
            }


            ?

            Comment

            • Chris Dollin

              #7
              Re: bit position

              rahul wrote:
              void setBit (int *arg, int n) {
              unsigned int flag = 1;
              flag = flag << (n-1);
              *arg = *arg | flag;
              }
              Why all the unnecessary code?

              *arg |= (1 << (n - 1);

              (and shouldn't it be `unsigned int *arg`?)
              This will set the n bit for arg.
              I am taking the rightmost bit as bit 1.
              Many people think it's bit 0 that's the "rightmost" (least
              significant), because we think that bit i has the value
              2-to-the-i.
              Of course, some validation is required for it to work in production
              environment.
              "Validation "?

              --
              "I am your new best friend." /Electra City/

              Hewlett-Packard Limited registered office: Cain Road, Bracknell,
              registered no: 690597 England Berks RG12 1HN

              Comment

              • Chris Thomasson

                #8
                Re: bit position


                "Chris Dollin" <chris.dollin@h p.comwrote in message
                news:g23e9h$v1$ 1@news-pa1.hpl.hp.com. ..
                rahul wrote:
                >
                >void setBit (int *arg, int n) {
                > unsigned int flag = 1;
                > flag = flag << (n-1);
                > *arg = *arg | flag;
                >}
                >
                Why all the unnecessary code?
                >
                *arg |= (1 << (n - 1);
                >
                (and shouldn't it be `unsigned int *arg`?)
                >
                >This will set the n bit for arg.
                >I am taking the rightmost bit as bit 1.
                >
                Many people think it's bit 0 that's the "rightmost" (least
                significant), because we think that bit i has the value
                2-to-the-i.
                >
                >Of course, some validation is required for it to work in production
                >environment.
                >
                "Validation "?
                Perhaps he means something like the assertion in the following function:

                int or_bit(int val, unsigned bit) {
                assert(bit >= 0 && bit < (sizeof(int) * CHAR_BIT));
                return val | (1 << bit);
                }


                Comment

                • Chris Dollin

                  #9
                  Re: bit position

                  Chris Thomasson wrote:
                  "Chris Dollin" <chris.dollin@h p.comwrote in message
                  >"Validation" ?
                  >
                  Perhaps he means something like the assertion in the following function:
                  >
                  int or_bit(int val, unsigned bit) {
                  assert(bit >= 0 && bit < (sizeof(int) * CHAR_BIT));
                  return val | (1 << bit);
                  }
                  Oh, I see.

                  I probably wouldn't do /that/.

                  --
                  "I am your new best friend." /Electra City/

                  Hewlett-Packard Limited Cain Road, Bracknell, registered no:
                  registered office: Berks RG12 1HN 690597 England

                  Comment

                  • vippstar@gmail.com

                    #10
                    Re: bit position

                    On Jun 3, 4:34 pm, Chris Dollin <chris.dol...@h p.comwrote:
                    Chris Thomasson wrote:
                    "Chris Dollin" <chris.dol...@h p.comwrote in message
                    "Validation "?
                    >
                    Perhaps he means something like the assertion in the following function:
                    >
                    int or_bit(int val, unsigned bit) {
                    assert(bit >= 0 && bit < (sizeof(int) * CHAR_BIT));
                    return val | (1 << bit);
                    }
                    >
                    Oh, I see.
                    >
                    I probably wouldn't do /that/.
                    Cannot it invoke undefined behavior?
                    I was under the impression you cannot freely operate on all bits of a
                    signed integer.

                    Comment

                    • Chris Thomasson

                      #11
                      Re: bit position


                      <vippstar@gmail .comwrote in message
                      news:cf77c2ed-baf8-4961-8fbd-ebdd20445c2d@79 g2000hsk.google groups.com...
                      On Jun 3, 4:34 pm, Chris Dollin <chris.dol...@h p.comwrote:
                      >Chris Thomasson wrote:
                      "Chris Dollin" <chris.dol...@h p.comwrote in message
                      >"Validation" ?
                      >>
                      Perhaps he means something like the assertion in the following
                      function:
                      >>
                      int or_bit(int val, unsigned bit) {
                      assert(bit >= 0 && bit < (sizeof(int) * CHAR_BIT));
                      return val | (1 << bit);
                      }
                      >>
                      >Oh, I see.
                      >>
                      >I probably wouldn't do /that/.
                      Cannot it invoke undefined behavior?
                      I was under the impression you cannot freely operate on all bits of a
                      signed integer.
                      Well...



                      #include <limits.h>

                      unsigned or_bit(unsigned val, unsigned bit) {
                      assert(bit >= 0 && bit < (sizeof(unsigne d) * CHAR_BIT));
                      return val | (1 << bit);
                      }




                      ;^D

                      Comment

                      • Eric Sosman

                        #12
                        Re: bit position

                        Chris Thomasson wrote:
                        >
                        <vippstar@gmail .comwrote in message
                        news:cf77c2ed-baf8-4961-8fbd-ebdd20445c2d@79 g2000hsk.google groups.com...
                        >On Jun 3, 4:34 pm, Chris Dollin <chris.dol...@h p.comwrote:
                        >>Chris Thomasson wrote:
                        >"Chris Dollin" <chris.dol...@h p.comwrote in message
                        >>"Validation "?
                        >>>
                        >Perhaps he means something like the assertion in the following >
                        >>function:
                        >>>
                        >int or_bit(int val, unsigned bit) {
                        > assert(bit >= 0 && bit < (sizeof(int) * CHAR_BIT));
                        > return val | (1 << bit);
                        >}
                        >>>
                        >>Oh, I see.
                        >>>
                        >>I probably wouldn't do /that/.
                        >Cannot it invoke undefined behavior?
                        >I was under the impression you cannot freely operate on all bits of a
                        >signed integer.
                        >
                        Well...
                        >
                        >
                        >
                        #include <limits.h>
                        >
                        unsigned or_bit(unsigned val, unsigned bit) {
                        assert(bit >= 0 && bit < (sizeof(unsigne d) * CHAR_BIT));
                        return val | (1 << bit);
                        }
                        ITYM `1u << bit'. Also, what's the point in asserting
                        `bit >= 0'?

                        Finally, there's the theoretical possibility of padding
                        bits: It's conceivable (albeit unlikely) that the other
                        asserted condition is too lax.

                        --
                        Eric.Sosman@sun .com

                        Comment

                        • Richard Harter

                          #13
                          Re: bit position

                          On Tue, 3 Jun 2008 10:16:47 +0100, "Ivan" <idiprima@hotma il.com>
                          wrote:
                          >Hi all,
                          >
                          >What is an easy and general way to convert the bit number into its position?
                          >Let say that I want to set bit 25 I would do something like:
                          >
                          >void Setbit(arg)
                          >{
                          >switch(arg)
                          >..
                          >case 25:
                          flag |= 0x02000000;
                          >...
                          >}
                          >
                          >Thanks
                          >Ivan

                          There are at least three ways to do this - use a switch, use a
                          shift operator, or use a table. Your example uses a switch, and
                          other respondents used shift operators. No one mentioned using a
                          table so here is how that is done.

                          I am going to call the table bitval. (Call it anything you like,
                          of course.) The i'th entry has the i'th bit set. The table is
                          static storage. You can initialize it in the declaration, or you
                          can load it with a simple loop, e.g.,

                          for (i=0;i<MAXFLAG; i++) bitval[i] = 1<<(i-1);

                          Then the code to set bit 25 would be

                          flag |= bitval[25];

                          AFAICT the switch method has little to recommend it. The choice
                          between using an inline shift and using a table is probably a
                          matter of style. The inline shift method doesn't require any
                          setup code and the bit setting code is compact. The table method
                          does require setup code; the bit setting code is even more
                          compact. Which is faster is implementation dependent - it
                          depends on things like the cost of shifts versus the cost of
                          accessing a table and almost certainly doesn't matter.

                          By the way, it usually better if the bits you are setting and
                          unsetting have names and are referred to by name, e.g.,

                          cow_flags |= -1<<(HAS_BEEN_MI LKED -1)
                          cow_flags |= bitval[HAS_BEEN_MILKED];


                          Richard Harter, cri@tiac.net
                          http://home.tiac.net/~cri, http://www.varinoma.com
                          Save the Earth now!!
                          It's the only planet with chocolate.

                          Comment

                          • Eric Sosman

                            #14
                            Re: bit position

                            Richard Harter wrote:
                            [...]
                            There are at least three ways to do this - use a switch, use a
                            shift operator, or use a table. Your example uses a switch, and
                            other respondents used shift operators. No one mentioned using a
                            table so here is how that is done.
                            >
                            I am going to call the table bitval. (Call it anything you like,
                            of course.) The i'th entry has the i'th bit set. The table is
                            static storage. You can initialize it in the declaration, or you
                            can load it with a simple loop, e.g.,
                            >
                            for (i=0;i<MAXFLAG; i++) bitval[i] = 1<<(i-1);
                            I'm having a hard time figuring out what value is stored
                            in bitval[0] ...

                            --
                            Eric.Sosman@sun .com

                            Comment

                            • Chris Thomasson

                              #15
                              Re: bit position


                              "Eric Sosman" <Eric.Sosman@su n.comwrote in message
                              news:1212508765 .905158@news1nw k...
                              Chris Thomasson wrote:
                              [...]
                              >>
                              >#include <limits.h>
                              >>
                              >unsigned or_bit(unsigned val, unsigned bit) {
                              > assert(bit >= 0 && bit < (sizeof(unsigne d) * CHAR_BIT));
                              > return val | (1 << bit);
                              >}
                              >
                              ITYM `1u << bit'.
                              Ah yes. Indeed.



                              Also, what's the point in asserting
                              `bit >= 0'?
                              Humm. I have no idea!

                              :^o



                              Finally, there's the theoretical possibility of padding
                              bits: It's conceivable (albeit unlikely) that the other
                              asserted condition is too lax.
                              Interesting.

                              Comment

                              Working...