Function called with two variables as parameters without commas, is this valid ??

Dec 19, 2014 at 1:34pm
Hi All,

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;
static int 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;
}
}
Dec 19, 2014 at 1:42pm
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.
Dec 19, 2014 at 1:44pm
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.
Last edited on Dec 19, 2014 at 1:47pm
Dec 19, 2014 at 1:53pm
@dhayden Yes, I was thinking of it as pass by reference (& operator). And, got confused with it. Thanks for the help

@Mutexe Thanks for answering the query, I got the logic. Was confused with the & operator until I realized that it is AND operator
Dec 19, 2014 at 2:26pm
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 &?
Dec 19, 2014 at 2:27pm
Also, how did you know it was different from the reference &

Due to the context it's being used in i.e. there are 2 integers, one either side of it.

Take a look here:
http://en.wikipedia.org/wiki/Bitwise_operations_in_C
and
http://www.thegeekstuff.com/2012/10/bitwise-operators/
Dec 19, 2014 at 2:59pm
whoa! Thanks, but working with bits is either brilliant or madness. I can't decide...
Dec 20, 2014 at 4:36am
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.
Topic archived. No new replies allowed.