From: Pito Salas Date: 2009-07-17T04:20:04+09:00 Subject: Re: Help with making this prettier? 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/.