Skip to content

Commit a375969

Browse files
pkulikovRon Petrusha
authored andcommitted
Added bitwise and shift operators snippets (#816)
1 parent c79bc96 commit a375969

File tree

2 files changed

+180
-8
lines changed

2 files changed

+180
-8
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
using System;
2+
3+
namespace operators
4+
{
5+
public static class BitwiseAndShiftOperators
6+
{
7+
public static void Examples()
8+
{
9+
Console.WriteLine("==== ~, &, ^, and | operators");
10+
BitwiseComplement();
11+
BitwiseAnd();
12+
BitwiseXor();
13+
BitwiseOr();
14+
15+
Console.WriteLine("==== << and >> operators");
16+
LeftShift();
17+
RightShift();
18+
ShiftCount();
19+
20+
Console.WriteLine("==== Additional examples");
21+
CompoundAssignment();
22+
Precedence();
23+
}
24+
25+
private static void BitwiseComplement()
26+
{
27+
// <SnippetBitwiseComplement>
28+
uint a = 0b_0000_1111_0000_1111_0000_1111_0000_1100;
29+
uint b = ~a;
30+
Console.WriteLine(Convert.ToString(b, toBase: 2));
31+
// Output:
32+
// 11110000111100001111000011110011
33+
// </SnippetBitwiseComplement>
34+
}
35+
36+
private static void BitwiseAnd()
37+
{
38+
// <SnippetBitwiseAnd>
39+
uint a = 0b_1111_1000;
40+
uint b = 0b_1001_1101;
41+
uint c = a & b;
42+
Console.WriteLine(Convert.ToString(c, toBase: 2));
43+
// Output:
44+
// 10011000
45+
// </SnippetBitwiseAnd>
46+
}
47+
48+
private static void BitwiseXor()
49+
{
50+
// <SnippetBitwiseXor>
51+
uint a = 0b_1111_1000;
52+
uint b = 0b_0001_1100;
53+
uint c = a ^ b;
54+
Console.WriteLine(Convert.ToString(c, toBase: 2));
55+
// Output:
56+
// 11100100
57+
// </SnippetBitwiseXor>
58+
}
59+
60+
private static void BitwiseOr()
61+
{
62+
// <SnippetBitwiseOr>
63+
uint a = 0b_1010_0000;
64+
uint b = 0b_1001_0001;
65+
uint c = a | b;
66+
Console.WriteLine(Convert.ToString(c, toBase: 2));
67+
// Output:
68+
// 10110001
69+
// </SnippetBitwiseOr>
70+
}
71+
72+
private static void LeftShift()
73+
{
74+
// <SnippetLeftShift>
75+
uint x = 0b_1100_1001_0000_0000_0000_0000_0001_0001;
76+
Console.WriteLine($"Before: {Convert.ToString(x, toBase: 2)}");
77+
78+
uint y = x << 4;
79+
Console.WriteLine($"After: {Convert.ToString(y, toBase: 2)}");
80+
// Output:
81+
// Before: 11001001000000000000000000010001
82+
// After: 10010000000000000000000100010000
83+
// </SnippetLeftShift>
84+
}
85+
86+
private static void RightShift()
87+
{
88+
// <SnippetRightShift>
89+
uint x = 0b_1001;
90+
Console.WriteLine($"Before: {Convert.ToString(x, toBase: 2), 4}");
91+
92+
uint y = x >> 2;
93+
Console.WriteLine($"After: {Convert.ToString(y, toBase: 2), 4}");
94+
// Output:
95+
// Before: 1001
96+
// After: 10
97+
// </SnippetRightShift>
98+
99+
// <SnippetArithmeticRightShift>
100+
int a = int.MinValue;
101+
Console.WriteLine($"Before: {Convert.ToString(a, toBase: 2)}");
102+
103+
int b = a >> 3;
104+
Console.WriteLine($"After: {Convert.ToString(b, toBase: 2)}");
105+
// Output:
106+
// Before: 10000000000000000000000000000000
107+
// After: 11110000000000000000000000000000
108+
// </SnippetArithmeticRightShift>
109+
110+
// <SnippetLogicalRightShift>
111+
uint c = 0b_1000_0000_0000_0000_0000_0000_0000_0000;
112+
Console.WriteLine($"Before: {Convert.ToString(c, toBase: 2), 32}");
113+
114+
uint d = c >> 3;
115+
Console.WriteLine($"After: {Convert.ToString(d, toBase: 2), 32}");
116+
// Output:
117+
// Before: 10000000000000000000000000000000
118+
// After: 10000000000000000000000000000
119+
// </SnippetLogicalRightShift>
120+
}
121+
122+
private static void ShiftCount()
123+
{
124+
// <SnippetShiftCount>
125+
int count1 = 0b_0000_0001;
126+
int count2 = 0b_1110_0001;
127+
128+
int a = 0b_0001;
129+
Console.WriteLine($"{a} << {count1} is {a << count1}; {a} << {count2} is {a << count2}");
130+
131+
int b = 0b_0100;
132+
Console.WriteLine($"{b} >> {count1} is {b >> count1}; {b} >> {count2} is {b >> count2}");
133+
// </SnippetShiftCount>
134+
}
135+
136+
private static void CompoundAssignment()
137+
{
138+
// <SnippetCompoundAssignment>
139+
uint a = 0b_1111_1000;
140+
a &= 0b_1001_1101;
141+
Display(a); // output: 10011000
142+
143+
a |= 0b_0011_0001;
144+
Display(a); // output: 10111001
145+
146+
a ^= 0b_1000_0000;
147+
Display(a); // output: 111001
148+
149+
a <<= 2;
150+
Display(a); // output: 11100100
151+
152+
a >>= 4;
153+
Display(a); // output: 1110
154+
155+
void Display(uint x) => Console.WriteLine($"{Convert.ToString(x, toBase: 2), 8}");
156+
// </SnippetCompoundAssignment>
157+
}
158+
159+
private static void Precedence()
160+
{
161+
// <SnippetPrecedence>
162+
uint a = 0b_1101;
163+
uint b = 0b_1001;
164+
uint c = 0b_1010;
165+
166+
uint d1 = a | b & c;
167+
Display(d1); // output: 1101
168+
169+
uint d2 = (a | b) & c;
170+
Display(d2); // output: 1000
171+
172+
void Display(uint x) => Console.WriteLine($"{Convert.ToString(x, toBase: 2), 4}");
173+
// </SnippetPrecedence>
174+
}
175+
}
176+
}

snippets/csharp/language-reference/operators/Program.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ static void Main(string[] args)
1818
LogicalOperators.Examples();
1919
Console.WriteLine();
2020

21+
Console.WriteLine("==== Bitwise and shift operators examples ======");
22+
BitwiseAndShiftOperators.Examples();
23+
Console.WriteLine();
24+
2125
Console.WriteLine("============== + operator examples =============");
2226
AdditionExamples.Examples();
2327
Console.WriteLine();
@@ -34,10 +38,6 @@ static void Main(string[] args)
3438
ConditionalExamples.Examples();
3539
Console.WriteLine();
3640

37-
Console.WriteLine("============== ~ operator examples =============");
38-
BitwiseComplementExamples.Examples();
39-
Console.WriteLine();
40-
4141
Console.WriteLine("======= >, <, >=, and <= operator examples =====");
4242
GreaterAndLessOperatorsExamples.Examples();
4343
Console.WriteLine();
@@ -54,10 +54,6 @@ static void Main(string[] args)
5454
LambdaOperatorExamples.Examples();
5555
Console.WriteLine();
5656

57-
Console.WriteLine("===== <<, <<=, >>, and >>= operator examples ===");
58-
ShiftOperatorsExamples.Examples();
59-
Console.WriteLine();
60-
6157
Console.WriteLine("============== . operator examples =============");
6258
MemberAccessExamples.Examples();
6359
Console.WriteLine();

0 commit comments

Comments
 (0)