microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/copilot

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/language/BitwiseOperators.qs

85lines · modecode

1/// # Sample
2/// Bitwise Operators
3///
4/// # Description
5/// Bitwise operators in Q# perform operations on the bits of integer values,
6/// producing a new integer value. The bitwise operators in Q# are
7/// `~~~`, `&&&`, `|||`, `^^^`, `>>>`, and `<<<`.
8namespace MyQuantumApp {
9
10 @EntryPoint()
11 function Main() : Unit {
12
13 // `~~~` performs a bitwise NOT on the bits of an integer.
14
15 // The integer value -6.
16 let integer = ~~~5;
17 Message($"Bitwise NOT: {integer}");
18
19 // The integer value 4.
20 let integer = ~~~-5;
21 Message($"Bitwise NOT: {integer}");
22
23 // `&&&` performs a bitwise AND on the bits of two integers.
24
25 // The integer value 4.
26 let integer = 5 &&& 6;
27 Message($"Bitwise AND: {integer}");
28
29 // The integer value 2.
30 let integer = -5 &&& 6;
31 Message($"Bitwise AND: {integer}");
32
33 // `|||` performs a bitwise OR on the bits of two integers.
34
35 // The integer value 7.
36 let integer = 5 ||| 6;
37 Message($"Bitwise OR: {integer}");
38
39 // The integer value -1.
40 let integer = -5 ||| 6;
41 Message($"Bitwise OR: {integer}");
42
43 // `^^^` performs a bitwise XOR on the bits of two integers.
44
45 // The integer value 3.
46 let integer = 5 ^^^ 6;
47 Message($"Bitwise XOR: {integer}");
48
49 // The integer value -3.
50 let integer = -5 ^^^ 6;
51 Message($"Bitwise XOR: {integer}");
52
53 // `>>>` performs a signed right bit-shift of a magnitude specified by the
54 // right-hand integer on the bits of the left-hand integer.
55 // If the right-hand integer is negative, it reverses the direction of the bit-shift.
56
57 // The integer value 1.
58 let integer = 5 >>> 2;
59 Message($"Right Bit-shift: {integer}");
60
61 // The integer value -2.
62 let integer = -5 >>> 2;
63 Message($"Right Bit-shift: {integer}");
64
65 // The integer value 20.
66 let integer = 5 >>> -2;
67 Message($"Right Bit-shift: {integer}");
68
69 // `<<<` performs a signed left bit-shift of a magnitude specified by the
70 // right-hand integer on the bits of the left-hand integer.
71 // If the right-hand integer is negative, it reverses the direction of the bit-shift.
72
73 // The integer value 20.
74 let integer = 5 <<< 2;
75 Message($"Left Bit-shift: {integer}");
76
77 // The integer value -20.
78 let integer = -5 <<< 2;
79 Message($"Left Bit-shift: {integer}");
80
81 // The integer value 1.
82 let integer = 5 <<< -2;
83 Message($"Left Bit-shift: {integer}");
84 }
85}
86