Math in c#...  
Author Message
The Angel Man





PostPosted: .NET Base Class Library, Math in c#... Top

I'm a young man with a problem

1. I got Skizofrenia/Asperger...

2. whay dossent this work:

using System;

namespace IVatNoV

{

class engine_More

{

static void Main(string[] args)

{

///purpose: determine whether a number is a power of 2

///input: the number being checked

///output: true if it's a power of 2, else false

bool powOfTwo(int num)

{

return !(num & (num - 1));

}

}

}

}

I seem to be lost in VC#... Read the c# bible and the c# black book, besides O'Rileys Learning c#... Dos'ent seem 2 work!




.NET Development6  
 
 
RizwanSharp





PostPosted: .NET Base Class Library, Math in c#... Top

You code is wrong! Use this:

using System;

namespace IVatNoV

{

class engine_More

{

static void Main(string[] args)

{

///purpose: determine whether a number is a power of 2

///input: the number being checked

///output: true if it's a power of 2, else false

Console.WriteLine(powOfTwo(4).ToString());

}

static bool powOfTwo(int num)

{

return

static void Main()

{

Console.WriteLine(PowOfTwo(4).ToString());

//Application.EnableVisualStyles();

//Application.SetCompatibleTextRenderingDefault(false);

//Application.Run(new Form1());

}

static bool PowOfTwo(int num)

{

return (num%2 == 0); //Return true if num is totally divisible byt 2 with remainder 0

}

}

}

}

Although this is not what you need but this code is atleaset executable now build your logic to check if its a power of 2 or not and return it!!!

Best Regards,



 
 
ThE_lOtUs





PostPosted: .NET Base Class Library, Math in c#... Top

Try This

using System;

namespace IsPow2

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

int t;

for(t=0;t<33;t++)

Console.WriteLine(t + ": " + IsPow2(t));

Console.ReadLine();

}

static bool IsPow2(int num)

{

return (num & (num - 1)) == 0;

}

}

}



 
 
The Angel Man





PostPosted: .NET Base Class Library, Math in c#... Top

Cut I then do:

using System;

namespace tester

{

class Run

{

static bool isPowOfTwo(int t, int num)

{

for(t;t=0;t++)

return ( num : 2 && num = 1 True : False );

}

[STAThread]

public static void Main()

{

Tester test = new Tester();

test.Run();

}

}