India's Largest Free Online Education Platform

Bitwise operator in java

Bitwise operator in java

Program:01

public class Bitwise_Operator
{
    public static void main(String agrs[])
    {
        int a = 6,b=9; // a= 0110, b= 1001
        System.out.println("'Bitwise &' performed at a= "+ a+ " and b= " +b+ " is: " + (a&b)); // a&b = 0000 .ie 0
        System.out.println("'Bitwise |' performed at a= "+ a+ " and b= " +b+ " is: " + (a|b)); // a|b = 1111 .ie 15
        System.out.println("'Bitwise ^' performed at a= "+ a+ " and b= " +b+ " is: " + (a^b)); // a^b =1111  .ie 15
    }
}

Output:

Program: 02

public class Bitwise_Operator
{
    public static void main(String agrs[])
    {
        int a = 3,b=5; // a= 0011, b= 0101
        System.out.println("'Bitwise &' performed at a= "+ a+ " and b= " +b+ " is: " + (a&b)); // a&b = 0001 .ie 1
        System.out.println("'Bitwise |' performed at a= "+ a+ " and b= " +b+ " is: " + (a|b)); // a|b = 0111 .ie 7
        System.out.println("'Bitwise ^' performed at a= "+ a+ " and b= " +b+ " is: " + (a^b)); // a^b =0110  .ie 6
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!