# Bitwise shift help

**URL:** <https://www.ruby-forum.com/t/bitwise-shift-help/181024>\
**Category:** Ruby\
**Created:** [January 4, 2010, 5:38am UTC](https://www.ruby-forum.com/t/bitwise-shift-help/181024 "2010-01-04T05:38:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [January 4, 2010, 5:38am UTC](https://www.ruby-forum.com/t/bitwise-shift-help/181024/1 "2010-01-04T05:38:06Z")

</div>

## Hello, I am new to Ruby and was wondering if there is an easier/faster way to determine if a bit is set in a number. For example, If my number is 1011000010 and I want to check to see if 3rd bit is set so 1011000010 1000010000

```
  0000 - So 3rd bit is not set

```

My code is

y = 1 \<\< 3  
x = 0b1011000010

if ( x & y ) == 1  
puts “The 3rd bit is set”  
else  
puts “The 3rd bit is not set”  
end

Is there a better/faster method of achieving this bit check?  
Thank you

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [January 4, 2010, 5:58am UTC](https://www.ruby-forum.com/t/bitwise-shift-help/181024/2 "2010-01-04T05:58:35Z")

</div>

Actually I suppose the bit check should read  
if ( x & y ) \>= 1  
Or I am still totally off.

Thanks

Kvetch K. wrote:

> ## Hello, I am new to Ruby and was wondering if there is an easier/faster way to determine if a bit is set in a number. For example, If my number is 1011000010 and I want to check to see if 3rd bit is set so 1011000010 1000010000
> 
> ```
> 0000 - So 3rd bit is not set
> 
> ```
> 
> My code is
> 
> y = 1 \<\< 3  
> x = 0b1011000010
> 
> if ( x & y ) == 1  
> puts “The 3rd bit is set”  
> else  
> puts “The 3rd bit is not set”  
> end
> 
> Is there a better/faster method of achieving this bit check?  
> Thank you

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [January 4, 2010, 6:45am UTC](https://www.ruby-forum.com/t/bitwise-shift-help/181024/3 "2010-01-04T06:45:45Z")

</div>

Maybe this:

Fixnum[n]  
Bit Reference—Returns the nth bit in the binary representation of  
fix, where fix[0] is the least significant bit.

–Axel

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [January 4, 2010, 8:43am UTC](https://www.ruby-forum.com/t/bitwise-shift-help/181024/4 "2010-01-04T08:43:59Z")

</div>

On 1/3/10, Kvetch K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Kvetch K. wrote:
> 
> > Hello, I am new to Ruby and was wondering if there is an easier/faster  
> > way to determine if a bit is set in a number. For example,  
> > [snip]
> 
> Actually I suppose the bit check should read  
> if ( x & y ) \>= 1  
> Or I am still totally off.

There’s nonzero?, which ought to be a tad faster than \>=1. So, write  
this instead:

if ( x & y ).nonzero?

Or, if you incorporate Axel’s suggestion to use Fixnum#[] as well, then:

if x[3].nonzero?

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [January 5, 2010, 4:42am UTC](https://www.ruby-forum.com/t/bitwise-shift-help/181024/5 "2010-01-05T04:42:46Z")

</div>

Hi,

Am Montag, 04. Jan 2010, 13:38:08 +0900 schrieb Kvetch K.:

> y = 1 \<\< 3  
> x = 0b1011000010
> 
> if ( x & y ) == 1  
> puts “The 3rd bit is set”  
> else  
> puts “The 3rd bit is not set”  
> end
> 
> Is there a better/faster method of achieving this bit check?  
> Thank you

As in SQL, 0 does not yield false. SQL’s NULL is different from  
0 and from ‘’. So is in Ruby `nil’ different from 0, from “”, and  
from [], and further from {}. In C you’re free to write

if (num % 7) { … }

That won’t work in Ruby.

`Numeric#nonzero?' has been mentioned. Of course, I cannot resist and just mention my beloved proposal of`String#notempty?’,  
`Array#notempty?', and`Hash#notempty?’.

`Bertram::NotEmpty#insists?' still returns`true’.

Bertram

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [January 5, 2010, 12:05am UTC](https://www.ruby-forum.com/t/bitwise-shift-help/181024/6 "2010-01-05T00:05:24Z")

</div>

Awesome thank you Caleb and Axel.  
I had tried the fixnum but it wasn’t working and then it occurred to me  
that it was being read in as a string. Thank you both for the quick  
answers. I appreciate the help.

Caleb C. wrote:

> On 1/3/10, Kvetch K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Kvetch K. wrote:
> > 
> > > Hello, I am new to Ruby and was wondering if there is an easier/faster  
> > > way to determine if a bit is set in a number. For example,  
> > > [snip]
> > 
> > Actually I suppose the bit check should read  
> > if ( x & y ) \>= 1  
> > Or I am still totally off.
> 
> There’s nonzero?, which ought to be a tad faster than \>=1. So, write  
> this instead:
> 
> if ( x & y ).nonzero?
> 
> Or, if you incorporate Axel’s suggestion to use Fixnum#[] as well, then:
> 
> if x[3].nonzero?
