09.10.2014 Views

Download Scala Tutorial (PDF Version) - Tutorials Point

Download Scala Tutorial (PDF Version) - Tutorials Point

Download Scala Tutorial (PDF Version) - Tutorials Point

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

^<br />

~<br />

><br />

>>><br />

Binary XOR Operator copies the bit if it is set in one operand<br />

but not both.<br />

Binary Ones Complement Operator is unary and has the<br />

efect of 'flipping' bits.<br />

Binary Left Shift Operator. The left operands value is moved<br />

left by the number of bits specified by the right operand.<br />

Binary Right Shift Operator. The left operands value is moved<br />

right by the number of bits specified by the right operand.<br />

Shift right zero fill operator. The left operands value is moved<br />

right by the number of bits specified by the right operand and<br />

shifted values are filled up with zeros.<br />

(A ^ B) will give 49, which is 0011 0001<br />

(~A ) will give -60, which is 1100 0011<br />

A > 2 will give 15, which is 1111<br />

A >>>2 will give 15, which is 0000 1111<br />

Example<br />

The following simple example program demonstrates the bitwise operators. Copy and paste the following <strong>Scala</strong><br />

program in Test.scala file and compile and run this program.<br />

object Test {<br />

def main(args: Array[String]) {<br />

var a = 60; /* 60 = 0011 1100 */<br />

var b = 13; /* 13 = 0000 1101 */<br />

var c = 0;<br />

c = a & b; /* 12 = 0000 1100 */<br />

println("a & b = " + c );<br />

c = a | b; /* 61 = 0011 1101 */<br />

println("a | b = " + c );<br />

c = a ^ b; /* 49 = 0011 0001 */<br />

println("a ^ b = " + c );<br />

c = ~a; /* -61 = 1100 0011 */<br />

println("~a = " + c );<br />

c = a > 2; /* 215 = 1111 */<br />

println("a >> 2 = " + c );<br />

}<br />

}<br />

c = a >>> 2; /* 215 = 0000 1111 */<br />

println("a >>> 2 = " + c );<br />

This would produce the following result:<br />

C:/>scalac Test.scala<br />

C:/>scala Test<br />

a & b = 12<br />

a | b = 61<br />

a ^ b = 49<br />

~a = -61<br />

a > 15<br />

TUTORIALS POINT<br />

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!