Integer.divideUnsigned() method in Java

The divideUnsigned() is a static method of the Integer class that divides two integers and returns its quotient. This post will look at the divideUnsigned() method in detail.

  • Method declaration – public static int divideUnsigned(int dividend, int divisor)
  • What does it do? This function was first introduced in Java 1.8, and it will divide the first argument (dividend) with the second argument(divisor) by treating the arguments and the result as unsigned.
  • What does it return? It will return an int primitive denoting the quotient after dividing the first argument with the second by treating them as unsigned.

Now, what are unsigned and signed integers? Signed integers contain positive and negative numbers, ranging from [-2147483648 to 2147483647], whereas unsigned integers contain only non-negative numbers ranging from [0 to 4294967295].

But Java doesn’t have any unsigned integers. So, we have to convert the negative numbers to unsigned explicitly.

Internal Implementation of the divideUnsigned() function
    public static int divideUnsigned(int dividend, int divisor) {
        return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));
    }

Code Examples

Dividing two positive numbers using divideUnsigned
public class Codekru {

	public static void main(String[] args) {

		int a = 123;
		int b = 12;

		System.out.println("a/b quotient = " + Integer.divideUnsigned(a, b));

	}
}

Output –

a/b quotient = 10
Dividing one positive and one negative number using divideUnsigned

In this case, the negative number would be converted to a non-negative number, which will be used further.

public class Codekru {

	public static void main(String[] args) {

		int a = -12;
		int b = 12345;

		System.out.println("a/b quotient = " + Integer.divideUnsigned(a, b));

	}
}

Output –

a/b quotient =347911

Now, how did this output come?

  • divideUnsigned uses the toUnsignedLong() method to convert its arguments into their corresponding unsigned values.
  • When we used -12 as a dividend, it was converted to 4294967284 after using the toUnsignedLong(-12) method.
  • The resulting number was then used in the division operation.
Dividing two negative numbers using divideUnsigned
public class Codekru {

	public static void main(String[] args) {

		int a = -6;
		int b = -12343;

		System.out.println("Unsigned long for a = " + Integer.toUnsignedLong(a));
		System.out.println("Unsigned long for b = " + Integer.toUnsignedLong(b));

		System.out.println("a/b quotient = " + Integer.divideUnsigned(a, b));

	}
}

Output –

Unsigned long for a = 4294967290
Unsigned long for b = 4294954953
a/b quotient = 1
Time Complexity of the Integer divideUnsigned() function

The time complexity of the Integer divideUnsigned() function is O(1) as it performs the operations in a constant time.

The What If scenarios

Q- What if divisor = 0 ?

Here, we will get the ArithmeticException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {

		int a = 6;
		int b = 0;

		System.out.println("a/b quotient = " + Integer.divideUnsigned(a, b));

	}
}

Output –

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at java.base/java.lang.Integer.divideUnsigned(Integer.java:1539)
Q- What if dividend = Integer.MAX_VALUE and divisor = Integer.MIN_VALUE ?
public class Codekru {

	public static void main(String[] args) {

		int a = Integer.MAX_VALUE;
		int b = Integer.MIN_VALUE;

		System.out.println("a/b quotient = " + Integer.divideUnsigned(a, b));

	}
}

Output –

a/b quotient = 0

Please visit this link to learn more about the Integer wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected].

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *