Exemplo n.º 1
0
        static void Main(string[] args)
        {
            int a = 0b_1011;

            Console.WriteLine($"Int {a} is binary {a.AsBinary(4)}");

            a = 0b_0101;
            int b = 0b_1001;

            int c = a & b;

            Console.WriteLine($"{a.AsBinary(4)} & {b.AsBinary(4)} = {c.AsBinary(4)}");

            c = a | b;
            Console.WriteLine($"{a.AsBinary(4)} | {b.AsBinary(4)} = {c.AsBinary(4)}");

            c = a ^ b;
            Console.WriteLine($"{a.AsBinary(4)} ^ {b.AsBinary(4)} = {c.AsBinary(4)}");

            c = ~a;
            // c is 32-bit number, so all bits get toggled
            // let's get only last 4 bits
            string bits = c.AsBinary(4);

            bits = bits.Substring(bits.Length - 4, 4);
            Console.WriteLine($"~{a.AsBinary(4)} = {bits}");
            Console.WriteLine("--------");
            for (sbyte i = 3; i > -4; i--)
            {
                Console.WriteLine($"Int {i} is binary {i.AsBinary()}");
            }
            Console.WriteLine("--------");
            c = 1;
            Console.WriteLine($"Int {c} is binary {c.AsBinary(8)}");
            // make a number negative
            c = ~c + 1;
            Console.WriteLine($"Int {c} is binary {c.AsBinary(8).Substring(0,8)}");
            Console.WriteLine("-------- Shift Left --------");

            TestShiftLeft();
            Console.WriteLine("-------- Shift Right -------");
            TestShiftRight();
            Console.WriteLine("-------- Shift Right Signed ");
            TestShiftRightSigned();
        }