성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C#의 Shift 비트 연산 정리</h1> <p> 우선, 모델이 되는 숫자 하나를 골라볼까요? ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > uint un = 2320987651; int n = 2020987651; OutputBitText(un + "[unsigned] ==> \t", un); OutputBitText(n + "[signed] ==> \t", n); private static void OutputBitText(string text, uint n) { Console.WriteLine(text + " " + Convert.ToString(n, 2).PadLeft(32, '0')); } private static void OutputBitText(string text, int n) { Console.WriteLine(text + " " + Convert.ToString(n, 2).PadLeft(32, '0')); } // 출력 결과 2320987651[unsigned] ==> 10001010010101110111011000000011, 2320987651 -1753885949[signed] ==> 10010111011101011101001100000011, -1753885949 </pre> <br /> 여기서 기본적인 C#의 Bit Shift 연산자(<<, >>)를 사용해 보겠습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 2320987651[unsigned] ==> <span style='color: blue; font-weight: bold'>10001010</span>01010111011101100000<span style='color: blue; font-weight: bold'>0011</span>, 2320987651 [unsigned] >> 4 ==> 0000<span style='color: blue; font-weight: bold'>10001010</span>01010111011101100000, 145061728 [unsigned] << 4 ==> <span style='color: blue; font-weight: bold'>1010</span>01010111011101100000<span style='color: blue; font-weight: bold'>0011</span>0000, 2776064048 -1753885949[signed] ==> <span style='color: blue; font-weight: bold'>10010111</span>01110101110100110000<span style='color: blue; font-weight: bold'>0011</span>, -1753885949 [signed] >> 4 ==> <span style='color: blue; font-weight: bold'>11111001</span>011101110101110100110000, -109617872 [signed] << 4 ==> <span style='color: blue; font-weight: bold'>0111</span>01110101110100110000<span style='color: blue; font-weight: bold'>0011</span>0000, 2002595888 </pre> <br /> 보시는 것처럼, C#의 기본 Bit Shift 연산자는 다음과 같은 규칙이 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > unsigned의 경우, 밀려난 비트들에 대한 처리를 하지 않고 최상위 비트에 대한 처리도 없음 signed의 경우, 우측 shift 연산자는 최상위 부호 비트를 유지하면서 밀려난 비트들에 대한 처리를 하지 않고, 좌측 shift 연산자는 부호 비트를 지키지 않고, 역시 밀려난 비트들에 대한 처리를 하지 않는다. </pre> <br /> 자, 그럼 여기서 부호 비트는 상관없이 순수하게 rotation 식의 shift 연산을 수행하려면 어떻게 해야 할까요? 이를 위해서는 다음과 같은 보조 함수를 만들어줘야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# bitwise rotate left and rotate right ; <a target='tab' href='http://stackoverflow.com/questions/812022/c-sharp-bitwise-rotate-left-and-rotate-right'>http://stackoverflow.com/questions/812022/c-sharp-bitwise-rotate-left-and-rotate-right</a> </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > static uint UnsignedLeftShift(uint number, int shift) { return (uint)((number << shift) | (number >> (32 - shift))); } static uint UnsignedRightShift(uint number, int shift) { return (uint)((number >> shift) | (number << (32 - shift))); } </pre> <br /> 사용법은 unsigned의 경우 그냥 사용하면 되지만, signed의 경우에는 unsigned 형변환을 해줘야만 정상적으로 rotation이 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > OutputBitText("[rotation] >> 4 ==> \t\t", UnsignedRightShift(un, 4)); OutputBitText("[rotation] << 4 ==> \t\t", UnsignedLeftShift(un, 4)); OutputBitText("[rotation] >> 4 ==> \t\t", UnsignedRightShift(<span style='color: blue; font-weight: bold'>(uint)n</span>, 4)); OutputBitText("[rotation] << 4 ==> \t\t", UnsignedLeftShift(<span style='color: blue; font-weight: bold'>(uint)n</span>, 4)); // 출력 결과 2320987651[unsigned] ==> <span style='color: blue; font-weight: bold'>10001010</span>01010111011101100000<span style='color: blue; font-weight: bold'>0011</span>, 2320987651 [rotation] >> 4 ==> <span style='color: blue; font-weight: bold'>00111000</span>101001010111011101100000, 950368096 [rotation] << 4 ==> <span style='color: blue; font-weight: bold'>1010</span>01010111011101100000<span style='color: blue; font-weight: bold'>00111000</span>, 2776064056 -1753885949[signed] ==> <span style='color: blue; font-weight: bold'>10010111</span>01110101110100110000<span style='color: blue; font-weight: bold'>0011</span>, -1753885949 [rotation] >> 4 ==> <span style='color: blue; font-weight: bold'>00111001</span>011101110101110100110000, 964123952 [rotation] << 4 ==> <span style='color: blue; font-weight: bold'>0111</span>01110101110100110000<span style='color: blue; font-weight: bold'>00111001</span>, 2002595897 </pre> <br /> 마지막으로 자바의 unsigned right shift 연산자(>>>)에 해당하는 C#의 코드는 어떻게 구현해야 할까요?<br /> <br /> 비교를 위해 우선 자바의 비트 처리 값을 확인해 보면 다음과 같은데요.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ==== Java ==== n == 320987651 == 00010011001000011110001000000011 -n == -320987651 == 11101100110111100001110111111101 n >>> 3 == 40123456 == 00000010011001000011110001000000 -n >>> 3 == 496747455 == 00011101100110111100001110111111 </pre> <br /> 즉, 최상위 비트를 무시하고 무조건 shift 연산만을 수행하는 것입니다. 이에 대한 C#의 동일한 작업은 다음의 글에서 소개하는 것처럼 간단합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > bitwise unsigned right shift >>> ; <a target='tab' href='http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6c892d10-75ff-4f4e-a555-3017f72ec170/'>http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6c892d10-75ff-4f4e-a555-3017f72ec170/</a> </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > static int TripleRightShift(int number, int shift) { return (int)((uint)number >> shift); } // 출력 결과 320987651 >>> 3 == 00000010011001000011110001000000, 40123456 -320987651 >>> 3 == 00011101100110111100001110111111, 496747455 </pre> <br /> 보시는 것처럼, 자바의 >>> 연산자와 동일한 출력 결과를 보입니다.<br /> <br /> 그럼... 정리가 다 된 것 같군요. ^^<br /> <br /> (<a target='tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=701&boardid=331301885'>첨부된 파일은 위의 코드를 포함한 예제 프로젝트</a>입니다.)<br /> <br /> 부가적으로 다음의 글도 예전에 쓴 적이 있답니다. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# - Right operand가 음수인 Shift 연산 결과 ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/1008'>http://www.sysnet.pe.kr/2/0/1008</a> </pre> </p><br /> <br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1927
(왼쪽의 숫자를 입력해야 합니다.)