Ternary and Bitwise operators.

In this article,Ternary and Bitwise operators is explained in detail with examples.
It is also known as a conditional operator.Ternary means three values.The operator is '?'.
Syntax
(expre1)?(expre2):(expre3)
If expre1 is true then expre2 is executed. If expre1 is false then expre3 is executed.
Ex
//program related to Ternary and Bitwise operators-ternary operator.
class TernaryOperatorDemo
{
public static void main(String args[])
{
int a=10,b=20;
System.out.println("Using ternary operator:");
int c=(a>b)?a:b;
System.out.println("Biggest of two numbers is:"+c);
int c=(a<b)?(a=a+10):(b=b+10);
System.out.println("A is: "+a);
System.out.println("B is: "+b);
System.out.println("C is: "+c);
}
}
Output
Using First integer values.
Ternary1-output-javaform

Using the second integer value.
Ternary2-output-javaform

Bit wise Operator.
These operators are used to perform operations on bit levels of a given numbers ( Binary representation of a number).
* These operators work only on integer values. (From topic Ternary and Bitwise operators)
The operators are
  • & ---Bitwise AND
  • |   ---Bitwise OR
  • ^  ---Bitwise XOR
  • ~  ---Bitwise Compliment
  • << --Bitwise Left-shift
  • >> --Bitwise Right-shift
  • >>>-Bitwise Right shift with zero-fill. 
Truth table
Ternary and Bitwise operators Javaform

Explanation of Bitwise operation.
S.C- Short cut.
Given values
Binary format of given numbers
  128 64 32 16 8 4 2 1
Remarks
Byte a=10;
Byte b=18;
    0  0    0    0 1 0 1 0
    0  0    0    1 0 0 1 0

a&b
a|b
a^b
~a
    0  0    0    0 0 0 1 0
    0  0    0    1 1 0 1 0
    0  0    0    1 1 0 0 0
    1  1    1    1 0 1 0 1
//If most significant bit is 1 then result is negative value//
=2 // in binary form value is 2.
=26
=24
=128+64+32+16+4+1=245 //range of byte is(-128 to 127)beyond that values are recycled so value of a=-11. Or S.c: 245-256=-11 or S.c=~(n+1).
a<<2              0 0

b>>2
    0  0    1    0 1 0 0 0

    0  0    0    0 0 1 0 0
= 40 // a<<2 means move 2 bits left,and at end remaining places put zeros. S.c:a*2bit
1 0 =4 // b>>2 means move two bits right side remaining places are placed by zeros denoted in red color,they are neglected. S.C: a/2bit .
a=-11  a<<2   1 1 

a=-11  a>>2
    1  1    0    1 0 1 0 0

    1  1     1   1 1 1 0 1 
128+64+16+4=212 // move 2 bits left side S.c= -11*22  =-44.
0 1 =-3 //128+64+32+16+8+4+1=253|
S.c:253-256=-3.

a=-11   a>>>2 //only in java with zero fill
    0 0      1   1 1 1 0 1
0 1=61 // 32+16+8+4+1.

Ex
//Program related to Ternary and Bitwise operators- Bitwise operation.
class BitwiseOperators
{
public static void main(String args[])
{
int x=10,y=18;
System.out.println("x is: "+x);
System.out.println("y is: "+y);
System.out.println("x&y is: "+(x&y));
System.out.println("x/y is: "+(x/y));
System.out.println("x^y is: "+(x^y));
System.out.println("~x is: "+(~x));
System.out.println("~y is: "+(~y));
x=10;y=18;
System.out.println("x is: "+x);
System.out.println("y is: "+y);
System.out.println("x<<2 is: "+(x<<2));
System.out.println("y<<2 is: "+(y<<2));
System.out.println("x>>2 is: "+(x>>2));
System.out.println("y>>2 is: "+(y>>2));
x=-11;y=-19;
System.out.println("x<<2 is: "+(x<<2));
System.out.println("y<<2 is: "+(y<<2));
System.out.println("x>>2 is: "+(x>>2));
System.out.println("y>>2 is: "+(y>>2));
x=18;y=-11;
System.out.println("x>>2 is: "+(x>>2));
System.out.println("y>>2 is: "+(y>>2));
}
}
Output
x is: 10
y is: 18
x&y is: 2
x/y is: 0
x^y is: 24
~x is: -11
~y is: -19
x is: 10
y is: 18
x<<2 is: 40
y<<2 is: 72
x>>2 is: 2
y>>2 is: 4
x<<2 is: -44
y<<2 is: -76
x>>2 is: -3
y>>2 is: -5
x>>2 is: 4
y>>2 is: -3
Bitwise operator-javaform
                                To get code file use me.

Continue to next topic String addition and Special Operators in Java.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

 Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabad, surprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required