07. 비트 연산 — 웹툰PD 지망생 : 정민재
07. 비트 연산
한국 웹툰 리뷰/백합

07. 비트 연산

728x90

 

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 1;

            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)

            num = num << 1;

            Console.WriteLine(num);
        }
    }
}

 

 

모든 숫자들이

왼쪽으로 1칸씩 이동했다는 뜻

 

 

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 1;

            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)

            num = num << 3;

            Console.WriteLine(num);
        }
    }
}

 

왜냐하면

 

 

 

모든 숫자들이 왼쪽으로 3칸 이동했다는 뜻

 

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 8;

            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)

            num = num >> 1;

            Console.WriteLine(num);
        }
    }
}

 

8에서 모든 숫자들을 1칸 오른쪽으로 이동하면?

 

 

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 1108;

            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)

            num = num >> 1;

            Console.WriteLine(num);
        }
    }
}

왜냐하면

 

여기서 모든 숫자들을 오른쪽으로 1칸씩 이동 시키면

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = -2147483648;

            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)

            num = num >> 1;

            Console.WriteLine(num);
        }
    }
}

 

왜냐하면

 

여기서 모든 숫자들을 오른쪽으로 1칸씩 이동시키면,

 

 

이렇게 하면 되지만, 사실 틀렸다.

 

마이너스(-)인 경우에는,

 

 

원래 있던 값을 놔두고 오른쪽으로 1칸 씩 이동시킨다.

 

 

이제 비트 연산자 &(and)와 |(or)을 사용해보자

 

 

비트 연산자 &(and)

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 21;
            int num2 = 14;
            int a;
            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)
            
            a = (num1 & num2);

            Console.WriteLine(a);
        }
    }
}

 

 

이번에는 비트 연산자 %(and)를 알아보자

비트 연산자 %(and)를 알아보기 위해

 

변수 두 개를 예로 들어보자

 

ex) 21, 14

 

 

 

 

 

비트연산자 |(or)

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 21;
            int num2 = 14;
            int a;
            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)
            
            a = (num1 | num2);

            Console.WriteLine(a);
        }
    }
}

 

 

비트 연산자 ~(not)

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 37;
            
            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)
            
            Console.WriteLine(~num);
        }
    }
}

 

 

 

 

 

 

비트 연산자 ^(xor)

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 21;
            int num2 = 14;
            int a;
            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)
            
            a = (num1 ^ num2);

            Console.WriteLine(a);
        }
    }
}

 

 

 

 

 

 

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //비트 연산자 : << >> &(and) |(or) ^(xor) ~(not)

            int id = 123;
            int key = 401;

            int a = id ^ key;
            int b = a ^ key;

            Console.WriteLine(a);

            //비트 연산자 ^(xor) 을 2번하면 다시 자기자신이다. 그렇기 때문에 암호학에서 선호
            Console.WriteLine(b);
        }
    }
}

 

728x90