Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Java Logical Bitwise Operators and Priority Tutorial

bitwise operator in java

Study and learn Java MCQ Questions and Answers on Bitwise Operators and their priorities. Attend job interviews easily with these Multiple Choice Questions

Java language allows developers to work with individual bits of a number using Logical Bitwise Operators. Bitwise logical operators and Logical Bitwise Operators are one and the same. Simply use the term “Bitwise Operators” to refer to bitwise operators. Do not use the word Logical.

Bitwise Operators vs Logical Operators in Java

Bitwise OperatorsLogical Operators
Bitwise operators work with integer type dataLogical operators work with boolean type data.So use LOGICAL to refer to boolean logical operators.
If boolean operands are present, use Bitwise Operators.If Relational operators are present, use Logical Operators.
Bitwise operations are used less in general.Programmers use logical operators very often.
Bitwise operators are ~, &, |, ^, <<, >>, >>> and op=.Logical operators are !, &, &&, |, ||, ^ and op=.

Bitwise Operators

The common operators used in both Bitwise Operation and Logical Operation are as follows.

  • AND – &
  • OR – |
  • Exclusive OR or XOR – ^
  • Compound AND – &=
  • Compound OR – |=
  • Compound XOR ^=

Bitwise Operators and Priority

Each integer type number contains so many bits of information. The purpose of bitwise operators is to manipulate individual bits of an integer type number.

Supported Five Integer Types for Bitwise Operation are as follows.

  • byte (8 bits)
  • char (16 bits) – unsigned
  • short (16 bits)
  • int (32 bits)
  • long (64 bits)

We have shown 6 different groups of bitwise operators with priority. This priority or precedence is used to resolve a deadlock in solving equal priority operations in an arithmetic expression.

PriorityOperatorSimple Name
1TILDE ~Bitwise Unary NOT
2>>Shift Right
2<<Shift Left
2>>>Shift Right Zero Fill
3& AmpersandBitwise AND
4^ CARATBitwise Exclusive OR
5|  PIPEBitwise OR
6op=Assignment Operatora op= ba = a op b
6&=Bitwise AND Assignmenta &=ba= a&b
6|=Bitwise OR Assignmenta |=ba= a|b
6^=Bitwise Exclusive OR Assignmenta ^=ba= a^b
6>>=Bitwise Shift Right Assignmenta >>=ba= a>>b
6>>>=Bitwise Shift Right Fill Zero Assignmenta >>>=ba= a>>>b
6<<=Bitwise Shift Left Assignmenta <<=ba= a<<b

Bitwise operators have less priority than arithmetic and relational operators. Only Bitwise Unary NOT or Bitwise Complementary Operator (~) has the highest priority than arithmetic and relational operators.

Example: Bitwise Operator Priority

class BitwisePriority
{
  public static void main(String args[])
  {
    int a=5; 0101
    int b=6; 0110
    //12     1100 -
    // 5     0101 -
    int c = a&b+6;
    System.out.println(c);
  }
}
//OUTPUT
//a&(b+6)
//a&12
//5&12
//4

Note: We have used Binary Literals in this example. A binary literal starts with 0b. You can specify a number in binary format using this notation. Also, “op” refers to “Operand” in this tutorial.  

1. Bitwise Unary NOT Operator (~ TILDE)

Bitwise Unary NOT operator (~) is also called Bitwise Complement Operator. It simply turns bit 0 to bit 1 and bit 1 to bit 0. We have used Type Casting to byte data type in most of the examples here.

Usage example:

class UnaryNOT
{
  public static void main(String args[])
  {
    byte a = 0b00001010; //a=10
    byte b = (byte)~a;
    System.out.println(a + ", " + b);
  }
}
//OUTPUT: 10, -11
OperandResult
01
10

2. Bitwise AND Operator (&)

Bitwise AND operator gives an output of 1 only if both the input bits are 1s. Even if one input bit is 0, the output is 0. Here operand is synonymous to input bit.

Usage example:

class BitwiseAND
{
  public static void main(String args[])
  {
    byte a = 0b00001010; //a=10
    byte b = 0b00111001; //b=57
             //00001000 = 8
    byte c = a & b;
    System.out.println(a + ", " + b + "= " + c);
  }
}
//OUTPUT: 10, 57 = 8
Op1Op2Result
000
010
100
111

3. Bitwise OR Operator (|)

Bitwise OR Operator (|) gives an output of 1 at least if one input bit is 1. Only if both the input bits are 0s, the output is 0.

Usage example:

class BitwiseOR
{
  public static void main(String args[])
  {
    byte a = 0b00001010; //a=10
    byte b = 0b00111001; //b=57
             //00111011 = 59
    byte c = a | b;
    System.out.println(a + ", " + b + "= " + c);
  }
}
//OUTPUT: 10, 57 = 59
Op1Op2Result
000
011
101
111

4. Bitwise Exclusive OR Operator (^)

Bitwise XOR Operator (|) or Bitwise Exclusive OR operator gives an output of 1 if both the input bits are different i.e 1,0 or 0, 1. If both the input bits are same i.e 0,0 or 1,1, the output is 0.

Usage example:

class BitwiseXOR
{
  public static void main(String args[])
  {
    byte a = 0b00001010; //a=10
    byte b = 0b00111001; //b=57
             //00110011 = 51
    byte c = a ^ b;
    System.out.println(a + ", " + b + "= " + c);
  }
}
//OUTPUT: 10, 57 = 51
Op1Op2Result
000
011
101
110

5. Bitwise Shift Left or Left Shift Operator Operator (<<)

Bitwise Shift Left or Right Shift operator shifts the individual bits from Right to Left. So gaps are formed on the right side. These gaps are filled with 0s. Left most bits are discarded.

Note: If discarded bits on the left side are zeroes, Left Shift operation is equivalent to multiplying the number by 2.

Usage example:

class BitwiseLeftShift
{
  public static void main(String args[])
  {
    byte a = 0b00001010; //a=10
         //00010100  //20
    byte b = (byte)(a << 1);
    System.out.println(a + ", " + b);

    byte c = 0b01010111; //87
        //0101-01110000
    //Left 4 bits discarded
    byte d = (byte)(c << 4);
    System.out.println(c + ", " + d);
  }
}
//OUTPUT:
//10, 20
//87, 112

6. Bitwise Shift Right or Right Shift Operator Operator (>>)

Right Shift Operator shifts the individual bits of a number towards Right Side from the Left Side. Usually Left Most bits are filled with Zeroes in the process of filling gaps created because shifting bits to the right side. Shift Right operator does Sign Extension by putting the Left Most Bits to 1s if they are already 1s. So, negative numbers are still negative numbers after the Right Shift operation.

Note: Right Shift operation is equivalent to dividing the number by 2.

Usage example:

class BitwiseRightShift
{
  public static void main(String args[])
  {
    byte a = 0b00010100; //a=20
             //00001010  //10
    byte b = (byte)(a >> 1); //=20/2
    System.out.println(a + ", " + b);

    byte c = 0b01010111; //87
             //00000101-0111 //5
    //Right 4 bits discarded
    byte d = (byte)(c >> 4); //=87/(2*2*2*2)
    System.out.println(c + ", " + d);

    byte e = (byte)0b11111000; //-8
                   //11111100 //-4
    //Right 1 bit discarded
    byte f = (byte)(e >> 1);
    System.out.println(e + ", " + f);
  }
}
//OUTPUT:
//20, 10
//87, 5
//-8, -4

7. Bitwise Shift Right Zero Fill Operator (or) Unsigned Right Shift Operator (>>>)

Unsigned Shift Right or Shift Right Fill Zero operator shifts the individual bits of a number from Left to Right. It fills Zeroes on the left side in the gaps created by shifting the digits to the right. This operator does not maintain Sign Extension. So, a negative number may become a positive number after the Right Shift Fill Zero operation.

Note: Right Shift Fill Zero operation is equivalent to dividing the number by 2.

Usage example:

class BitwiseRightShiftUnsigned
{
  public static void main(String args[])
  {
    byte a = 0b00010100; //a=20
             //00001010  //10
    byte b = (byte)(a >>> 1); //=20/2
    System.out.println(a + ", " + b);

    byte c = 0b01010111; //87
             //00000101-0111 //5
    //Right 4 bits discarded
    byte d = (byte)(c >>> 4); //=87/(2*2*2*2)
    System.out.println(c + ", " + d);

    int e = -1;//(byte)0b11111111;
      //11111111 11111111 11111111 11111111 //-1
      //00000000 00000000 00000000 00001111 //15
    int f = (int)(e >>> 28);
    System.out.println(e + ", " + f);
  }
}
//OUTPUT:
//20, 10
//87, 5
//-1, -15

This is all about Bitwise Operators in Java. You should practice these examples on your PC for better understanding.

Bitwise Operators online test

[WpProQuiz 70]

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming Tests
  • C programming Tests
  • C++ programming Tests
  • Aptitude Tests

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2022
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2022
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.