I am having a casting problem with long and unsigned longs getting cast as integer. I have wasted a great deal of time experimenting with the syntax, doing explicit casts, putting things into parentheses… Can someone please tell me the correct syntax to eliminate my casting error?

What I am trying to do is find powers of 2. The built in pow() function uses doubles and I get rounding errors. So I wrote my own iPow() function. But when I try to find 2 to the power of 16 which should be 65536, I get 0 [zero]. Likewise when I try to find 2 to the power of 15 I get -32768 [negative].

Here is my iPow() function as it currently stands. As I said, I have tried all sorts of things with the syntax but can’t seem to find the magic syntax to get the proper results.

```
unsigned long iPow(int power)
{
// Integer version of pow() function
// Used to get powers of 2
// Because the built in pow() function uses doubles and has rounding errors
unsigned long lOut = 1UL;
const unsigned long two = 2UL;
for (int iLoop = 1; iLoop <= power; iLoop++)
{
lOut *= two;
}
return lOut;
}
```

Here is the entire testbed sketch:

```
int iMult = 0;
int Pwr = 15;
void setup()
{
Serial.begin(9600);
Serial.println("Initialized");
iMult = iPow(Pwr);
}
void loop()
{
Serial.print ("The iPow of ");
Serial.print (Pwr);
Serial.print (" is ");
//print_hex(iMult,24);
Serial.print (iMult);
Serial.println();
delay(2000);
}
unsigned long iPow(int power)
{
// Integer version of pow() function
// Used to get powers of 2
// Because the build in pow() function uses doubles and has rounding errors
unsigned long lOut = 1UL;
const unsigned long two = 2UL;
for (int iLoop = 1; iLoop <= power; iLoop++)
{
lOut *= two;
}
return lOut;
}
void print_hex(int v, int num_places)
{
int mask=0, n, num_nibbles, digit;
for (n=1; n<=num_places; n++)
{
mask = (mask << 1) | 0x0001;
}
v = v & mask; // truncate v to specified number of places
num_nibbles = num_places / 4;
if ((num_places % 4) != 0)
{
++num_nibbles;
}
do
{
digit = ((v >> (num_nibbles-1) * 4)) & 0x0f;
Serial.print(digit, HEX);
}
while(--num_nibbles);
} // end print_hex
```

I know I’m doing something really simple wrong and I just can’t see the forest for the trees!