** Operators**

Operators:

  • Bitwise, logical,arithmetic and logical
  • Instance and arrow operator.(newly introduced)

Arithmetic operations:

  • unary minus operand is used to negates its single operand.
  • unary plus operand is used to return the value of operand.
  • Modulus,increment, Decrement,addition assignment,subtraction assignment,multplication assignment.modulus assignment

30/4/17

Arithmetic compound with assignement operator:

  • Java provides a special operator that can be used to combine an arithmetic operation with assignment.
  • The statement of form var=var op expression can be written as var op=expression.
  • The statement can be written from a=a+4 as a+=4
  • It uses compound assignment operator as increase the value a by 4
  • It provide two benefits as
  • It save you bit of type as shorthand for equivalent long forms are used.
  • It is more efficient than are their equivalent long forms.

Bitwise logical operator:

NOT

  • Not operator is inverts all of bits operand.
  • It simply inverse values.
  • 00001010 has changed to 11110101

AND(&)

  • It produce operands to 1 in each cases has 1 or else 0.
  • It has 42 as 00101010
  • & has 15 as 00001111
  • 0001010 as AND operator whose value for the number 10.

OR(|)

  • Combines operands were either of the bits as 1 then resultant 1.

XOR(^)

  • Exactly one operand as 1.or else 0.

Examples:

package com;
public class project {
public static void main(String args[])
{
String binary[] = {"0000","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
int a= 3;
int b= 6;
int c= a | b;
int d= a & b;
int e= a ^ b;
int f= (~a & b) | (a & ~ b);
int g= ~ a & 0x0f;
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 & 0x0f =" + binary[g]);
}
}

Left Shift:

  • The left operand value is moved left by the number of bits specified by the right operand.

  • << it shift all of the bits in the value to the left a specified number of times

  • For each shift high order bit is shifted out and zero is brought on right.

  • In at left shift is applied on int operand these operator zero is brought as one less than bit and then for int as 31 in bit position and long as 63 in bit position.

  • In value a contain 64 as twice 256 and b value contains 0 as low order byte is 0 after the shift.

  • The value b contains 0 after the shift and lower order byte is 0 and its only 1 bit has shifted out.

  • Examples:

public class project {
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 );
}
}

Right shift:

  • The left operand value is moved right by the number of bits specified by the right operand.

  • >> it shift all of the bits in the value to the right specified number of times.

  • value>> num

  • int a=32 and a=a >> 2. a contains 8.

  • num specifies the number of positions in the right shift in value.

Unsigned right shift operator:

  • The >> operator automatically fills the with its previous contents each time a shift occurs.
  • This preserve the sign of the value.
  • The numeric value may not want the sign extension takes place and working on pixel based graphics and generally want to shift a zero in higher order bit.
  • This set a 255 sets to 32 bits in ignoring a sign extension.

Relational operator:

  • It determines the relationship between one operand has to the other.
  • Java includes the floating point numbers,characters and booleans can be compared with equality test and inequality test
  • In Java equality is denoted by two equal signs not one.
  • The outcome of the operator produce boolean value.
  • In Java true and false values are non-numeric value that do not relate to zero or non zero.

The ? operator:

  • It includes special three way operator that can replace certain types of if-then-else statement.
  • It has general three forms
  • Expression1: It can be any expression that evaluates to a boolean value.
  • If expression1 is true then expression 2 is evaluated else otherwise expression 3 is evaluated.

Using parentheses:

  • parentheses is the precedence of the operations that are inside them.These is often necessary to obtain the result.
  • a >> b +3
  • It add 3 first to b and then shifts a right by the result.
  • In addition to normal precedence the parentheses sometimes help to clarify the meaning of expression.
  • (a |4(((4+c)>> b) & 7)) is easiest form by using parentheses compared to normal form a+4 + c >> b & 7..

results matching ""

    No results matching ""