Core Java

Java Integer Binary Representation

Java utilizes binary representation to store numbers in memory. Gaining insight into how integers are depicted at the bit level can significantly aid in performing specific operations. Let us delve into understanding Java Integer Bit representation and explore their applications.

1. Understanding Bitwise Operations in Java

Bitwise operations in Java are fundamental techniques used to manipulate individual bits of integer values. These operations work directly at the binary level, enabling efficient handling of data in scenarios like cryptography, device driver programming, and low-level system programming.

1.1 AND (&) Operator

The AND operator performs a bitwise AND operation between corresponding bits of two integer operands. It results in a new value where a bit is set if and only if both corresponding bits in the operands are set. It is represented by the following syntax:

int result = operand1 & operand2;

The AND operator is commonly used for clearing specific bits and masking to extract particular bits of interest.

1.2 OR (|) Operator

The OR operator performs a bitwise OR operation between corresponding bits of two integer operands. It results in a new value where a bit is set if at least one of the corresponding bits in the operands is set. It is represented by the following syntax:

int result = operand1 | operand2;

The OR operator is often used for setting specific bits and combining sets of flags or options.

1.3 XOR (^) Operator

The XOR operator performs a bitwise exclusive OR operation between corresponding bits of two integer operands. It results in a new value where a bit is set if and only if one of the corresponding bits in the operands is set, but not both. It is represented by the following syntax:

int result = operand1 ^ operand2;

The XOR operator is useful for flipping specific bits and detecting changes between two sets of bits.

1.4 Complement (~) Operator

The complement operator performs a bitwise NOT operation on a single integer operand, resulting in the inversion of all its bits. It is represented by the following syntax:

int result = ~operand;

The complement operator is utilized for flipping all bits in an operand.

1.5 Left/Right Shift (<< >>) Operators

The left shift operator shifts all bits in a value to the left by a specified number of positions. The right shift operator shifts all bits in a value to the right by a specified number of positions.

int result = operand << numberOfPositions;

int result = operand >> numberOfPositions;

These operators are employed for multiplying or dividing by powers of 2 and for efficient multiplication or division by 2, 4, 8, etc.

2. Code Example

Let’s consider the below example:

package com.jcg.example;

public class BitwisePermissionsExample {

    public static void main(String[] args) {
        // Initial permissions
        int permissions = 0b0000_0010; // Represents permission to read

        // Set permission to write
        permissions |= 0b0000_0100; // Set write permission

        // Toggle permission to execute
        permissions ^= 0b0000_1000; // Toggle execute permission

        // Clear permission to read
        permissions &= ~0b0000_0010; // Clear read permission

        // Updated permissions
        int updatedPermissions = permissions; // Updated permissions

        // Output
        System.out.println("Updated Permissions: " + Integer.toBinaryString(updatedPermissions));
    }
}

2.1 Code Breakdown

This example demonstrates how bitwise operators are used to manipulate permissions represented as binary values in Java.

  • The BitwisePermissionsExample class is defined.
  • The main method serves as the entry point of the program.
  • An integer variable permissions is declared and initialized with the binary value 0b0000_0010, representing permission to read.
  • The |= operator is used to set the write permission by performing a bitwise OR operation with the binary value 0b0000_0100.
  • The ^= operator is used to toggle the execute permission by performing a bitwise XOR operation with the binary value 0b0000_1000.
  • The &= operator is used to clear the read permission by performing a bitwise AND operation with the complement of the binary value 0b0000_0010.
  • The updated permissions are stored in the updatedPermissions variable.
  • The updated permissions are printed to the console using System.out.println, converting the integer value to a binary string representation.

2.2 Code Output

The output represents the updated permissions after performing the bitwise operations.

Updated Permissions: 110

3. Conclusion

Bitwise operations in Java provide powerful tools for manipulating individual bits within integers. They can be effectively used to manage flags, permissions, and various other bitwise tasks, making them essential for low-level programming and optimizations.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button