본문 바로가기
C#

25# 비트 연산

by NaHyungMin 2019. 10. 11.

어떤 숫자를 암호화하기는 낭비고 조금 귀찮게 만들기 위해 비트 연산을 사용하기로 했다.

 

static void Main(string[] args)

{

    int user = 1;

    int key = 34284388;

 

    int crtpograph = user ^ key;

    Console.WriteLine(crtpograph);

    int decrpt = crtpograph ^ key;

    Console.WriteLine(decrpt);

 

    int a = 1;

    var b = a << 8;

    Console.WriteLine(b);

    var c = b >> 8;

    Console.WriteLine(c);

    Console.Read();

}

 

내가 채택한 방식은 XOR과 Shift 연산이다.

성능에 대해 찾아봤더니 닷넷 구조상 별 다른 차이는 없는것 같다.

 

아래는 양형의 실행 결과

AND operator7979680 ticks

OR operator7826806 ticks

XOR operator7826806 ticks

NOT operator7826806 ticks

Left shift operator7826806 ticks

Right shift operator7826806 ticks

 

https://stackoverflow.com/questions/21435805/fastest-bitwise-operation-in-c-sharp

 

Fastest Bitwise Operation in C#

Is any particular bitwise operation (AND, OR, XOR, etc.) faster than another in C#? The reason I ask is because I know that in hardware, most things are built using NAND gates, because a few NAND

stackoverflow.com

'