.

Convert to binary

using functions:

[Test]
public void binaryTest()
{
    Func<int, long> twopow = i => 1 << i;
    Func<long, int, bool> contain2pow = (orig, pow) => (twopow(pow) & orig) > 0;
    Func<long, int> binlenght = (orig) =>  Enumerable.Range(1, int.MaxValue).Where(m=>twopow(m) >orig).First();
    Func<long, IEnumerable<bool>> bits = (orig) =>  Enumerable.Range(0, binlenght(orig)).Select(m=>contain2pow(orig,m));

    var j = 123456789;
    var b = bits(j).Reverse().Select(m=>m?"1":"0");
    var @join = string.Join("", b);
    Assert.AreEqual("111010110111100110100010101", @join);
    string conversionToBinary = Convert.ToInt32("111010110111100110100010101", 2).ToString();
    Assert.AreEqual("123456789", conversionToBinary);
}

What's your thoughts on this?

*

Protected by WP Anti Spam