Reputation: 7400
Recently, I observed a very interesting result in Ruby while making use of && and & for the input combination of 0 & 1.
I want to understand the below output with respect to the above mentioned two operators. This is implemented using Ruby 2.0.0-p451
2.0.0-p451 :006 > 0 && 1
=> 1
2.0.0-p451 :008 > 0 & 1
=> 0
Upvotes: 4
Views: 7851
Reputation: 1066
&& is a Logical operator which will always return a boolean value.
z
It's either true if both of the values are true
true && true
and will return false if any of the value is false
false && true
Now comes to second point & Operator. Here are two answer for the & Operator.
& is a Bitwise operator as mentioned in above answers, It compares bit representation in the form of 0 and 1.
Another Important answer is, In upgradation of Ruby 2.6 its implementation is somewhat modified.
For ex:
[1, 2, 3] & [2, 3, 4]
$ [2, 3]
Upvotes: 0
Reputation: 1837
&&
is logical and. Which is to say, a && b
returns true if a is true and b is true. &
is bitwise and. In (almost) any other language, logical and of 0 and 1 would be 0, because (almost) all other languages consider 0 to be false. But in ruby, anything except nil
and false
are considered to be truthy.
Upvotes: 0
Reputation: 107142
&&
is a boolean and
. It returns the second argument if the first argument is true-ish. Because 0
is true-ish in Ruby, 1
is returned.
&
is a bitwise and
. It compares the bit representation of the values. Because (imaging 8 bit) 00000000
(0
) and 00000001
(1
) have no 1
digits in common, 00000000
(0
) is returned.
Upvotes: 11
Reputation: 369614
&&
is the logical AND operator. It will be truthy, IFF both operands are truthy. And it is lazy (aka short-circuiting), which means it will stop evaluating as soon as the result has been fully determined. (So, since both operands need to be truthy, if the first operand is falsy, you already know that the result is going to be falsy, without even evaluating the second operand.) It will also not just return true
or false
, but rather return the operand which determines the outcome. IOW: if a
is falsy, it'll return a
, otherwise it'll return b
:
nil && (loop {})
# => nil
# Note: the infinite loop is *not* evaluated
# Note: the return value is nil, not false
true && nil
# => nil
true && 'Hello'
# => 'Hello'
&
simply calls the method &
. It will do whatever the object wants it to do:
def (weirdo = Object.new).&(other)
puts 'Whoah, weird!'
'Hello, ' + other
end
weirdo & 'World'
# Whoah, weird!
# => 'Hello, World'
In general, &
and its brother |
are expected to perform conjunction and disjunction. So, for booleans, they are perform AND and OR (TrueClass#&
, FalseClass#&
, NilClass#&
, TrueClass#|
, FalseClass#|
, NilClass#|
) with the exception that &
and |
are standard method calls and thus always evaluate their argument and that they always return true
or false
and not their arguments.
For Set
s, they perform set intersection and set union: Set#&
, Set#|
. For other collections (specifically Array
s), they also perform set operations: Array#&
, Array#|
.
For Integer
s, they perform BITWISE-AND of the two-complement's binary representation: Fixnum#&
, Bignum#&
, Fixnum#|
, Bignum#|
.
Upvotes: 12
Reputation: 78571
One is a boolean operator, the other is a bitwise operator:
# Bitwise operators
a = 78 # 01001110
b = 54 # 00110110
puts (a&b) # 00000110 = 6
puts (a|b) # 01111110 = 126
puts (a^b) # 01111000 = 120
puts (~a) # 10110001 = -79
puts (a<<2) # 00111000 = 312
puts (a>>2) # 00010011 = 19
http://www.public.traineronrails.com/courses/ruby/pages/008-rubyoperators.html
Upvotes: 0
Reputation: 2003
This page gives a good explanation of the different operators in Ruby.
&&
is logical AND operator in ruby.
> a = true
> b = true
> c = false
> a && b
=> true
> a && c
=> false
&
is the bitwise AND operator in ruby. Per the wikipedia article on the description for the "bitwise AND operator":
A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 and the second bit is 1; otherwise, the result is 0.
Upvotes: 0