Reputation: 251
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.
Upvotes: 12
Views: 22669
Reputation: 311
In order to check if a random division results in an integer or fraction you need the following:
int n = 9;
int p = 3;
if (n % p == 0) {
//the division results in an integer.
}
else
{
//the division results in a fraction.
}
You can do this as an alternative:
if (n / p == Math.ceil((double) n / (double) p)) {
//the division results in an integer.
}
else
{
//the division results in a fraction.
}
One needs Math.ceil() and not round or floor, because a integer division is almost equal to a floor and fraction will be rounded down and appear as 'integer division'.
Upvotes: 1
Reputation: 141877
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
Upvotes: 5
Reputation: 5710
If you need the nearest multiple of 16, then you have two cases for dealing with odd multiples of 8. 1. 8 becomes 16, 24 becomes 32 2. 8 becomes 0, 24 becomes 16
For the first:
int j = ((i+8)/16)*16;
In the second case:
int j = ((i+7)/16)*16;
If you want always want to round DOWN (i.e. 17 becomes 16, and 15 becomes 0):
int j = (i/16)*16;
If you wanted to always round UP (not what your example says), you would have done this instead:
int j = ((i+15)/16)*16;
Upvotes: 0
Reputation: 13520
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.
Upvotes: 1
Reputation: 11986
There are a number of striking issues with your original code:
%
) 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.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.>>
) by 4 bits to do that instead.Upvotes: 4
Reputation: 16158
(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
}
Upvotes: 5
Reputation: 118734
Use the mod operator. Mod gives you the remainder of a division operation.
public boolean isEvenlyDivisable(int a, int b) {
return a % b == 0;
}
Upvotes: 18