[go: up one dir, main page]

0% found this document useful (0 votes)
5 views35 pages

Module2 Operators1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views35 pages

Module2 Operators1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Operators

Arithmetic Operators
• The operands of the arithmetic operators
must be of a numeric type.
• cannot use them on boolean types,
• but can use them on char types
class BasicMath { // arithmetic using doubles
public static void main(String System.out.println("\nFloating
args[ ]) { Point Arithmetic");
// arithmetic using integers double da = 1 + 1;
System.out.println("Integer double db = da * 3;
Arithmetic"); double dc = db / 4;
int a = 1 + 1; double dd = dc - a;
int b = a * 3; double de = -dd;
int c = b / 4; System.out.println("da = " + da);
int d = c - a; System.out.println("db = " + db);
int e = -d; System.out.println("dc = " + dc);
System.out.println("a = " + a); System.out.println("dd = " + dd);
System.out.println("b = " + b); System.out.println("de = " + de);
System.out.println("c = " + c); }
System.out.println("d = " + d); }
System.out.println("e = " + e);
The Modulus Operator
It can be applied to floating-point types as
well as integer types

class Modulus {
public static void main(String args[]) {
int x = 42;
double y = 42.25;

System.out.println("x mod 10 = " + x % 10);


System.out.println("y mod 10 = " + y % 10);
}
}
Arithmetic Compound Assignment
Operators
• var op= expression;

• a += 5;
• b *= 4;
• c += a * b;
• c %= 6;
Increment and Decrement
class IncDec {
public static void main(String args[])
{
int a = 1;
int b = 2;
int c;
int d;

c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
The Bitwise Operators
• All of the
integer types
(except char)
are signed
integers.
• This means
that they can
represent
negative
values as well
as positive
ones.
• the byte value for 42 in binary is
• 00101010
• thus, 42 is the sum of 21 + 23 + 25, which is 2 +
8 + 32.

• Java uses an encoding known as two’s


complement, which means that negative
numbers are represented by inverting
(changing 1’s to 0’s and vice versa) all of the
bits in a value, then adding 1 to the result
• –42 is represented by inverting all of the bits
in 42,
• or 00101010, which yields 11010101,
• then adding 1, which results in 11010110, or
–42.

• To decode a negative number, first invert all


of the bits, then add 1.
• For example, –42, or 11010110 inverted,
yields 00101001, or 41, so when you add 1
you get 42.
• The reason Java uses two’s complement is easy to
see when you consider the issue of zero crossing.
• Assuming a byte value, zero is represented by
00000000. In one’s complement, simply inverting all
of the bits creates 11111111, which creates
negative zero.
• This problem is solved by using two’s complement
to represent negative values.
• 1 is added to the complement, producing
100000000. This produces a 1 bit too far to the left
to fit back into the byte value, resulting in the
desired behavior, where –0 is the same as 0, and
• 11111111 is the encoding for –1.
The Bitwise Logical Operators
• The bitwise logical operators are &, |, ^, and ~.
class BitLogic {
public static void main(String args[]) {
String binary[ ] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111“ };
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f; //0x0f is 0000 1111 in binary
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
} }
The Left Shift
• The left shift operator, <<, shifts all of the bits in
a value to the left a specified number of times.
• It has this general form:
value << num
• Here, num specifies the number of positions to
left-shift the value in value.

• Java’s automatic type promotions produce


unexpected results when you are shifting
byte and short values are promoted to int when an expression
is evaluated.

// Left shifting a byte value.


class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;

i = a << 2;
b = (byte) (a << 2);

System.out.println("Original value of a: " + a);


System.out.println("i and b: " + i + " " + b);
}
}
// Left shifting as a quick way to multiply by 2.
class MultByTwo {
public static void main(String args[]) {
int i;
int num = 0xFFFFFFE;

for(i=0; i<4; i++) {


num = num << 1;
System.out.println(num);
}
}
}
The Right Shift

• The right shift operator, >>, shifts all of the


bits in a value to the right a specified number
of times.
• Its general form is shown here:
value >> num
• When you are shifting right, the top (leftmost)
bits exposed by the right shift are filled in with
the previous contents of the top bit. This is
called sign extension and serves to preserve
the sign of negative numbers when you shift
them right
// Masking sign extension.
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;

System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);


}
}

Here is the output of this


program:
b = 0xf1
The Unsigned Right Shift
• want to shift a zero into the high-order bit no
matter what its initial value was.
• This is known as an unsigned shift.
• To accomplish this, you will use Java’s
unsigned, shift-right operator, >>>, which
always shifts zeros into the high-order bit.
int a = -1;
a = a >>> 24;
11111111 11111111 11111111 11111111
–1 in binary as an int
>>>24
00000000 00000000 00000000 11111111
255 in binary as an int
The >>> operator is only meaningful for 32- and 64-bit values.
Remember, smaller values are automatically promoted to int
in expressions. This means that sign-extension occurs and
that the shift will take place on a 32-bit rather than on an 8-
or 16-bit value.
// Unsigned shifting a byte value.
class ByteUShift {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
}; b = 0xf1
byte b = (byte) 0xf1; b >> 4 = 0xff
byte c = (byte) (b >> 4); b >>> 4 = 0xff
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);
(b & 0xff) >> 4 = 0x0f

System.out.println(" b = 0x"
+ hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
System.out.println(" b >> 4 = 0x"
+ hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b >>> 4 = 0x"
+ hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff) >> 4 = 0x"
+ hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);
}
}
Bitwise Operator Compound
Assignments
class OpBitEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;

a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
Relational Operators

•The outcome of these operations is a boolean value.


•The relational operators are most frequently used in the
expressions that control the if statement and the various loop
statements.
•Any type in Java, including integers, floating-point numbers,
characters, and Booleans can be compared using the equality test,
==, and the inequality test, !=.
int a = 4;
int b = 1;
boolean c = a < b;

int done;
// ...
if(!done) ... // Valid in C/C++
if(done) ... // but not in Java.

In Java, these statements must be written like this:


if(done == 0) ... // This is Java-style.
if(done != 0) ...

• In Java, true and false are nonnumeric values that do


not relate to zero or nonzero
Boolean Logical Operators
• The logical Boolean operators, &, |, and ^, operate on
boolean values in the same way that they operate on the
bits of an integer. The logical ! operator inverts the Boolean
state: !true == false and !false == true.
// Demonstrate the boolean logical operators.
class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}}
Short-Circuit Logical Operators (new)
• The secondary versions of the Boolean AND and
OR operators are known as short-circuit logical
operators.
• use the || and && forms, rather than the | and
& forms of these operators
• Java will not bother to evaluate the right-hand
operand when the outcome of the expression
can be determined by the left operand alone.

• if (denom != 0 && num / denom > 10)


The Assignment Operator
• var = expression;

int x, y, z;
x = y = z = 100; // set x, y, and z to 100
• “chain of assignment”
The ? Operator
• ternary (three-way) operator that can replace
certain types of if-then-else statements.
• expression1 ? expression2 : expression3

• Both expression2 and expression3 are required


to return the same type, which can’t be void.

ratio = denom == 0 ? 0 : num / denom;


// Demonstrate ?.
class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
}
Operator Precedence

You might also like