# Irb: 1 & 1 =\> 1, not true huh? I thought 1 & 1 =\> true

**URL:** <https://www.ruby-forum.com/t/irb-1-1-1-not-true-huh-i-thought-1-1-true/149719>\
**Category:** Ruby\
**Created:** [September 28, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/irb-1-1-1-not-true-huh-i-thought-1-1-true/149719 "2008-09-28T23:19:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [September 28, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/irb-1-1-1-not-true-huh-i-thought-1-1-true/149719/1 "2008-09-28T23:19:47Z")

</div>

In irb I did:

puts 1 & 1  
and get 1  
shouldn’t I get true?

Bottom line is this: What is Ruby idiom to test for bit mask? Is there  
a more simple way to write:

if session[:permissions] & Permissions::SomeMask ==  
Permissions::SomeMask

# user has permission to wash clothes, etc.

end

TIA,  
Pete

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [September 28, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/irb-1-1-1-not-true-huh-i-thought-1-1-true/149719/2 "2008-09-28T23:33:17Z")

</div>

Peter A. wrote:

> Permissions::SomeMask
> 
> # user has permission to wash clothes, etc.
> 
> end

a && b returns the first false element or the last true element.

a & b where a and b are Fixnums does a bit mask like you’re looking for.

- Charlie

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [September 28, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/irb-1-1-1-not-true-huh-i-thought-1-1-true/149719/3 "2008-09-28T23:40:21Z")

</div>

On Sep 28, 4:19 pm, Peter A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Permissions::SomeMask
> 
> # user has permission to wash clothes, etc.
> 
> end
> 
> TIA,

You say thanks when nobody has replied, so  
you are thanking nobody. You should wait  
till someone has replied before you thank  
him. Otherwise, he won’t even know that you  
have read his post; that is very rude and  
inconsiderate.

> Pete

# bitwise

254 & 15  
==\>14

# boolean

254 && 15  
==\>15

Only two things are untrue: nil and false.

puts “it’s true” if 0  
it’s true  
==\>nil  
puts “it’s true” if 254 & 1  
it’s true  
==\>nil  
puts “it’s true” if 254 & 1 \> 0  
==\>nil

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [September 29, 2008, 10:09am UTC](https://www.ruby-forum.com/t/irb-1-1-1-not-true-huh-i-thought-1-1-true/149719/4 "2008-09-29T10:09:20Z")

</div>

> Bottom line is this: What is Ruby idiom to test for bit mask? Is there  
> a more simple way to write:
> 
> if session[:permissions] & Permissions::SomeMask ==  
> Permissions::SomeMask
> 
> # user has permission to wash clothes, etc.
> 
> end

That’s basically right - you can factor this out into your own method if  
you do it a lot.

If you want to test for a single bit, there is Fixnum#[]

a = 14  
a[0] # 0  
a[1] # 1  
a[2] # 1  
a[3] # 1  
a[4] # 0

You’d still have to test for a[2] == 1, since 0 and 1 are both true.

However you could use a different representation for your permissions,  
such as a string containing a single letter for each permission granted.  
Then:

if session[:permissions].include?(“w”)  
# user has permission to wash clothes  
end

or simply

if session[:permissions][“w”]  
# user has permission to wash clothes  
end

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [September 29, 2008, 5:43am UTC](https://www.ruby-forum.com/t/irb-1-1-1-not-true-huh-i-thought-1-1-true/149719/5 "2008-09-29T05:43:05Z")

</div>

Hi,

At Mon, 29 Sep 2008 06:19:01 +0900,  
Peter A. wrote in [ruby-talk:316270]:

> puts 1 & 1  
> and get 1  
> shouldn’t I get true?

No. If it were, bit AND operation would be unavailable.

> if session[:permissions] & Permissions::SomeMask ==  
> Permissions::SomeMask

if (session[:permissions] & Permissions::SomeMask).nonzero?
