# Converting numbers via bit dropping

**URL:** <https://www.ruby-forum.com/t/converting-numbers-via-bit-dropping/56638>\
**Category:** Ruby\
**Created:** [April 2, 2006, 1:46am UTC](https://www.ruby-forum.com/t/converting-numbers-via-bit-dropping/56638 "2006-04-02T01:46:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [April 2, 2006, 1:46am UTC](https://www.ruby-forum.com/t/converting-numbers-via-bit-dropping/56638/1 "2006-04-02T01:46:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [April 2, 2006, 2:02am UTC](https://www.ruby-forum.com/t/converting-numbers-via-bit-dropping/56638/2 "2006-04-02T02:02:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [April 2, 2006, 2:08am UTC](https://www.ruby-forum.com/t/converting-numbers-via-bit-dropping/56638/3 "2006-04-02T02:08:11Z")

</div>

On Apr 1, 2006, at 5:58 PM, Patrick G. 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
