Bitwise Operators in Java(Part1)

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 13:- Bitwise Operators in Java(Part1)

In Java, bitwise operators perform operations on individual bits of integral data types (byte, short, int, long, and char). These operators are useful for low-level manipulation of data, dealing with flags, and optimizing certain algorithms. There are six bitwise operators in Java:

  1. Bitwise AND (&):
    • The AND operator compares the corresponding bits of two operands and produces a result with 1s only in the positions where both operands have 1s. Otherwise, the result will have 0s.

Example:

javaCopy code
int a = 10; // Binary: 1010 int b = 6; // Binary: 0110 int result = a & b; // 1010 & 0110 = 0010 (Decimal 2)
  1. Bitwise OR (|):
    • The OR operator compares the corresponding bits of two operands and produces a result with 1s in the positions where at least one of the operands has 1s.

Example:

javaCopy code
int a = 10; // Binary: 1010 int b = 6; // Binary: 0110 int result = a | b; // 1010 | 0110 = 1110 (Decimal 14)
  1. Bitwise XOR (^):
    • The XOR (exclusive OR) operator compares the corresponding bits of two operands and produces a result with 1s only in the positions where one of the operands has 1s, but not both.

Example:

javaCopy code
int a = 10; // Binary: 1010 int b = 6; // Binary: 0110 int result = a ^ b; // 1010 ^ 0110 = 1100 (Decimal 12)
  1. Bitwise NOT (~):
    • The NOT operator (also called bitwise complement) flips each bit of the operand, turning 0s into 1s and 1s into 0s. It operates on a single operand.

Example:

javaCopy code
int a = 10; // Binary: 1010 int result = ~a; // ~1010 = 0101 (Decimal 5) - The sign bit is also flipped
  1. Left Shift (<<):
    • The left shift operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. It fills the empty positions with 0s.

Example:

javaCopy code
int a = 5; // Binary: 0101 int result = a << 2; // 0101 << 2 = 010100 (Decimal 20)
  1. Right Shift (>>):
    • The right shift operator shifts the bits of the left operand to the right by the number of positions specified by the right operand. For positive numbers, it fills the empty positions with 0s; for negative numbers, it fills with 1s.

Example:

javaCopy code
int a = -10; // Binary: 11111111111111111111111111110110 (32-bit representation of -10) int result = a >> 2; // 11111111111111111111111111110110 >> 2 = 11111111111111111111111111111101 (Decimal -3)

These bitwise operators can be handy in various scenarios, such as setting and clearing specific bits, working with flags in binary representation, or optimizing certain computations in specific algorithms. They provide a way to manipulate data at the bit level, which can be particularly useful in certain programming applications.

 

Disclaimer:-

Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.

 

4. Operators

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?