- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Shift Operator Examples
The following program is a simple example that demonstrates the bitwise operators. Copy and paste the following Java program into Test.java file and compile and run this program −
Example
public class Test { 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 ); } }
Output
This will produce the following result −
a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 2 = 15 a >>> 2 = 15
- Related Articles
- Bitwise right shift operator in Java\n
- Java Unary Operator Examples
- Java Arithmetic Operator Examples
- Java AND Operator Examples
- Java OR Operator Examples
- Java Ternary Operator Examples
- Java Assignment Operator Examples
- What does the bitwise left shift operator do in Java?
- What does the bitwise right shift operator do in Java?
- What is Bitwise Left Shift Operator (
- What is right shift (>>) operator in Python?
- What is JavaScript Bitwise Right Shift(>>) Operator?\n
- What is unsigned Right Shift Operator (>>>) in JavaScript?
- What is Bitwise Right Shift Operator (>>) in JavaScript?
- Java Shift operators

Advertisements