Operators:
- Operators are symbols and used to perform some mathematics operations.
- There are 6 kinds of operators,
package exampleprogram;
import java.util.Scanner;
public class Operator {
public
static void main(String[] args){
Scanner
input = new Scanner(System.in);
int
a,b;
System.out.println("\t\t
Arithmetic Operator.... ");
System.out.println("Enter two value
: ");
a =
input.nextInt();
b =
input.nextInt();
System.out.println("Add value is : "+(a+b));
System.out.println("Sub value is : "+(a-b));
System.out.println("Mul value is : "+(a*b));
System.out.println("Div value is : "+(a/b));
System.out.println("Mod value is : "+(a%b));
System.out.println("Post increament of a value is : "+(a++));
System.out.println("Pre increament of a value is : "+(++a));
System.out.println("Post decreament of b value is : "+(b--));
System.out.println("Pre increament of a value is : "+(--b));
}
}
package exampleprogram;
import java.util.Scanner;
public class Operator {
public
static void main(String[] args){
Scanner
input = new Scanner(System.in);
int
a,b;
System.out.println("\t\t
Arithmetic Operator.... ");
System.out.println("Enter two value
: ");
a =
input.nextInt();
b =
input.nextInt();
System.out.println("Add value is : "+(a+b));
System.out.println("Sub value is : "+(a-b));
System.out.println("Mul value is : "+(a*b));
System.out.println("Div value is : "+(a/b));
System.out.println("Mod value is : "+(a%b));
System.out.println("Post increament of a value is : "+(a++));
System.out.println("Pre increament of a value is : "+(++a));
System.out.println("Post decreament of b value is : "+(b--));
System.out.println("Pre increament of a value is : "+(--b));
}
}
- It is used to compare two or more data.
- These logical operators mostly used in conditions.
- It is used to combine simple relational statements into more complex expressions.
- These operators are used to getting the result in the same variable(input/output).
package exampleprogram;import java.util.Scanner;public class Operator { public static void main(String[] args){ Scanner input = new Scanner(System.in); int a,b; System.out.println("\t\t Assignment Operator.... "); System.out.println("\nEnter a value : "); a = input.nextInt(); a+=10; System.out.println("Now a value is : "+a); } }
- It checks itself the condition and executes the statement depending on the condition.
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\t\t
Conditional Operator.... ");
System.out.println("\nEnter a and b value
: ");
a = input.nextInt();
b = input.nextInt();
int big=a>b?a:b;
System.out.println("Biggest value is : "+big);
}
}
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\t\t
Conditional Operator.... ");
System.out.println("\nEnter a and b value
: ");
a = input.nextInt();
b = input.nextInt();
int big=a>b?a:b;
System.out.println("Biggest value is : "+big);
}
}
- It is used to manipulate the data at a bit level and it operates on integer only.
- The truth table for ‘ & ‘is shown below(It means True=1 and false=0.)
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\t\t
Bitwise AND Operator.... ");
System.out.println("\nEnter a and b value : ");
a = input.nextInt();
b = input.nextInt();
int c=a&b;
System.out.println("Bitwise AND of a&b value is : "+c);
}
}
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\t\t
Bitwise AND Operator.... ");
System.out.println("\nEnter a and b value : ");
a = input.nextInt();
b = input.nextInt();
int c=a&b;
System.out.println("Bitwise AND of a&b value is : "+c);
}
}
- The truth table for ‘ | ‘is shown below
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\t\t
Bitwise OR Operator.... ");
System.out.println("\nEnter a and b
value : ");
a = input.nextInt();
b = input.nextInt();
int c=a|b;
System.out.println("Bitwise OR of a|b value is : "+c);
}
}
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\t\t
Bitwise OR Operator.... ");
System.out.println("\nEnter a and b
value : ");
a = input.nextInt();
b = input.nextInt();
int c=a|b;
System.out.println("Bitwise OR of a|b value is : "+c);
}
}
- The truth table for ‘ ^ ‘is shown below
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\t\t
Bitwise Exclusive OR Operator.... ");
System.out.println("\nEnter a and b value : ");
a = input.nextInt();
b = input.nextInt();
int c=a^b;
System.out.println("Bitwise Exclusive OR of a^b value is :
"+c);
}
}
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\t\t
Bitwise Exclusive OR Operator.... ");
System.out.println("\nEnter a and b value : ");
a = input.nextInt();
b = input.nextInt();
int c=a^b;
System.out.println("Bitwise Exclusive OR of a^b value is :
"+c);
}
}
- The bitwise shift left operator is used to move the bits from right to left in storage.
- Each box called bits and a=6 and a<<1 then a=12.
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a;
System.out.println("\t\t
Bitwise left shit Operator.... ");
System.out.println("\nEnter a value
: ");
a = input.nextInt();
int c=a<<1;
System.out.println("Bitwise left shit of a value is : "+c);
}
}
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a;
System.out.println("\t\t
Bitwise left shit Operator.... ");
System.out.println("\nEnter a value
: ");
a = input.nextInt();
int c=a<<1;
System.out.println("Bitwise left shit of a value is : "+c);
}
}
- The shit right is similar to the left shit.
- This operator is used to forward the bits from left to right in storage.
- For example, If a=6 and a>>1 then its value is 3.
Result: a of right shit value is 3.
Example program for the bitwise shift right operator:
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a;
System.out.println("\t\t
Bitwise right shit Operator.... ");
System.out.println("\nEnter a value
: ");
a = input.nextInt();
int c=a>>1;
System.out.println("Bitwise right shit of a value is : "+c);
}
}
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a;
System.out.println("\t\t
Bitwise right shit Operator.... ");
System.out.println("\nEnter a value
: ");
a = input.nextInt();
int c=a>>1;
System.out.println("Bitwise right shit of a value is : "+c);
}
}
- The truth table for ‘ ~ ‘is shown below
If a=6(binary code is 0110),
Example program for the bitwise one's complement operator:
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a;
System.out.println("\t\t
Bitwise one\'s complement Operator.... ");
System.out.println("\nEnter a value
: ");
a = input.nextInt();
int c=~a;
System.out.println("Bitwise one\'s complement of a value is :
"+c);
}
}
package exampleprogram;
import java.util.Scanner;
public class Operator {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a;
System.out.println("\t\t
Bitwise one\'s complement Operator.... ");
System.out.println("\nEnter a value
: ");
a = input.nextInt();
int c=~a;
System.out.println("Bitwise one\'s complement of a value is :
"+c);
}
}
No comments:
Post a Comment