Lara

Bitwise Operator(Sunday)

 

public class AirthmaticOperators {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int i = 20;

                                int j = 10 ;

                               

//                            System.out.println(--j);

//                            System.out.println(j);

 

                               

                                // Additions

                                System.out.println(j--);

                                System.out.println(j);

 

                }

 

}

 

 

public class BinaryString {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

 

                                int number = 8 ;

                                // second way

                                String binaryString = Integer.toString(number, 2);

                                System.out.println("decimal to binary using "

                                                                + "Integer.toString: " + binaryString);

                               

 

                }

 

}

 

 

public class BitShiftTest {

                public static void main(String args[]) {

                                int number = 8; // 0000 1000

                                System.out.println("Original number : " + number);

                                // left shifting bytes with 1 position

                               

                                number = number << 1; // should be 16 i.e. 0001 0000

                               

                                // equivalent of multiplication of 2

                                System.out.println("value of number after left shift: "

                                + number);

                               

                               

                                number = -8;

                                // right shifting bytes with sign 1 position

                                number = number >> 1; // should be 16 i.e. 0001 0000

                                // equivalent of division of 2

                                System.out.println("value of number after right shift with sign: " + number);

                               

                                number = -8;

                                // right shifting bytes without sign 1 position

                                number = number >>> 1; // should be 16 i.e. 0001 0000

               

                                System.out.println("value of number after right shift with sign: " + number);

                }

}

 

class BitwiseAND{

                  public static void main(String args[]){

 

                  int num1 = 42;

                  int num2 = 15;

 

                  System.out.println("AND Result =" +(num1&num2));

 

                  }

                }

 

 

public class BitwiseComplement {

 

                public static void main(String[] args) {

                                int a = 60; /* 60 = 0011 1100 */

                                System.out.println(Integer.toBinaryString(a));

                                int b = 13; /* 13 = 0000 1101 */

                                System.out.println(Integer.toBinaryString(b));

                                int c = 0;

                                int d =0;

 

                                c = ~a; /*-61 = 1100 0011 */

                                d = ~b;

                               

                                System.out.println("~a = "+c);

                                System.out.println("~b = "+d);

                               

                }

 

}

 

 

public class BitwiseOperatorsDemo1 {

                public static void main(String args[]) {

                                // Bitwise Logical Operators

                                System.out.println("********* Bitwise Logical Operators **************");

                                // Bitwise AND Operator

                                System.out.println("**** Bitwise AND Operator *******");

                                int value1 = 6;

                                int value2 = 5;

                                int result = value1 & value2;

                                System.out.println("value1 : " + value1);

                                System.out.println("value2 : " + value2);

                                System.out.println("result : " + result);

                                // Bitwise OR Operator

                                System.out.println("**** Bitwise OR Operator *******");

                                value1 = 6;

                                value2 = 5;

                                result = value1 | value2;

                                System.out.println("value1 : " + value1);

                                System.out.println("value2 : " + value2);

                                System.out.println("result : " + result);

                                // Bitwise Exclusive OR Operator

                                System.out.println("**** Bitwise Exclusive OR Operator *******");

                                value1 = 6;

                                value2 = 5;

                                result = value1 ^ value2;

                                System.out.println("value1 : " + value1);

                                System.out.println("value2 : " + value2);

                                System.out.println("result : " + result);

                                // Bitwise NOT operator

                                System.out.println("**** Bitwise NOT operator *******");

                                value1 = 6;

                                result = ~value1;

                                System.out.println("value1 : " + value1);

                                System.out.println("result : " + result);

                                // Bitwise Shift Operators

                                System.out.println("********* Bitwise Shift Operators **************");

                                // Right Shift Operator

                                System.out.println("**** Right Shift Operator *******");

                                int value = 12;

                                int test = value >> 2;

                                System.out.println("value : " + value);

                                System.out.println("test : " + test);

                                value = -12;

                                test = value >> 2;

                                System.out.println("value : " + value);

                                System.out.println("test : " + test);

                                // Left Shift Operator

                                System.out.println("**** Left Shift Operator *******");

                                value = 12;

                                test = value << 2;

                                System.out.println("value : " + value);

                                System.out.println("test : " + test);

                                value = -12;

                                test = value << 2;

                                System.out.println("value : " + value);

                                System.out.println("test : " + test);

                                // Unsigned Right Shift Operator

                                System.out.println("**** Unsigned Right Shift Operator *******");

                                value = 12;

                                test = value >>> 2;

                                System.out.println("value : " + value);

                                System.out.println("test : " + test);

                                value = -12;

                                test = value >>> 2;

                                System.out.println("value : " + value);

                                System.out.println("test : " + test);

                }

}

public class ConvertDecimalToBinary {

                /** *www.instanceofjava.com */

                public static void main(String[] args) {

                               

                                System.out.println("\nBinary representation of 1: ");

                                System.out.println(Integer.toBinaryString(1));

                               

                                System.out.println("\nBinary representation of 2: ");

                                System.out.println(Integer.toBinaryString(2));

                               

                                System.out.println("\nBinary representation of 3: ");

                                System.out.println(Integer.toBinaryString(3));

                               

                               

                                System.out.println("\nBinary representation of 4: ");

                                System.out.println(Integer.toBinaryString(4));

                               

                                System.out.println("Binary representation of 10: ");

                                System.out.println(Integer.toBinaryString(10));

                               

                                System.out.println("\nBinary representation of 12: ");

                                System.out.println(Integer.toBinaryString(12));

                               

                                System.out.println("\nBinary representation of 120: ");

                                System.out.println(Integer.toBinaryString(120));

                               

                                System.out.println("\nBinary representation of 500: ");

                                System.out.println(Integer.toBinaryString(500));

                }

}

 

 

import java.security.Timestamp;

import java.sql.Time;

 

public class HexaDecimal {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int number = 8726;

 

                                // decimal to hexadecimal

                               

                                String hexString = Integer.toHexString(number);

                                System.out.println("decimal to hexadecimal: " + hexString);

 

                }

 

}

 

 

public class HexString {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                               

                                int number = 32 ;

                               

                                String hexString = Integer.toString(number, 16);

                                System.out.println("decimal to hexadecimal "

                                                                + "using Integer.toString: " + hexString);

 

 

                }

 

}

 

 

public class LeftShift {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                               

                                int a = 60; /* 60 = 0011 1100 */

                               

                                // 1 1 1 1 0 0 0 0

                                System.out.println(Integer.toBinaryString(a));

                                int b = 13; /* 13 = 0000 1101 */

                                System.out.println(Integer.toBinaryString(b));

                                int c = 0;

                                int d =0;

 

                               

                                c = a << 2; /* 240 = 1111 0000 */

                                d = b << 2 ;   // 00110100

                                System.out.println("a << 2 = " + c);

                                System.out.println(Integer.toBinaryString(c));

                                System.out.println("b << 2 = " + d);

                                System.out.println(Integer.toBinaryString(d));

 

 

                }

 

}

 

 

public class OctalBitWise {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                               

                                int number = 16 ;

                               

                                String octalString = Integer.toString(number, 8);

                                System.out.println("decimal to octal using Integer.toString:"

                                                                + " " + octalString);

                               

                }

 

}

 

 

public class OctalString {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int number = 16;

 

                                // decimal to octal

                                String octalString = Integer.toOctalString(number);

                                System.out.println("decimal to octal: " + octalString);

 

                }

 

}

 

 

public class RightShift {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                               

 

                                int a = 60; /* 60 = 0011 1100 */

                               

                                // 0 0 0 0 1 1 1 1

                                System.out.println(Integer.toBinaryString(a));

                                int b = 13; // 1101     

                                System.out.println(Integer.toBinaryString(b));

                                int c = 0;

                                int d =0;

                               

                                c = a >> 2; /* 15 = 1111 */

                                d = b >> 2; // 3

                                System.out.println("a >> 2  = " + c);

                                System.out.println(Integer.toBinaryString(c));

                                System.out.println("b >> 2  = " + d);

                                System.out.println(Integer.toBinaryString(d));

 

 

                }

 

}

 

 

public class RightShiftZero {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

 

                                int a = 60; /* 60 = 0011 1100 */

 

                                // 0 0 0 0 1 1 1 1

                                System.out.println(Integer.toBinaryString(a));

                                int b = 13; // 1101

                                System.out.println(Integer.toBinaryString(b));

                                int c = 0;

                                int d = 0;

 

                                c = a >>> 2; /* 15 = 0000 1111 */

                                System.out.println("a >>> 2 = " + c);

 

                }

 

}

 

 

// Bitwise And operators

public class SamapleTest {

 

                public static void main(String[] args) {

                                int a = 60; /* 60 = 0011 1100 */

                                int b = 13; /* 13 = 0000 1101 */

                                int c = 0;

 

                                c = a & b; /* 12 = 0000 1100 */

                                System.out.println("a & b = " + c);

                                System.out.println(Integer.toBinaryString(c));

 

                }

 

}

 

 

public class SampleBitwiseXOR {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                               

                                int a = 60; /* 60 = 0011 1100 */

                                int b = 13; /* 13 = 0000 1101 */

                                int c = 0;

                               

                                c = a ^ b; /* 49 = 0011 0001 */

                                System.out.println("a ^ b = " + c);

                                System.out.println(Integer.toBinaryString(c));

 

                }

 

}

 

 

//BitwiseOR operators

public class SampleTestOR {

 

                public static void main(String[] args) {

                                int a = 60; /* 60 = 0011 1100 */

                                int b = 13; /* 13 = 0000 1101 */

                                int c = 0;

                               

                               

                                c = a | b; /* 61 = 0011 1101 */

                                System.out.println("a | b = " + c);

                                System.out.println(Integer.toBinaryString(c));

 

 

                }

 

}

 

 

public class Test {

 

                public static void main(String args[]) {

                                int a, b;

                                a = 10;

                               

                                b = (a == 1) ? 20 : 30;

                                System.out.println("Value of b is : " + b);

                                // 30

 

                                b = (a == 10) ? 20 : 30;

                                //20

                                System.out.println("Value of b is : " + b);

                }

}

 

public class Test1 {

 

                   public static void main(String args[]) {

                                  

                                   Test1 t1 = new Test1();

 

                      String name = "JamesBond";

 

                      // following will return true since name is type of String

                      boolean result = name instanceof String;

                      System.out.println( result );

                     

                      boolean result1 = t1 instanceof Test1;

                      System.out.println( result1 );

                   }

                }

 

public class Test2 {

 

                public static void main(String args[]) {

                                int a = 60; /* 60 = 0011 1100 */

                                int b = 13; /* 13 = 0000 1101 */

                                int c = 0;

 

                                c = a & b; /* 12 = 0000 1100 */

                                System.out.println("a & b = " + c);

 

                                c = a | b; /* 61 = 0011 1101 */

                                System.out.println("a | b = " + c);

 

                                c = a ^ b; /* 49 = 0011 0001 */

                                System.out.println("a ^ b = " + c);

 

                                c = ~a; /*-61 = 1100 0011 */

                                System.out.println("~a = " + c);

 

                                c = a << 2; /* 240 = 1111 0000 */

                                System.out.println("a << 2 = " + c);

 

                                c = a >> 2; /* 15 = 1111 */

                                System.out.println("a >> 2  = " + c);

 

                                c = a >>> 2; /* 15 = 0000 1111 */

                                System.out.println("a >>> 2 = " + c);

                }

}

 

 

public class Test3 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int a = 4; //0100

                                int b = 8; //1000

                                //example of bitwise AND operator &

                                System.out.println("Result of a&b is " + (a&b)); //should be zero

 

 

                }

 

}

 

 

public class Test4 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int a = 4; //0100

                                int b = 4; //0100

                                // 0100

                                System.out.println(" value of A bitwise OR B in Java : " + (a|b) );

 

 

                }

 

}

 

 

public class Test5 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int a = 5; //0101

                                int b = 6; //0110

                               

                                        //  0011

 

                                System.out.println(" value of a XOR B in Java : " + (a^b) );

                                System.out.println(Integer.toBinaryString(a^b));

 

 

                }

 

}

 

 

public class Test6 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                               

                                byte x = 50;  // 50 bytes ->

                                byte y = 51; // 51 bytes ->

                                byte result = (byte) (x&y);

                                System.out.println("Result of x&y= : " + result );

 

 

                }

 

}

 

 

public class Test7 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                               

                                int xInt = 30;

                                int yInt = 31;

                                int result = (xInt&yInt);

                                System.out.println("Result of x&y (Integers)= : " + result );

 

                }

 

}

 

 

public class Test8 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                byte x = 50;

                                byte y = 51;

                                byte result = (byte) (x|y);

                                System.out.println("Result of x|y= : " + result );

 

                }

 

}

 

 

public class Test9 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int xInt = 30;

                                int zInt=33;

                                int result = (xInt|zInt);

                                System.out.println("Result of x|y (Integers)= : " + result );

 

                }

 

}

 

 

public class Test10 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                byte x = 50;

                                byte y = 51;

                                byte result = (byte) (x^y);

                                System.out.println("Result of x^y= : " + result );

 

                }

 

}

 

 

public class Test12 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int xInt = 30;

                                int result = ~xInt;

                                System.out.println("Result of ~xInt(Integers)= : " + result );

 

                }

 

}

 

 

public class Test13 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int number = 2;

 

                                // decimal to binary

                                String binaryString = Integer.toBinaryString(number);

                                System.out.println("decimal to binary: " + binaryString);

                               

                               

                                // decimal to octal

                                String octalString = Integer.toOctalString(number);

                                System.out.println("decimal to octal: " + octalString);

                               

                                // decimal to hexadecimal

                                String hexString = Integer.toHexString(number);

                                System.out.println("decimal to hexadecimal: " + hexString);

                               

                                // second way

                                binaryString = Integer.toString(number, 2);

                                System.out.println("decimal to binary using Integer.toString: " + binaryString);

                                // decimal to octal

                               

                                octalString = Integer.toString(number, 8);

                                System.out.println("decimal to octal using Integer.toString: " + octalString);

                                // decimal to hexadecimal

                               

                                hexString = Integer.toString(number, 16);

                                System.out.println("decimal to hexadecimal using Integer.toString: " + hexString);

 

                }

 

}

 

 

import java.util.Scanner;

import java.util.Stack;

 

public class Test14 {

               

                public static void main(String[] args) {

                                Scanner in = new Scanner(System.in);

                                // Create Stack object

                                Stack<Integer> stack = new Stack<Integer>();

                               

                                // Take User input from keyboard

                               

                                System.out.println("Enter decimal number: ");

                               

                                int num = in.nextInt();

                               

                                while (num != 0) {

                                                int d = num % 2;

                                                stack.push(d);

                                                num /= 2;

                                               

                                }

                                System.out.print("\nBinary representation is:");

                                while (!(stack.isEmpty())) {

                                                System.out.print(stack.pop());

                                }

                                System.out.println();

                }

}

 

 

public class Tset11 {

 

                public static void main(String[] args) {

                                // TODO Auto-generated method stub

                                int xInt = 30;

                                int zInt=31;

                                int result = (xInt^zInt);

                                System.out.println("Result of x^y (Integers)= : " + result );

 

                }

 

}

public class Tset15 {

               

                public static void convertDeciamlToBinary(int num) {

                               

                                int binary[] = new int[40];

                                int index = 0;

                               

                                while (num > 0) {

                                                binary[index++] = num % 2;

                                                num = num / 2;

                                }

                                for (int i = index - 1; i >= 0; i--) {

                                                System.out.print(binary[i]);

                                }

                }

 

                public static void main(String[] args) {

                                System.out.println("Binary representation of 2: ");

                               

                                convertDeciamlToBinary(2);

                               

                                System.out.println("\nBinary representation of 4: ");

                                convertDeciamlToBinary(4);

                                System.out.println("\nBinary representation of 10: ");

                                convertDeciamlToBinary(10);

                                System.out.println("\nBinary representation of 12: ");

                                convertDeciamlToBinary(12);

                }

}