/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/shared/BitwiseAndShiftOperators.cs
237 строк
7 KB
Bill Wagner
Use Collection expressions for all language reference samples (#37993)
16 ноя 2023, 23:06
Не верифицирован
16 ноя 2023, 23:06
ace7aec
Код
Авторство
О чём код?
namespace operators; public static class BitwiseAndShiftOperators { public static void Examples() { Console.WriteLine("==== ~, &, ^, and | operators"); BitwiseComplement(); BitwiseAnd(); BitwiseXor(); BitwiseOr(); Console.WriteLine("==== <<, >>, and >>> operators"); LeftShift(); RightShift(); ShiftCount(); UnsignedRightShift(); Console.WriteLine("==== Additional examples"); CompoundAssignment(); CompoundAssignmentWithCast(); Precedence(); } private static void BitwiseComplement() { // <SnippetBitwiseComplement> uint a = 0b_0000_1111_0000_1111_0000_1111_0000_1100; uint b = ~a; Console.WriteLine(Convert.ToString(b, toBase: 2)); // Output: // 11110000111100001111000011110011 // </SnippetBitwiseComplement> } private static void BitwiseAnd() { // <SnippetBitwiseAnd> uint a = 0b_1111_1000; uint b = 0b_1001_1101; uint c = a & b; Console.WriteLine(Convert.ToString(c, toBase: 2)); // Output: // 10011000 // </SnippetBitwiseAnd> } private static void BitwiseXor() { // <SnippetBitwiseXor> uint a = 0b_1111_1000; uint b = 0b_0001_1100; uint c = a ^ b; Console.WriteLine(Convert.ToString(c, toBase: 2)); // Output: // 11100100 // </SnippetBitwiseXor> } private static void BitwiseOr() { // <SnippetBitwiseOr> uint a = 0b_1010_0000; uint b = 0b_1001_0001; uint c = a | b; Console.WriteLine(Convert.ToString(c, toBase: 2)); // Output: // 10110001 // </SnippetBitwiseOr> } private static void LeftShift() { // <SnippetLeftShift> uint x = 0b_1100_1001_0000_0000_0000_0000_0001_0001; Console.WriteLine($"Before: {Convert.ToString(x, toBase: 2)}"); uint y = x << 4; Console.WriteLine($"After: {Convert.ToString(y, toBase: 2)}"); // Output: // Before: 11001001000000000000000000010001 // After: 10010000000000000000000100010000 // </SnippetLeftShift> // <SnippetLeftShiftPromoted> byte a = 0b_1111_0001; var b = a << 8; Console.WriteLine(b.GetType()); Console.WriteLine($"Shifted byte: {Convert.ToString(b, toBase: 2)}"); // Output: // System.Int32 // Shifted byte: 1111000100000000 // </SnippetLeftShiftPromoted> } private static void RightShift() { // <SnippetRightShift> uint x = 0b_1001; Console.WriteLine($"Before: {Convert.ToString(x, toBase: 2), 4}"); uint y = x >> 2; Console.WriteLine($"After: {Convert.ToString(y, toBase: 2).PadLeft(4, '0'), 4}"); // Output: // Before: 1001 // After: 0010 // </SnippetRightShift> // <SnippetArithmeticRightShift> int a = int.MinValue; Console.WriteLine($"Before: {Convert.ToString(a, toBase: 2)}"); int b = a >> 3; Console.WriteLine($"After: {Convert.ToString(b, toBase: 2)}"); // Output: // Before: 10000000000000000000000000000000 // After: 11110000000000000000000000000000 // </SnippetArithmeticRightShift> // <SnippetLogicalRightShift> uint c = 0b_1000_0000_0000_0000_0000_0000_0000_0000; Console.WriteLine($"Before: {Convert.ToString(c, toBase: 2), 32}"); uint d = c >> 3; Console.WriteLine($"After: {Convert.ToString(d, toBase: 2).PadLeft(32, '0'), 32}"); // Output: // Before: 10000000000000000000000000000000 // After: 00010000000000000000000000000000 // </SnippetLogicalRightShift> } private static void UnsignedRightShift() { // <SnippetUnsignedRightShift> int x = -8; Console.WriteLine($"Before: {x,11}, hex: {x,8:x}, binary: {Convert.ToString(x, toBase: 2), 32}"); int y = x >> 2; Console.WriteLine($"After >>: {y,11}, hex: {y,8:x}, binary: {Convert.ToString(y, toBase: 2), 32}"); int z = x >>> 2; Console.WriteLine($"After >>>: {z,11}, hex: {z,8:x}, binary: {Convert.ToString(z, toBase: 2).PadLeft(32, '0'), 32}"); // Output: // Before: -8, hex: fffffff8, binary: 11111111111111111111111111111000 // After >>: -2, hex: fffffffe, binary: 11111111111111111111111111111110 // After >>>: 1073741822, hex: 3ffffffe, binary: 00111111111111111111111111111110 // </SnippetUnsignedRightShift> } private static void ShiftCount() { // <SnippetShiftCount> int count1 = 0b_0000_0001; int count2 = 0b_1110_0001; int a = 0b_0001; Console.WriteLine($"{a} << {count1} is {a << count1}; {a} << {count2} is {a << count2}"); // Output: // 1 << 1 is 2; 1 << 225 is 2 int b = 0b_0100; Console.WriteLine($"{b} >> {count1} is {b >> count1}; {b} >> {count2} is {b >> count2}"); // Output: // 4 >> 1 is 2; 4 >> 225 is 2 int count = -31; int c = 0b_0001; Console.WriteLine($"{c} << {count} is {c << count}"); // Output: // 1 << -31 is 2 // </SnippetShiftCount> } private static void CompoundAssignment() { // <SnippetCompoundAssignment> uint INITIAL_VALUE = 0b_1111_1000; uint a = INITIAL_VALUE; a &= 0b_1001_1101; Display(a); // output: 10011000 a = INITIAL_VALUE; a |= 0b_0011_0001; Display(a); // output: 11111001 a = INITIAL_VALUE; a ^= 0b_1000_0000; Display(a); // output: 01111000 a = INITIAL_VALUE; a <<= 2; Display(a); // output: 1111100000 a = INITIAL_VALUE; a >>= 4; Display(a); // output: 00001111 a = INITIAL_VALUE; a >>>= 4; Display(a); // output: 00001111 void Display(uint x) => Console.WriteLine($"{Convert.ToString(x, toBase: 2).PadLeft(8, '0'), 8}"); // </SnippetCompoundAssignment> } private static void CompoundAssignmentWithCast() { // <SnippetCompoundAssignmentWithCast> byte x = 0b_1111_0001; int b = x << 8; Console.WriteLine($"{Convert.ToString(b, toBase: 2)}"); // output: 1111000100000000 x <<= 8; Console.WriteLine(x); // output: 0 // </SnippetCompoundAssignmentWithCast> } private static void Precedence() { // <SnippetPrecedence> uint a = 0b_1101; uint b = 0b_1001; uint c = 0b_1010; uint d1 = a | b & c; Display(d1); // output: 1101 uint d2 = (a | b) & c; Display(d2); // output: 1000 void Display(uint x) => Console.WriteLine($"{Convert.ToString(x, toBase: 2), 4}"); // </SnippetPrecedence> } }