Forum: Ruby converting numbers via bit dropping

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F4a46925eed374819ceed93f3e911e8d?d=identicon&s=25 lists (Guest)
on 2006-04-02 01:46
(Received via mailing list)
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
3522d767bf5fe5ed27719deb6a88adf7?d=identicon&s=25 Patrick Gundlach (Guest)
on 2006-04-02 02:02
(Received via mailing list)
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
F4a46925eed374819ceed93f3e911e8d?d=identicon&s=25 lists (Guest)
on 2006-04-02 02:08
(Received via mailing list)
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
This topic is locked and can not be replied to.