Signup/Sign In

Java Long divideUnsigned() Method

Java divideUnsigned() method belongs to the Long class. This method is used to return the quotient (unsigned) obtained by dividing the first argument with the second argument. The result i.e the quotient is always taken as an unsigned value.

Syntax:

public static long divideUnsigned(long dividend, long divisor)

Parameters:

The parameters passed will be long dividend which will be divided and long divisor which will be doing the division process.

Returns:

Returns the unsigned quotient when the first argument(i.e dividend) is divided by the second argument(i.e divisor).

Example 1:

Here, the normal division process takes place as all the long values are signed.

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {  
        long a = 100L;  
        long b = 5L;
        long c = 3L;        
        System.out.println("Quotient of\t" + a+ "/" +b+ "\t is \t" +Long.divideUnsigned(a, b));  
        System.out.println("Quotient of\t" + a+ "/" +c+ "\t is \t" +Long.divideUnsigned(a, c));  
    }   
}  


Quotient of 100/5 is 20
Quotient of 100/3 is 33

Example 2:

Here, long value 100L is divided with numbers from -5 to +5 to clear the concept of the method. The output will be zero for signed integers and respective quotient for the unsigned integers.

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {  
        long a = 100L;  
        try
        {
            for(long i=-5L;i<=5L;i++)
            {
                if(i==0)
                {
                    System.out.println("Division with zero not possible");
                }
                else
                {
                    System.out.println("Quotient of\t" + a+ "/" +i+ "\t is \t" +Long.divideUnsigned(a, i));  
                } 
            }
        }
        catch(Exception e)
        {
            System.out.println("Exception occurred", e);  
        }  
    }        
}  


Quotient of 100/-5 is 0
Quotient of 100/-4 is 0
Quotient of 100/-3 is 0
Quotient of 100/-2 is 0
Quotient of 100/-1 is 0
Division with zero not possible
Quotient of 100/1 is 100
Quotient of 100/2 is 50
Quotient of 100/3 is 33
Quotient of 100/4 is 25
Quotient of 100/5 is 20

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.