Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Help with making this prettier?

Reply
Thread Tools

Help with making this prettier?

 
 
Pito Salas
Guest
Posts: n/a
 
      07-16-2009
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/.

 
Reply With Quote
 
 
 
 
Chris Howe
Guest
Posts: n/a
 
      07-16-2009
[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 <(E-Mail Removed)> 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/.
>
>


 
Reply With Quote
 
 
 
 
Pito Salas
Guest
Posts: n/a
 
      07-16-2009
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: A fresh new approach to making making online Fakename Computer Information 0 11-18-2005 02:10 AM
Making new Flavors : Making a custom transferhandler for and drop applications ebby83@gmail.com Java 5 01-12-2005 11:10 AM
Help with making a control work . Please help :-( Simon Harvey ASP .Net 2 10-21-2004 08:03 AM
Help with making a control work . Please help :-( Simon Harvey ASP .Net Web Controls 2 10-21-2004 08:03 AM
Help with making a control work . Please help :-( Simon Harvey ASP .Net Building Controls 2 10-21-2004 08:03 AM



Advertisments