microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
main

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/language/BitwiseOperators.qs

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