Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Bitwise Operators in Java

by suresh

The Bitwise operators in Java programming are used to perform bit operations. In Java bitwise, all the decimal values will convert into binary values (sequence of bits i.e., 0100, 1100, 1000, 1001, etc.).

The Java Bitwise Operators will work on these bits such as shifting them left to right or converting bit value from 0 to 1 etc.

The table below shows the different Java Bitwise operators and their meaning. For example, Consider x = 6 and y = 8 and their values in binary form are

x = 0110

y = 1000

Bitwise Operators in JavaMeaning of operatorsExamples
&Bitwise ANDX & Y = 0000
|Bitwise ORX | Y = 1110
^Bitwise exclusive ORX ^ Y = 1110
~Bitwise complement~X = 00001001 (Bitwise Not operator will convert all 0 into 1). Remember, the Bitwise Compliment of N will be -(N + 1)
<<Shift leftX << 1 = 00001100 (Bits will move 1 step left. If we use 2 or 3, then they shift accordingly)
>>Shift rightY >> 1 = 00000100

Let us see the Truth Table behind Bitwise Operators in Java programming Language

xyx & y X | yx ^ y
00000
01011
10011
11110

Bitwise operators in Java example

This example helps to understand the Bitwise Operators practically.

This program allows the user to enter two integer variables a and b, and we are going to use these two variables to show various Bitwise operators.

// Bitwise operators in Java Example

package JavaOperators;

import java.util.Scanner;

public class BitwiseOperators {
	private static Scanner sc;
	public static void main(String[] args) {
		int a, b;
		sc = new Scanner(System.in);
		System.out.println(" Please Enter two integer Value: ");
		a = sc.nextInt();
		b = sc.nextInt();
		
		System.out.format(" Bitwise AND Operator: %d & %d = %d \n", a, b, a & b);
		System.out.format(" Bitwise OR Operator: %d | %d = %d \n", a, b, a | b);
		System.out.format(" Bitwise EXCLUSIVE OR: Operator %d ^ %d = %d \n", a, b, a ^ b);
		System.out.format(" Bitwise NOT Operator: ~%d = %d \n", a, ~a);
		
		System.out.format(" LEFT SHIFT Operator: %d << 1 = %d \n", a, a << 1);
		System.out.format(" RIGHT SHIFT Operator: %d >> 1 = %d \n", b, b >> 1);
	}
}

OUTPUT

Bitwise Operators in Java 1

ANALYSIS

In this Java bitwise operator example, the following statement will ask the user to enter integer values a, b. Next, we are going to assign the user input values to the variables.

System.out.println(" Please Enter two integer Value: ");
a = sc.nextInt();
b = sc.nextInt();

The following System.out.format statements will perform the Java bitwise operations on a and b, and then they will display the output

System.out.format(" Bitwise AND Operator: %d & %d = %d \n", a, b, a & b);
System.out.format(" Bitwise OR Operator: %d | %d = %d \n", a, b, a | b);
System.out.format(" Bitwise EXCLUSIVE OR: Operator %d ^ %d = %d \n", a, b, a ^ b);
System.out.format(" Bitwise NOT Operator: ~%d = %d \n", a, ~a);

System.out.format(" LEFT SHIFT Operator: %d << 1 = %d \n", a, a << 1);
System.out.format(" RIGHT SHIFT Operator: %d >> 1 = %d \n", b, b >> 1);

In this Java bitwise operator example, we assigned the values as a = 12 and b = 25. The binary form of 12 = 00001100 and 25 = 00011001.

Let us see the calculations.

Java Bitwise AND Operation = a & b
00001100 & 00011001 ==> 00001000 = 8

Java Bitwise OR Operation = a || b
00001100 || 00011001 ==> 00011101 = 29

Next, Bitwise Exclusive OR Operation = a ^ b
00001100 || 00011001 ==> 00010101 = 21

Bitwise Complement Operation = ~a
As we said before, Bitwise Compliment of N will be -(N + 1). It means – (12 + 1) = -13

Left Shift of Java Operator = a << 1
00001100 << 1 = 00011000 = 24

Java Right Shift = b >> 1
00011001 >> 1 = 00001100 = 12

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy