Core Java

Arithmetic Ops On Precision Binary Ints in Java

Java provides robust support for handling binary integers, including operations on arbitrary-length binary integers. Let us delve to understand how to perform arithmetic operations on such integers, how to determine the number of bits in binary literals, and the significance of arithmetic ops on precision binary ints in Java.

1. Introduction

In Java, binary integers are a way to represent numbers using the binary numeral system, which consists of only two digits: 0 and 1. This system is fundamental to computing, as it directly corresponds to the on-and-off states of transistors in digital circuits. In Java, Binary literals can be defined using the prefix 0b or 0B, followed by a sequence of binary digits. This allows developers to work with numbers in their binary form, making certain types of bitwise operations and calculations more intuitive. Java also supports operations on binary integers of varying lengths, from fixed-size primitive types like int and long to arbitrary-length integers using the BigInteger class.

2. Number of Bits of Binary Literals

Binary literals in Java are introduced with a prefix 0b or 0B. They represent binary numbers directly within the code. The number of bits used to represent these literals depends on the type of variable to which they are assigned. For instance, an int type has 32 bits, while a long type has 64 bits.

2.1 Code Example and Output

package com.jcg.example;

public class BinaryLiteralBits {
    public static void main(String[] args) {
        int binaryInt = 0b1101;  // 13 in decimal
        long binaryLong = 0b110110110110110110110110110110110110110110L;

        System.out.println("Number of bits in int binaryInt: " + Integer.bitCount(binaryInt));
        System.out.println("Number of bits in long binaryLong: " + Long.bitCount(binaryLong));
    }
}

The above code assigns binary literals to an int and a long variable. The Integer.bitCount() and Long.bitCount() methods return the number of one-bits in the two’s complement binary representation of the specified values.

Number of bits in int binaryInt: 3

Number of bits in long binaryLong: 39

In this example, the binaryInt variable has 3 one-bits and the binaryLong variable has 39 one-bits.

3. Arbitrary-Length Binary Integers

In Java, the BigInteger class in the java.math package allows for the creation and manipulation of arbitrary-length integers. This class supports operations like addition, subtraction, multiplication, and division for numbers of any size, including those defined in binary format.

The BigInteger class does not provide methods for unsigned number arithmetic operations. Instead, all operations behave as if BigIntegers were represented in two’s-complement notation (like Java’s primitive integer types). Furthermore,  BigInteger can represent any arbitrary length integer, including positive, negative, and zero values without overflow or underflow issues.

As stated earlier, the BigInteger class in Java can handle very large numbers without running into problems like overflow (when a number is too big for a typical data type). However, it’s important to understand how it stores and works with these numbers.

Sign and Value:

  • BigInteger stores the number in two parts: the sign (positive, negative, or zero) and the magnitude (the size of the number).
  • The sign is stored separately, so BigInteger knows if the number is positive or negative.

Magnitude: The magnitude is stored as an array of numbers (like a list of digits). This array doesn’t worry about whether the number is positive or negative; it just stores the size of the number.

Working: When you add, subtract, multiply, or divide with BigInteger, it performs these operations as if the operands of each operation were represented in two’s-complement notation (like Java’s primitive integer types). Effectively rendering every operation as an addition.

For unsigned arithmetic on primitive types like int and long, Java provides methods such as Integer.divideUnsigned(), Integer.remainderUnsigned(), etc. However, when working with BigInteger, you directly use the arithmetic methods like add(), subtract(), multiply(), and divide(), which operate as unsigned by default.

3.1 Code Example and Output

package com.jcg.example;

import java.math.BigInteger;

public class ArbitraryLengthBinaryIntegers {
    public static void main(String[] args) {
        BigInteger binaryNum1 = new BigInteger("110110110110110110110110110110110110110110", 2);
        BigInteger binaryNum2 = new BigInteger("101010101010101010101010101010101010101010", 2);

        BigInteger sum = binaryNum1.add(binaryNum2);
        BigInteger difference = binaryNum1.subtract(binaryNum2);
        BigInteger product = binaryNum1.multiply(binaryNum2);
        BigInteger quotient = binaryNum1.divide(binaryNum2);

        System.out.println("BinaryNum1: " + binaryNum1.toString(2));
        System.out.println("BinaryNum2: " + binaryNum2.toString(2));
        System.out.println("Sum: " + sum.toString(2));
        System.out.println("Difference: " + difference.toString(2));
        System.out.println("Product: " + product.toString(2));
        System.out.println("Quotient: " + quotient.toString(2));
    }
}

In this code, two arbitrary-length binary numbers are created using the BigInteger class, which takes a string representation of the binary number and the radix (2 for binary). We perform addition, subtraction, multiplication, and division on these numbers using the respective methods provided by the BigInteger class.

BinaryNum1: 110110110110110110110110110110110110110110

BinaryNum2: 101010101010101010101010101010101010101010

Sum: 1100101011001010101010101010101010101010100

Difference: 11101101101101101101101101101101101101100

Product: 111111100000000000000000000000000000000000000000000000000000000000000

Quotient: 1

The BigInteger class handles binary numbers of any length, making it ideal for working with arbitrary-length binary integers.

4. Conclusion

Handling binary integers in Java is straightforward for both fixed-length and arbitrary-length numbers. While primitive types like int and long have limitations on the number of bits, the BigInteger class offers a powerful way to work with arbitrarily large binary integers. By understanding the number of bits in binary literals and leveraging the capabilities of the BigInteger class, developers can effectively perform arithmetic operations on binary numbers of any size.

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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button