Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

JAVA OPERATOR Activity Cont.

Java Comparison Operators


Comparison operators are used to compare two values:

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Sample Code for Comparison:
Equal to. == Not Equal. !=
Sample: Sample:
int x = 20; int x = 30;
int y = 5; int y = 8;
System.out.println(x == y); // returns false because 20 is System.out.println(x != y); // returns true because 30 is
not equal to 5 not equal to 8
Greater than. > Less than. <
Sample: Sample:
int x = 10; int x = 40;
int y = 9; int y = 35;
System.out.println(x > y); // returns true because 10 is System.out.println(x < y); // returns false because 40 is
greater than 9 not less than 35
Greater than or equal to. >= Less than or equal to. <=
Sample: Sample:
int x = 15; int x = 5;
int y = 12; int y = 3;
System.out.println(x >= y); // returns true because 15 is System.out.println(x <= y); // returns false because 5 is
greater, or equal, to 12 neither less than or equal to 3

Java Logical Operators


Logical operators are used to determine the logic between variables or values:
Operator Name Description Example

&& Logical and Returns true if both statements are true x < 5 && x < 10

|| Logical or Returns true if one of the statements is true x < 5 || x < 4

! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)
Sample Code for Logical:
Logical and. && Logical or. ||
Sample: Sample:
int x = 5; int x = 5;
System.out.println(x > 3 && x < 10); // returns true System.out.println(x > 3 || x < 4); // returns true
because 5 is greater than 3 AND 5 is less than 10 because one of the conditions are true (5 is greater than
3, but 5 is not less than 4)
Logical not. !
Sample:
int x = 5;
System.out.println(!(x > 3 && x < 10)); // returns false
because ! (not) is used to reverse the result
Java Bitwise Operator
Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they
will be as follows −
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
The following table lists the bitwise operators −
Assume integer variable A holds 60 and variable B holds 13 then −
Operator Description Example

Binary AND Operator copies a bit to the result if it exists in both


& (bitwise and) (A & B) will give 12 which is 0000 1100
operands.

| (bitwise or) Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101

Binary XOR Operator copies the bit if it is set in one operand but
^ (bitwise XOR) (A ^ B) will give 49 which is 0011 0001
not both.

(~A ) will give -61 which is 1100 0011 in


~ (bitwise Binary Ones Complement Operator is unary and has the effect of
2's complement form due to a signed
compliment) 'flipping' bits.
binary number.

Binary Left Shift Operator. The left operands value is moved left
<< (left shift) A << 2 will give 240 which is 1111 0000
by the number of bits specified by the right operand.

Binary Right Shift Operator. The left operands value is moved right
>> (right shift) A >> 2 will give 15 which is 1111
by the number of bits specified by the right operand.

Shift right zero fill operator. The left operands value is moved right
>>> (zero fill
by the number of bits specified by the right operand and shifted A >>>2 will give 15 which is 0000 1111
right shift)
values are filled up with zeros.

Sample Code for Bitwise Operator


int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
int c = 0;

c = a & b; /* 12 = 0000 1100 */


System.out.println("a & b = " + c );

c = a | b; /* 61 = 0011 1101 */
System.out.println("a | b = " + c );

c = a ^ b; /* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );

c = ~a; /*-61 = 1100 0011 */


System.out.println("~a = " + c );

c = a << 2; /* 240 = 1111 0000 */


System.out.println("a << 2 = " + c );

c = a >> 2; /* 15 = 1111 */
System.out.println("a >> 2 = " + c );

c = a >>> 2; /* 15 = 0000 1111 */


System.out.println("a >>> 2 = " + c );

You might also like