[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

converting numbers via bit dropping

lists

4/1/2006 11:46:00 PM

Hi,
I'm trying to figure out how to convert a number by dropping bits
higher than the range that I want. For example, I'd like to convert
248 (0000 0001 0001 1100) into 28 (0001 1100) by converting any bits
over the 8th one into a zero:

000 0001 0001 1100
^^^ ^^^^

Any suggestions?

Thanks,
Ryan


2 Answers

Patrick Gundlach

4/1/2006 11:57:00 PM

0

Hi,


> I'm trying to figure out how to convert a number by dropping bits
> higher than the range that I want. For example, I'd like to convert
> 248 (0000 0001 0001 1100) into 28 (0001 1100) by converting any bits

This should be 284, right?

> over the 8th one into a zero:
>
> 000 0001 0001 1100
> ^^^ ^^^^
>
> Any suggestions?

irb(main):001:0> 284 & 255
=> 28

HTH,

Patrick

lists

4/2/2006 12:07:00 AM

0


On Apr 1, 2006, at 5:58 PM, Patrick Gundlach wrote:

> Hi,
>
>
>> I'm trying to figure out how to convert a number by dropping bits
>> higher than the range that I want. For example, I'd like to convert
>> 248 (0000 0001 0001 1100) into 28 (0001 1100) by converting any bits
>
> This should be 284, right?

Yep, my bad.

>
>> over the 8th one into a zero:
>>
>> 000 0001 0001 1100
>> ^^^ ^^^^
>>
>> Any suggestions?
>
> irb(main):001:0> 284 & 255
> => 28

That sure does help, thanks!

>
> HTH,
>
> Patrick
>


-Ryan