Help with making this prettier?

Discussion in 'Ruby' started by Pito Salas, Jul 16, 2009.

  1. Pito Salas

    Pito Salas Guest

    What I want to do is to extract the integer value corresponding to a bit
    field within a FixNum. That is, for example, what is the integer value
    formed by bits 4,5,6,7,8 of a number?

    This is my ugly first cut. There must be a library function that does
    this but I can't find it. Anyone have a pointer, that would be great!!
    Or a suggestion on refactoring/rewriting this?

    Thanks!

    def bit_field(from, to, val)
    accum = 0
    ind = 0
    from.upto(to) do |index|
    bitval = val[index]
    accum = accum + bitval * (2 ** ind)
    ind = ind + 1
    end
    accum
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Pito Salas, Jul 16, 2009
    #1
    1. Advertising

  2. Pito Salas

    Chris Howe Guest

    [Note: parts of this message were removed to make it a legal post.]

    How about this:

    def bit_field(from,to,val)
    return ( val>>from ) & ( (2 << (to - from) ) - 1)
    end
    In binary 2<<(to-from) is going to be a 1 with (to-from+1) zeros after it,
    subtracting 1 leaves you with just (to-from+1) 1's in the lower bits of the
    number.

    val>>from shifts the bit field over to the lower bits.

    Bitwise anding the two together masks off the upper order bits of val,
    leaving you with the value you are looking for.



    On Thu, Jul 16, 2009 at 10:26 AM, Pito Salas <> wrote:

    > What I want to do is to extract the integer value corresponding to a bit
    > field within a FixNum. That is, for example, what is the integer value
    > formed by bits 4,5,6,7,8 of a number?
    >
    > This is my ugly first cut. There must be a library function that does
    > this but I can't find it. Anyone have a pointer, that would be great!!
    > Or a suggestion on refactoring/rewriting this?
    >
    > Thanks!
    >
    > def bit_field(from, to, val)
    > accum = 0
    > ind = 0
    > from.upto(to) do |index|
    > bitval = val[index]
    > accum = accum + bitval * (2 ** ind)
    > ind = ind + 1
    > end
    > accum
    > end
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Chris Howe, Jul 16, 2009
    #2
    1. Advertising

  3. Pito Salas

    Pito Salas Guest

    Chris Howe wrote:
    > How about this:
    >
    > def bit_field(from,to,val)
    > return ( val>>from ) & ( (2 << (to - from) ) - 1)
    > end
    > In binary 2<<(to-from) is going to be a 1 with (to-from+1) zeros after
    > it,
    > subtracting 1 leaves you with just (to-from+1) 1's in the lower bits of
    > the
    > number.
    >
    > val>>from shifts the bit field over to the lower bits.
    >
    > Bitwise anding the two together masks off the upper order bits of val,
    > leaving you with the value you are looking for.


    Thanks. Beautiful, and nice explanation too!

    - Pito

    --
    Posted via http://www.ruby-forum.com/.
     
    Pito Salas, Jul 16, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    1,878
    Andrew Thompson
    Jan 12, 2005
  2. Jim Langston
    Replies:
    3
    Views:
    398
    Jeff F
    Oct 5, 2007
  3. Rob Sanheim
    Replies:
    2
    Views:
    94
    Rob Sanheim
    Jul 26, 2006
  4. Helder Ribeiro

    Prettier solution to circular dependency

    Helder Ribeiro, Jun 11, 2007, in forum: Ruby
    Replies:
    4
    Views:
    131
    Bas van Gils
    Jun 11, 2007
  5. Christophe Mckeon

    prettier documentation

    Christophe Mckeon, Nov 17, 2007, in forum: Ruby
    Replies:
    3
    Views:
    80
    Christophe Mckeon
    Nov 19, 2007
Loading...

Share This Page