### Java Programming Assignment Help - Stanford University USA| Java Programming Homework Help - Stanford University USA | Java Programming Project Help - Stanford University USA | Online Java Programming Help - Stanford University USA

Student asked about operators in Help with Java Programming Assignment for Java programming language . Operators in java are that perform bit wise and bit shift operations on integral types.
Operator Description are as follows :
~ Inverts a bit pattern
<< Perform Left shift
>> Perform Right Shift
>>> Perform unsigned right shift
& Bit wise AND
| Bit wise OR
^ Bit wise Exclusive OR

Before discuss about the Bit wise operators, will discuss about Binary representations of a number

1's Complement
1's complement simply inverts the bits, if the bit is set to zero, it sets the bit to 1 and if the bit is set to 1 it sets the bit to zero.

Ex:
8 in binary = 0 0 0 0 1 0 0 0
1's complement(8) = 1 1 1 1 0 1 1 1

2's complement
Negative numbers are stored in 2's complement form. i.e., -8 is 2's complement of 8.
2's complement is sum of (1's complement of a number + 1)

Ex:
8 in binary = 0 0 0 0 1 0 0 0
1's complement(8) = 1 1 1 1 0 1 1 1
2's complement(8) = (+) 1
-----------------
1 1 1 1 1 0 0 0
----------------

there for -8 in 2's complement form is 11111000

Inversion (~)
Inversion operator change the bit value to zero, if it is one and change the bit value to one if it is zero.

Example
byte a = 7;

a in binary format is 0 0 0 0 0 1 1 1
~(a) in binary format is 1 1 1 1 1 0 0 0
~(a) represents -8 in binary format.

Example
class BitWiseEx{
public static void main(String args[]){
byte byteVar = 7;

System.out.println("Value of Byte variable is " + byteVar);
System.out.println("Value of Byte variable after inversion is " + ~(byteVar));
}
}

Output
Value of Byte variable is 7
Value of Byte variable after inversion is -8

Bit Wise Left Shift(<<)
Left shift operator simply shifts all the bits to one bit left, i.e, it simply doubles the value.

Syntax:
variable << n

left shift the variable by 'n' number of times.

Variable << n is equals to variable = variable * 2 power n

int var = 2;
var << 1 = 2 * 2 power 1 = 4
var << 2 = 2 * 2 power 2 = 8
var << 9 = 2 * 2 power 9 = 1024

Example
class BitWiseEx{
public static void main(String args[]){
byte byteVar = 7;

System.out.println("Value of Byte variable is " + byteVar);
System.out.println("After shifting 1 bit left " + (byteVar<<1));
System.out.println("After shifting 2 bits left " + (byteVar<<2));
System.out.println("After shifting 3 bits left " + (byteVar<<3));
System.out.println("After shifting 4 bits left " + (byteVar<<4));
System.out.println("After shifting 5 bits left " + (byteVar<<5));
System.out.println("After shifting 6 bits left " + (byteVar<<6));
System.out.println("Value of Byte variable is " + byteVar);
}
}

Output
Value of Byte variable is 7
After shifting 1 bit left 14
After shifting 2 bits left 28
After shifting 3 bits left 56
After shifting 4 bits left 112
After shifting 5 bits left 224
After shifting 6 bits left 448
Value of Byte variable is 7

Bit Wise Right Shift(>>)
Right shift operator simply shifts all the bits to one bit right, i.e, it simply half (divide by 2) the value

Syntax:
variable >> n
right shift the variable by 'n' number of times.

Variable >> n is equals to variable = variable / 2 power n

int var = 128;
var >> 1 = 128 / (2 power 1) = 64
var >> 2 = 128 / (2 power 2) = 32

Example
class BitWiseEx{
public static void main(String args[]){
int intVar = 128;

System.out.println("Value of int variable is " + intVar);
System.out.println("After shifting 1 bit Right " + (intVar>>1));
System.out.println("After shifting 2 bits Right " + (intVar>>2));
System.out.println("After shifting 3 bits Right " + (intVar>>3));
System.out.println("After shifting 4 bits Right " + (intVar>>4));
System.out.println("After shifting 5 bits Right " + (intVar>>5));
System.out.println("After shifting 6 bits Right " + (intVar>>6));
System.out.println("After shifting 6 bits Right " + (intVar>>7));
System.out.println("Value of int variable is " + intVar);
}
}

Output
Value of int variable is 128
After shifting 1 bit Right 64
After shifting 2 bits Right 32
After shifting 3 bits Right 16
After shifting 4 bits Right 8
After shifting 5 bits Right 4
After shifting 6 bits Right 2
After shifting 6 bits Right 1
Value of int variable is 128

Bit Wise unsigned Right Shift(>>>)
shifts all the bits to one bit right, and make the left most bit to zero
class BitWiseEx{
public static void main(String args[]){
int intVar = -128;

System.out.println("Value of int variable is " + short);
System.out.println("After shifting 1 bit Right " + (short>>>1));
}
}

Output
Value of int variable is -128
After shifting 1 bit Right 2147483584

Explanation
-128 in 2's complement form is

11111111 11111111 11111111 10000000

>>> shifts the one bit right and replace leftmost bit by zero

so (-128>>>1) is like below sequence

01111111 11111111 11111111 11000000

Bit Wise AND(&), OR(|), Exclusive OR(^)

Bit 1 Bit 2 & | ^
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Example
class BitWiseEx{
public static void main(String args[]){
byte a = 8;
byte b = 9;

System.out.println("a & b is " + (a&b));
System.out.println("a | b is " + (a|b));
System.out.println("a ^ b is " + (a^b));
}
}

Output
a & b is 8
a | b is 9
a ^ b is 1

Explanation
a = 0000 1000
b = 0000 1001
a&b = 0000 1000

a = 0000 1000
b = 0000 1001
a|b = 0000 10001

a = 0000 1000
b = 0000 1001
a^b = 0000 0001

Note:
Bit wise operators are applicable only to integer values like byte, short etc., these are not applicable to float, double, Boolean.

Assignment Operator
Assignment operator is used to assign a value to a variable.
Syntax
type variable = value;

Example
int a = 10.
int b = 20

Java supports compound assignments
Compound Assignment
Syntax:
variable operator= value

Example:
lets say
int a = 10;
a+=10 evaluates to a=a+10

Compound Operator Example Description
+= a+=10 a=a+10
-= a-=10 a=a-10
*= a*=10 a=a*10
/= a/=10 a=a/10
Example Program
class CompoundAssignment{
public static void main(String args[]){
int var1 = 100;
int sum=10, sub=10, mul=10, div=1000;

System.out.println("sum+var1 is " + (sum+=var1));
System.out.println("sub-var1 is " + (sub-=var1));
System.out.println("mul*var1 is " + (mul*=var1));
System.out.println("div/var1 is" + (div/=var1));
}
}

Output
sum+var1 is 110
sub-var1 is -90
mul*var1 is 1000
div/var1 is10

The Type Comparison Operator instanceof
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

Example
class InstanceOfEx{
public static void main(String args[]){
Object obj = new Object();
System.out.println(obj instanceof Object);
System.out.println(obj instanceof InstanceOfEx);
}
}
Output
true
false

Ternary operator( ?: )
Returns one of two expressions depending on a condition.

Syntax
test ? expression1 : expression2
Returns expression1 if the “test” condition evaluates to true other wise returns expression 2.

Example
class TernaryEx{
public static void main(String args[]){
int a = 100;

int flag = a>10 ? 1 : 0;
System.out.println(flag);

flag = a>1000 ? 1 : 0;
System.out.println(flag);
}
}
Output
1
0

Observation
See the statement “flag = a>10 ? 1 : 0”, a>10 is true, so 1 is returned and assigned to the flag

### Our Team

We have 2500+ expert pool from all over the world. They are highly qualified and well experienced. All experts have experinced in teaching and good command over programming topics. Some of our experts are as follows:

#### John Doe

JAVA EXPERT(10+ Yrs Exp.)

#### Larry Doe

C EXPERT(12+ Yrs Exp.)

#### Ranith Kays

C++ EXPERT(8+ Yrs Exp.)

#### Joan Ray

OPEN SOURCE LANGUAGE EXPERT(8+ Yrs Exp.)