성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 그냥 RSS Reader 기능과 약간의 UI 편의성 때문에 사용...
[이종효] 오래된 소프트웨어는 보안 위협이 되기도 합니다. 혹시 어떤 기능...
[정성태] @Keystroke IEEE의 문서를 소개해 주시다니... +_...
[손민수 (Keystroke)] 괜히 듀얼채널 구성할 때 한번에 같은 제품 사라고 하는 것이 아...
[정성태] 전각(Full-width)/반각(Half-width) 기능을 토...
[정성태] Vector에 대한 내용은 없습니다. Vector가 닷넷 BCL...
[orion] 글 읽고 찾아보니 디자인 타임에는 InitializeCompon...
[orion] 연휴 전에 재현 프로젝트 올리자 생각해 놓고 여의치 않아서 못 ...
[정성태] 아래의 글에 정리했으니 참고하세요. C# - Typed D...
[정성태] 간단한 재현 프로젝트라도 있을까요? 저런 식으로 설명만 해...
글쓰기
제목
이름
암호
전자우편
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# 11 - 새로운 연산자 ">>>" (Unsigned Right Shift)</h1> <p> (Visual Studio 2022 17.3 이후 버전에서 테스트할 수 있습니다.)<br /> <br /> 자바의 경우 "unsigned right 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;' > C#의 Shift 비트 연산 정리 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/1248'>https://www.sysnet.pe.kr/2/0/1248</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 TripleRightShift(uint number, int shift) { return ((uint)number >> shift); } </pre> <br /> 늦었다고 해야 할까요? ^^ 어쨌든 C# 11에는 자바와 동일한 역할의 Unsigned Right Shift 연산자가 기호까지 동일하게 ">>>"로 추가되었습니다.<br /> <br /> 피연산자(operand)가 unsigned 형인 경우, 기존 right shift 연산자(>>)가 어차피 부호 유지를 하지 않으므로 이런 경우에는 Unsigned Right Shift (>>>)를 써도 값에 차이가 나지는 않습니다.<br /> <br /> 하지만, 피연산자가 signed 형인 경우에는 >> 연산자가 부호 유지를 하기 때문에 최상위 비트가 1인 음의 정수형에 대해서는 ">>" 연산자와 ">>>" 연산자의 결과가 다르게 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > { int n = -2020987651; OutputBitText("[n] \t\t ", n); OutputBitText("[n] >>> 4 ==> \t ", n >>> 4); OutputBitText("[n] >> 4 ==> \t ", n >> 4); } /* 출력 결과 [n] 10000111100010100010110011111101 [n] >>> 4 ==> 00001000011110001010001011001111 [n] >> 4 ==> 11111000011110001010001011001111 */ </pre> <br /> 재미있는 것은, ">>>" 연산자가 추가되었다고 해서 그에 대응하는 새로운 IL 코드가 도입된 것은 아니라는 점입니다. 단지, C# 컴파일러가 "부호를 유지하지 않도록" 자료형을 바꾼 후 기존의 ">>" 연산자로 처리하는 것에 불과합니다. 즉, 위의 코드는 결국 다음과 같이 바뀌는 것에 불과합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > n >>> 4 ==> (int)((uint)n >> 4) // <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.shr_un'>IL 코드로는 shr.un으로 동일하게 처리</a> </pre> <br /> 그래서 현존하는 .NET Decompiler 도구들이 ">>>" 연산자가 사용되었어도 아무런 문제없이 위와 같이 코드를 보여줍니다. ^^<br /> <br /> <hr style='width: 50%' /><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 c = new C(); <span style='color: blue; font-weight: bold'>C d = c >>> 4; C e = c >>> 0.4f;</span> class C { <span style='color: blue; font-weight: bold'>public static C operator >>>(C c, int n)</span> { Console.WriteLine("right shift operator called with " + n); return c; } <span style='color: blue; font-weight: bold'>public static C operator >>>(C c, float n)</span> { Console.WriteLine("right shift operator called with " + n); return c; } } </pre> <br /> 이때의 SpecialName은 "op_UnsignedRightShift"입니다. 또한, 지난 글에 설명한 제약 역시,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 11 - shift 연산자 재정의에 대한 제약 완화 (Relaxing Shift Operator) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13100'>https://www.sysnet.pe.kr/2/0/13100</a> </pre> <br /> 적용되므로 두 번째 피연산자에 대해 int 이외의 타입을 정의할 수 있습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 11 - 인터페이스 내에 정적 추상 메서드 정의 가능 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4436'>Static Abstract Members In Interfaces C# 10 Preview</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12814'>https://www.sysnet.pe.kr/2/0/12814</a> C# 11 - 제네릭 타입의 특성 적용 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/generic-attributes'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/124'>Generic attributes</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12839'>https://www.sysnet.pe.kr/2/0/12839</a> C# 11 - 사용자 정의 checked 연산자 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/checked-user-defined-operators.md'>Checked user-defined operators</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13099'>https://www.sysnet.pe.kr/2/0/13099</a> C# 11 - shift 연산자 재정의에 대한 제약 완화 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4666'>Relaxing Shift Operator</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13100'>https://www.sysnet.pe.kr/2/0/13100</a> C# 11 - IntPtr/UIntPtr과 nint/unint의 통합 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#numeric-intptr-and-uintptr'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/numeric-intptr.md'>Numeric IntPtr</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13111'>https://www.sysnet.pe.kr/2/0/13111</a> C# 11 - 새로운 연산자 ">>>" (Unsigned Right Shift) (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4682'>Unsigned right shift operator</a>) ; https://www.sysnet.pe.kr/2/0/13110 C# 11 - 원시 문자열 리터럴 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/raw-string-literal.md'>raw string literals</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13085'>https://www.sysnet.pe.kr/2/0/13085</a> C# 11 - 문자열 보간 개선 2가지 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#newlines-in-string-interpolations'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4935'>Allow new-lines in all interpolations</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13086'>https://www.sysnet.pe.kr/2/0/13086</a> C# 11 - 목록 패턴 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/list-patterns.md'>List patterns</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13112'>https://www.sysnet.pe.kr/2/0/13112</a> C# 11 - Span 타입에 대한 패턴 매칭 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#pattern-match-spanchar-or-readonlyspanchar-on-a-constant-string'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/1881'>Pattern matching on ReadOnlySpan<char></a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13113'>https://www.sysnet.pe.kr/2/0/13113</a> C# 11 - Utf8 문자열 리터럴 지원 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/utf8-string-literals.md'>Utf8 Strings Literals</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13096'>https://www.sysnet.pe.kr/2/0/13096</a> C# 11 - ref struct에 ref 필드를 허용 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/low-level-struct-improvements.md'>ref fields</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13015'>https://www.sysnet.pe.kr/2/0/13015</a> C# 11 - 파일 범위 내에서 유효한 타입 정의 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/6011'>File-local types</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13117'>https://www.sysnet.pe.kr/2/0/13117</a> C# 11 - 메서드 매개 변수에 대한 nameof 지원 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#extended-nameof-scope'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/373'>nameof(parameter)</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13122'>https://www.sysnet.pe.kr/2/0/13122</a> C# 11 - 멤버(속성/필드)에 지정할 수 있는 required 예약어 추가 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/required-members.md'>Required members</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13123'>https://www.sysnet.pe.kr/2/0/13123</a> C# 11 - 구조체 필드의 자동 초기화 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/auto-default-structs.md'>auto-default structs</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13125'>https://www.sysnet.pe.kr/2/0/13125</a> C# 11 - 정적 메서드에 대한 delegate 처리 시 cache 적용 (공식 문서, <a target='tab' href='https://github.com/dotnet/roslyn/issues/5835'>Cache delegates for static method group</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13126'>https://www.sysnet.pe.kr/2/0/13126</a> Language Feature Status ; <a target='tab' href='https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md'>https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md</a> </pre> </p><br /> <br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1795
(왼쪽의 숫자를 입력해야 합니다.)