bit position

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

    #16
    Re: bit position

    On Jun 3, 7:00 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
    Chris Thomasson wrote:
    >
    <vipps...@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.
    I believe this is possible:
    unsigned or_bit(unsigned val, unsigned bit) {
    assert(bit < (unsigned)log2l (UINT_MAX);
    ....

    Actually, can this problem be solved fully portably? (I guess there
    isn't a guarantee that LDBL_MAX UINT_MAX)

    Comment

    • Richard Harter

      #17
      Re: bit position

      On Tue, 03 Jun 2008 12:54:55 -0400, Eric Sosman
      <Eric.Sosman@su n.comwrote:
      >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] ...

      0xDEADBEEF :-)

      That's what I get for copying someone's code without looking at
      it.

      (should be <<i)


      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

      • user923005

        #18
        Re: bit position

        On Jun 3, 2:16 am, "Ivan" <idipr...@hotma il.comwrote:
        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;
        ...
        >
        }
        #include <assert.h>

        const unsigned long long mask[64] = {
        0x0000000000000 001ULL, 0x0000000000000 002ULL,
        0x0000000000000 004ULL, 0x0000000000000 008ULL,
        0x0000000000000 010ULL, 0x0000000000000 020ULL,
        0x0000000000000 040ULL, 0x0000000000000 080ULL,
        0x0000000000000 100ULL, 0x0000000000000 200ULL,
        0x0000000000000 400ULL, 0x0000000000000 800ULL,
        0x0000000000001 000ULL, 0x0000000000002 000ULL,
        0x0000000000004 000ULL, 0x0000000000008 000ULL,
        0x0000000000010 000ULL, 0x0000000000020 000ULL,
        0x0000000000040 000ULL, 0x0000000000080 000ULL,
        0x0000000000100 000ULL, 0x0000000000200 000ULL,
        0x0000000000400 000ULL, 0x0000000000800 000ULL,
        0x0000000001000 000ULL, 0x0000000002000 000ULL,
        0x0000000004000 000ULL, 0x0000000008000 000ULL,
        0x0000000010000 000ULL, 0x0000000020000 000ULL,
        0x0000000040000 000ULL, 0x0000000080000 000ULL,
        0x0000000100000 000ULL, 0x0000000200000 000ULL,
        0x0000000400000 000ULL, 0x0000000800000 000ULL,
        0x0000001000000 000ULL, 0x0000002000000 000ULL,
        0x0000004000000 000ULL, 0x0000008000000 000ULL,
        0x0000010000000 000ULL, 0x0000020000000 000ULL,
        0x0000040000000 000ULL, 0x0000080000000 000ULL,
        0x0000100000000 000ULL, 0x0000200000000 000ULL,
        0x0000400000000 000ULL, 0x0000800000000 000ULL,
        0x0001000000000 000ULL, 0x0002000000000 000ULL,
        0x0004000000000 000ULL, 0x0008000000000 000ULL,
        0x0010000000000 000ULL, 0x0020000000000 000ULL,
        0x0040000000000 000ULL, 0x0080000000000 000ULL,
        0x0100000000000 000ULL, 0x0200000000000 000ULL,
        0x0400000000000 000ULL, 0x0800000000000 000ULL,
        0x1000000000000 000ULL, 0x2000000000000 000ULL,
        0x4000000000000 000ULL, 0x8000000000000 000ULL,
        };

        unsigned long long bit2longlong(un signed bit)
        {
        assert (bit < 64);
        return mask[i];
        }

        Comment

        • Peter Nilsson

          #19
          Re: bit position

          vipps...@gmail. com wrote:
          I believe this is possible:
          unsigned or_bit(unsigned val, unsigned bit) {
          assert(bit < (unsigned)log2l (UINT_MAX);
          Why bother with the cast?
          Actually, can this problem be solved fully
          portably?
          unsigned unsigned_value_ bits(void)
          {
          unsigned n = 16;
          unsigned m = 0x8000;
          while (m <<= 1) n++;
          return n;
          }

          ...
          assert( bit < unsigned_value_ bits() );

          --
          Peter

          Comment

          • rahul

            #20
            Re: bit position

            So basically we are left with using the switch, inline shifts, tables
            created with shifts or hard-coded tables. 'switch' is pretty much out
            of question. The other choices are just a question of style. I believe
            this problem can easily be solved portably by taking unsigned data
            types and basic error checking which has already been posted by most
            of you.

            Comment

            Working...