How to print bits of an int in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 127;

            string bits = Convert.ToString(n, 2).PadLeft(8, '0');
            Console.WriteLine(bits);

            bits = Convert.ToString(7, 2).PadLeft(8, '0');
            Console.WriteLine(bits);

            bits = Convert.ToString(15, 2).PadLeft(8, '0');
            Console.WriteLine(bits);

            bits = Convert.ToString(128, 2).PadLeft(8, '0');
            Console.WriteLine(bits);
        }
    }
}


/*
run:
    
01111111
00000111
00001111
10000000

*/

 



answered Feb 14, 2017 by avibootz

Related questions

1 answer 109 views
1 answer 131 views
1 answer 118 views
1 answer 110 views
1 answer 98 views
1 answer 113 views
1 answer 93 views
...