1{
2 "cells": [
3  {
4   "cell_type": "markdown",
5   "metadata": {},
6   "source": [
7    "# Integer logic and bit fiddling\n",
8    "## Preliminaries\n",
9    "\n",
10    "Let us load the mp++ runtime, include the ``integer.hpp`` header and import the user-defined literals:"
11   ]
12  },
13  {
14   "cell_type": "code",
15   "execution_count": 1,
16   "metadata": {},
17   "outputs": [],
18   "source": [
19    "#pragma cling add_include_path(\"$CONDA_PREFIX/include\")\n",
20    "#pragma cling add_library_path(\"$CONDA_PREFIX/lib\")\n",
21    "#pragma cling load(\"mp++\")\n",
22    "\n",
23    "#include <mp++/integer.hpp>\n",
24    "\n",
25    "using namespace mppp::literals;"
26   ]
27  },
28  {
29   "cell_type": "markdown",
30   "metadata": {},
31   "source": [
32    "Let us also include a few useful bits from the standard library:"
33   ]
34  },
35  {
36   "cell_type": "code",
37   "execution_count": 2,
38   "metadata": {},
39   "outputs": [],
40   "source": [
41    "#include <iostream>"
42   ]
43  },
44  {
45   "cell_type": "markdown",
46   "metadata": {},
47   "source": [
48    "## Logic and bit fiddling"
49   ]
50  },
51  {
52   "cell_type": "markdown",
53   "metadata": {},
54   "source": [
55    "Multiprecision integers support the standard bitwise operators, such as the bitwise NOT operator:"
56   ]
57  },
58  {
59   "cell_type": "code",
60   "execution_count": 3,
61   "metadata": {},
62   "outputs": [
63    {
64     "data": {
65      "text/plain": [
66       "-1"
67      ]
68     },
69     "execution_count": 3,
70     "metadata": {},
71     "output_type": "execute_result"
72    }
73   ],
74   "source": [
75    "~(0_z1)"
76   ]
77  },
78  {
79   "cell_type": "code",
80   "execution_count": 4,
81   "metadata": {},
82   "outputs": [
83    {
84     "data": {
85      "text/plain": [
86       "-124"
87      ]
88     },
89     "execution_count": 4,
90     "metadata": {},
91     "output_type": "execute_result"
92    }
93   ],
94   "source": [
95    "~(123_z1)"
96   ]
97  },
98  {
99   "cell_type": "markdown",
100   "metadata": {},
101   "source": [
102    "Negative integers are treated as-if they were represented using two’s complement:"
103   ]
104  },
105  {
106   "cell_type": "code",
107   "execution_count": 5,
108   "metadata": {},
109   "outputs": [
110    {
111     "data": {
112      "text/plain": [
113       "0"
114      ]
115     },
116     "execution_count": 5,
117     "metadata": {},
118     "output_type": "execute_result"
119    }
120   ],
121   "source": [
122    "~(-1_z1)"
123   ]
124  },
125  {
126   "cell_type": "code",
127   "execution_count": 6,
128   "metadata": {},
129   "outputs": [
130    {
131     "data": {
132      "text/plain": [
133       "123"
134      ]
135     },
136     "execution_count": 6,
137     "metadata": {},
138     "output_type": "execute_result"
139    }
140   ],
141   "source": [
142    "~(-124_z1)"
143   ]
144  },
145  {
146   "cell_type": "markdown",
147   "metadata": {},
148   "source": [
149    "The bitwise OR, AND and XOR operators:"
150   ]
151  },
152  {
153   "cell_type": "code",
154   "execution_count": 8,
155   "metadata": {},
156   "outputs": [
157    {
158     "name": "stdout",
159     "output_type": "stream",
160     "text": [
161      "11011110"
162     ]
163    }
164   ],
165   "source": [
166    "std::cout << (0b01001010_z1 | 0b10010100_z1).to_string(2);"
167   ]
168  },
169  {
170   "cell_type": "code",
171   "execution_count": 10,
172   "metadata": {},
173   "outputs": [
174    {
175     "name": "stdout",
176     "output_type": "stream",
177     "text": [
178      "10000000"
179     ]
180    }
181   ],
182   "source": [
183    "std::cout << (0b11001010_z1 & 0b10010100_z1).to_string(2);"
184   ]
185  },
186  {
187   "cell_type": "code",
188   "execution_count": 11,
189   "metadata": {},
190   "outputs": [
191    {
192     "name": "stdout",
193     "output_type": "stream",
194     "text": [
195      "1011110"
196     ]
197    }
198   ],
199   "source": [
200    "std::cout << (0b11001010_z1 ^ 0b10010100_z1).to_string(2);"
201   ]
202  },
203  {
204   "cell_type": "markdown",
205   "metadata": {},
206   "source": [
207    "Multiprecision integer arguments can be mixed with C++ integral arguments:"
208   ]
209  },
210  {
211   "cell_type": "code",
212   "execution_count": 12,
213   "metadata": {},
214   "outputs": [
215    {
216     "name": "stdout",
217     "output_type": "stream",
218     "text": [
219      "1101111"
220     ]
221    }
222   ],
223   "source": [
224    "std::cout << (0b01001010_z1 | 45).to_string(2);"
225   ]
226  },
227  {
228   "cell_type": "code",
229   "execution_count": 13,
230   "metadata": {},
231   "outputs": [
232    {
233     "name": "stdout",
234     "output_type": "stream",
235     "text": [
236      "10000100"
237     ]
238    }
239   ],
240   "source": [
241    "std::cout << (-123ll & 0b10010100_z1).to_string(2);"
242   ]
243  },
244  {
245   "cell_type": "code",
246   "execution_count": 14,
247   "metadata": {},
248   "outputs": [
249    {
250     "name": "stdout",
251     "output_type": "stream",
252     "text": [
253      "1101011"
254     ]
255    }
256   ],
257   "source": [
258    "std::cout << (255u ^ 0b10010100_z1).to_string(2);"
259   ]
260  },
261  {
262   "cell_type": "markdown",
263   "metadata": {},
264   "source": [
265    "The in-place variants are supported as well:"
266   ]
267  },
268  {
269   "cell_type": "code",
270   "execution_count": 15,
271   "metadata": {},
272   "outputs": [
273    {
274     "name": "stdout",
275     "output_type": "stream",
276     "text": [
277      "7\n"
278     ]
279    }
280   ],
281   "source": [
282    "{\n",
283    "    auto n = 0b001_z1;\n",
284    "    n |= 0b111_z1;\n",
285    "    std::cout << n << '\\n';\n",
286    "}"
287   ]
288  },
289  {
290   "cell_type": "code",
291   "execution_count": 16,
292   "metadata": {},
293   "outputs": [
294    {
295     "name": "stdout",
296     "output_type": "stream",
297     "text": [
298      "6\n"
299     ]
300    }
301   ],
302   "source": [
303    "{\n",
304    "    int n = -0b101010;\n",
305    "    n &= 0b111_z1;\n",
306    "    std::cout << n << '\\n';\n",
307    "}"
308   ]
309  },
310  {
311   "cell_type": "code",
312   "execution_count": 17,
313   "metadata": {},
314   "outputs": [
315    {
316     "name": "stdout",
317     "output_type": "stream",
318     "text": [
319      "1\n"
320     ]
321    }
322   ],
323   "source": [
324    "{\n",
325    "    auto n = 0b001_z1;\n",
326    "    n ^= 0b111_z1;\n",
327    "    std::cout << n << '\\n';\n",
328    "}"
329   ]
330  }
331 ],
332 "metadata": {
333  "kernelspec": {
334   "display_name": "C++17",
335   "language": "C++17",
336   "name": "xcpp17"
337  },
338  "language_info": {
339   "codemirror_mode": "text/x-c++src",
340   "file_extension": ".cpp",
341   "mimetype": "text/x-c++src",
342   "name": "c++",
343   "version": "17"
344  }
345 },
346 "nbformat": 4,
347 "nbformat_minor": 4
348}
349