# Thread: Function purpose

1. ## Function purpose

Hi ,
Could you please help me understand what the "test" function do ?
test(15,8) >> gives 16
test(7,15) >> gives 17

thanks

Code:
```#include <stdio.h>

int test(unsigned int n ,unsigned int m);

int main()
{

unsigned int k;

k=test(15,8);
printf("k= %d\n",k);  // k=16

return 0;
}

int test(unsigned int n ,unsigned int m)
{
return (n+m -1)& ~ (m-1);
}```

2. Originally Posted by ilans11il
Hi ,
Could you please help me understand what the "test" function do ?
test(15,8) >> gives 16
test(7,15) >> gives 17
It's doing logical and arithmetical operations on the two numbers. I can't see a purpose, but if you plotted a 2 dimensional table in x, y you might see an interesting pattern.

3. If you want to understand bit operations, write down the binary representation of the values and do the operations on paper.

Bye, Andreas

4. Looks like m should be a power of 2. What the function does is round a number up and then mask off the lower bits. So if the call is test(x,8), then you get multples of 8: 0 == x returns 0, 0 < x <= 8 returns 8, 8 < x <= 16 returns 16. If the call is test(y,16), then 0 == y returns 0, 0 < y <= 16 returns 16, 16 < y <= 32 returns 32, ...

Popular pages Recent additions