I came across this question where the function is called with this kind parameters
snape(num &(mask-1),(count-1))
What type of function call is this? I have never seen this one before.
Can anyone please explain about the validity of this statement and the advantages of using this type of arguments?
Please find the complete code below:
1 2 3 4 5 6 7 8 9 10 11
num=219,count=4;
staticint snape(int num,int count)
{
if(count<=1)return num;
else
{
int mask=1<<(count-1);
int value=snape(num &(mask-1),(count-1))<<1;
return (!!(num&mask))/value;
}
}
The first argument is num & (mask-1). The second arg is count-1
In the first argument, & is the bit-wise AND operator. It sounds like you're thinking it's the address operator.
it's defined to take 2 ints, and when you call it you pass it 2 ints as well? i dont see anything weird.
This is using a bitwise and for the first parameter: num &(mask - 1);
with you current variable values will yield 3 for this term (i think).
it's no different from calling it like this:
int value=snape(1+2+3+1+2+6+6, 5+6+1+2);
i.e. you're still passing in 2 int values at the end of the day, but you're doing some intermediate calculations before the call.
That may be all fine and dandy for you, but could someone explain what the bitwise & means? Also, how did you know it was different from the reference &?
whoa! Thanks, but working with bits is either brilliant or madness. I can't decide...
It's brilliant when you need to and madness when you don't :).
Seriously though, the advantage of working with bits is that they're small, you can work with lots at once (32 or 64 typically) and the code is blindingly fast because the CPU typically supports and, or, not, and xor operations as single instructions. Working with bits is well worth learning for all these reasons.