www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use Power on D

reply "Carlos" <checoimg gmail.com> writes:
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.
Jun 12 2013
next sibling parent "Infiltrator" <Infiltrator d.irc> writes:
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]?
Jun 12 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Carlos:

 Thank you for your time.
That's one bitwise operator. You want ^^ Bye, bearophile
Jun 12 2013
parent "Carlos" <checoimg gmail.com> writes:
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"); } }
Jun 12 2013