Java - How to check if a division is an integer or a float?

16,187

Solution 1

Since all ints which are divisible by 16 will have their last 4 bits all set to 0. You can accomplish what you want without a loop or even an if statement:

i &= 0xfffffff0; // Sets i to the greatest multiple of 16 less than i, or 0 for i < 16

For example:

int i = 50;
i &= 0xfffffff0; // i == 48

i = 39;
i &= 0xfffffff0; // i == 32

i = 16;
i &= 0xfffffff0; // i == 16

Solution 2

Use the mod operator. Mod gives you the remainder of a division operation.

public boolean isEvenlyDivisable(int a, int b) {
    return a % b == 0;
}

Solution 3

(i - x)/16 is integer when remainder of (i - x)/16 is 0. Use %(modulus) operator like :

if((i - x)%16 == 0) {
   // (i-x)/16 is integer
}

Solution 4

There are a number of striking issues with your original code:

  1. If you are rounding down to the nearest multiple of 16, it follows that the highest value you possibly may have to substract is 15. Therefore the upper boundary of your loop should be at most 15.
  2. As others have noted you could use the modulo operator (%) to determine the exact value to substract from a given value to round it down to the nearest multiple of 16. This removes the need for a loop entirely.
  3. But since 16 is a power of 2, and since integers are represented as a binary number with 32 digits (i.e. 32bits), you can calculate the value more directly by using a bitmask to zero out any digits smaller than 16 in the number. In Java you can use the binary & operator for that purpose like this: i & 0xfffffff0. This will zero out the last 4 digits (those representing: 8-4-2-1), which effectively rounds your number down to the nearest value divisible by 16.
  4. If you need to perform integer division of number and ignore any remainder you can simply shift (>>) by 4 bits to do that instead.

Solution 5

To find out whether a number evenly divides another, check out the other answers, Modulo (%) is the way to do that.

To do what you want to do above, you don't need a loop:

public int nearestDivider(final int input)
{
    final int multiple = input / 16; // this will divide by 16 and it's integer math, so it loses the decimal
    return multiple * 16;
}

That will return 48 if you give it 50 like your example.

If you really want nearest then you will have to do some floating point division

public int nearestDivider(final int input)
{
    final int multiple = Math.round((float) input / 16);
    return multiple * 16;
}

Now 46 returns 48, 149 returns 144 etc.

Share:
16,187
user1541106
Author by

user1541106

Updated on June 26, 2022

Comments

  • user1541106
    user1541106 almost 2 years

    Couldnt think of a better title. Well the problem is: I have the "int i", it can be any value. my goal is to transform the "int i" to the closest number divisible by 16.

    For example, I got i = 33. Then i will be turned to 32 (16x2).But if I get i = 50, then it will be turned to 48 (16x3).

    I tried many things for example:

    for (int x = i; x < 999; x++){
    if ( (i - x)/16 *is an integer*){
    i = i - x;
    }
    

    But I dont know how to check if its an integer. So maybe my previous code work, but I just need to find a way to check if its an integer or a float. So.. any help is appreciated.

  • Alex
    Alex over 11 years
    Depending on the definition of "closest", you might need to add 16.
  • xbakesx
    xbakesx over 11 years
    True, his examples (and code) seemed to indicate, he wanted closest less than or equal to...
  • xbakesx
    xbakesx over 11 years
    @Alex now you really have nearest : )