Thread overview
How to use Power on D
Jun 13, 2013
Carlos
Jun 13, 2013
Infiltrator
Jun 13, 2013
bearophile
Jun 13, 2013
Carlos
June 13, 2013
So I have this code I'm working on but I get weird results. What am I doing wrong ?

Code :

import std.stdio;
import std.c.stdlib;

void main()
{
foreach (count; 1 .. 16){
	write("Result : ", (2)^(count), " from : ", count, "\n");
	}
}

Prints:

Result : 3 from : 1
Result : 0 from : 2
Result : 1 from : 3
Result : 6 from : 4
Result : 7 from : 5
Result : 4 from : 6
Result : 5 from : 7
Result : 10 from : 8
Result : 11 from : 9
Result : 8 from : 10
Result : 9 from : 11
Result : 14 from : 12
Result : 15 from : 13
Result : 12 from : 14
Result : 13 from : 15

Thank you for your time.
June 13, 2013
On Thursday, 13 June 2013 at 00:24:18 UTC, Carlos wrote:
> So I have this code I'm working on but I get weird results. What am I doing wrong ?
>
> Code :
>
> import std.stdio;
> import std.c.stdlib;
>
> void main()
> {
> foreach (count; 1 .. 16){
> 	write("Result : ", (2)^(count), " from : ", count, "\n");
> 	}
> }
>
> Prints:
>
> Result : 3 from : 1
> Result : 0 from : 2
> Result : 1 from : 3
> Result : 6 from : 4
> Result : 7 from : 5
> Result : 4 from : 6
> Result : 5 from : 7
> Result : 10 from : 8
> Result : 11 from : 9
> Result : 8 from : 10
> Result : 9 from : 11
> Result : 14 from : 12
> Result : 15 from : 13
> Result : 12 from : 14
> Result : 13 from : 15
>
> Thank you for your time.


I don't see the problem.  You're XORing 2 with 1..16 and getting the correct results.

Unless you actually want std.math.pow [http://dlang.org/phobos/std_math.html#.pow]?
June 13, 2013
Carlos:

> Thank you for your time.

That's one bitwise operator. You want ^^

Bye,
bearophile
June 13, 2013
On Thursday, 13 June 2013 at 00:27:33 UTC, bearophile wrote:
> Carlos:
>
>> Thank you for your time.
>
> That's one bitwise operator. You want ^^
>
> Bye,
> bearophile

I didn't understoof in the first try but Infiltrator told me on the #d irc chat and here is the new code.

import std.stdio;
import std.c.stdlib;

void main()
{
foreach (count; 1 .. 16){
	write("Result : ", (2)^^(count), " from : ", count, "\n");
	}
}