I have seen the use of & in many programming language and since I love groovy a lot I tried the following code to find the use of & :
a = 1 ;
println a & 2
I’m getting the output as 0. When I change the values of a I get different answer’s.
So any one can say whats the use of & in programming languages like Groovy in simple english, possibly with an simple example in any language?
Thanks in advance.
&is usually either bitwise-and (on integer arguments) or non-short-circuiting logical and (on boolean arguments).bitwise-and returns a series of bits (usually represented as an
inttype) that have only the bits in common setThis seems to be what is happening in your Groovy code.
1 & 2 == 0since 1 and 2 share no bits in common.Non-short-ciruiting logical and is similar to
&&butIn languages that allow operator overloading, libraries sometimes overloaded
&to do set intersection, or element-wise bit-intersection.