Test a int if it is dividable by4

  • Thread starter Thread starter Jeroen Ceuppens
  • Start date Start date
J

Jeroen Ceuppens

Hi,

I want to test if a integer is dividable by 4, and if not, make it dividable
by 4

So: 80=ok
79= not good should be 80 after test
78=should be 80
77=should be 80
81=should be 84
........

What is the best way to do this?
I used to program in C and c# math functions are not my knowledge yet

Greetz
 
I see 3 solution

1. myInt % 4 == 0
2. (myInt & 3) == 0
3. really, you should revise your elementary mathematic :)
 
Since I have a pretty good idea why you are asking this, here is the answer
I believe you are looking for:

LONG lStride = (biWidth * lBitCount + 7) / 8;
lStride = (lStride + 3) & ~3;


Or for cases where bitcount is multiple of 8 bits is simply
lStride = (nWidth + 3) & ~3;
 
Jeroen said:
Hi,

I want to test if a integer is dividable by 4, and if not, make it dividable
by 4

So: 80=ok
79= not good should be 80 after test
78=should be 80
77=should be 80
81=should be 84

x = ((x + 3) / 4) * 4

Hilton
 
Jeroen Ceuppens said:
Hi,

I want to test if a integer is dividable by 4, and if not, make it dividable
by 4

So: 80=ok
79= not good should be 80 after test
78=should be 80
77=should be 80
81=should be 84
.......

What is the best way to do this?
I used to program in C and c# math functions are not my knowledge yet

Greetz

This is Java, but you can easily convert it ;o)

public static int divisify(int n)
{
int iRet = n;

if (0 != n)
{
int remainder = n % 4;

if (0 != remainder)
{
iRet += 4 - remainder;
}
}

return iRet;
}

HTH
 
As I also know why you are asking, you may want to take a look at following
sample. It has code for reading and writing bitmaps directly from and to
streams. It uses GAPI so you may not be as interested in the drawing code
but you can extract what you want.

Dancing Zombies: Adding Bitmaps to the Managed Graphics Library
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI2.asp

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
GrOrH said:
This is Java, but you can easily convert it ;o)

public static int divisify(int n)
{
int iRet = n;

if (0 != n)
{
int remainder = n % 4;

if (0 != remainder)
{
iRet += 4 - remainder;
}
}

return iRet;
}

Those "if" statements make things inefficient though. I think the solution
I presented earlier consists or 3 very simple and fast instructions:

x = ((x + 3) / 4) * 4

BTW: The "/4" and "*4" can be written as shifts bye 2.

Hilton
 
The most efficient solution is probably the one suggested by Alex Feinman:

((number + 3) & ~3)

It's lost likely just 2 ARM instructions and no conditional jumps which
would trash pipeline on modern CPUs.

I believe, solution a la Java would be 10-50 times slower, which might
allow you to enjoy a nice cap of Java while your app is running. :)

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Chris Tacke said:
Doh, lack of sleep!

Should be:

if (val % 4) {val += (4 - val % 4); }

Then what's wrong with
val-=val%4;
?
No questions asked, just the mod operator.
(Oh, upon reading the initial (I want 77-79 to become 80), I can see why!)

But I like the & (and) version better. Bitwise operators tends to be faster
than divisions.

/Keld Laursen
 
Hilton's version is probably the best. The -= version get's the floor value
of the range, the version I posted gets the ceiling (which I think is what
the OP was after for aligning bitmap data).

-Chris
 
Alex Feinman said:
Since I have a pretty good idea why you are asking this, here is the answer
I believe you are looking for:

LONG lStride = (biWidth * lBitCount + 7) / 8;
lStride = (lStride + 3) & ~3;


Or for cases where bitcount is multiple of 8 bits is simply
lStride = (nWidth + 3) & ~3;

This actually should be
lStride = (nWidth * (lBitCount >> 3) + 3) & ~3;
 
Back
Top