{"id":5767,"date":"2022-02-02T18:31:00","date_gmt":"2022-02-02T17:31:00","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5767"},"modified":"2024-06-18T14:20:59","modified_gmt":"2024-06-18T12:20:59","slug":"logic-and-bit-operations","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/02\/02\/logic-and-bit-operations\/","title":{"rendered":"Logic and bit operations"},"content":{"rendered":"<div class=\"small-12 large-6 columns\">\n<p>1. Python supports the following logical operators:<\/p>\n<p><!--more--><\/p>\n<ul>\n<li><code>and<\/code> \u2192 if both operands are true, the condition is true, e.g., <code>(True and True)<\/code> is <code>True<\/code>,<\/li>\n<li><code>or<\/code> \u2192 if any of the operands are true, the condition is true, e.g., <code>(True or False)<\/code> is <code>True<\/code>,<\/li>\n<li><code>not<\/code> \u2192 returns false if the result is true, and returns true if the result is false, e.g., <code>not True<\/code> is <code>False<\/code>.<\/li>\n<\/ul>\n<p>2. You can use bitwise operators to manipulate single bits of data. The following sample data:<\/p>\n<ul>\n<li><code>x = 15<\/code>, which is <code>0000 1111<\/code> in binary,<\/li>\n<li><code>y = 16<\/code>, which is <code>0001 0000<\/code> in binary.<\/li>\n<\/ul>\n<p>will be used to illustrate the meaning of bitwise operators in Python. Analyze the examples below:<\/p>\n<ul>\n<li><code>&amp;<\/code> does a <i>bitwise and<\/i>, e.g., <code>x &amp; y = 0<\/code>, which is <code>0000 0000<\/code> in binary,<\/li>\n<li><code>|<\/code> does a <i>bitwise or<\/i>, e.g., <code>x | y = 31<\/code>, which is <code>0001 1111<\/code> in binary,<\/li>\n<li><code>\u02dc <\/code> does a <i>bitwise not<\/i>, e.g., <code>\u02dc x = 240<\/code>*, which is <code>1111 0000<\/code> in binary,<\/li>\n<li><code>^<\/code> does a <i>bitwise xor<\/i>, e.g., <code>x ^ y = 31<\/code>, which is <code>0001 1111<\/code> in binary,<\/li>\n<li><code>&gt;&gt;<\/code> does a <i>bitwise right shift<\/i>, e.g., <code>y &gt;&gt; 1 = 8<\/code>, which is <code>0000 1000<\/code> in binary,<\/li>\n<li><code>&lt;&lt;<\/code> does a <i>bitwise left shift<\/i>, e.g., <code>y &lt;&lt; 3 = <\/code>, which is <code>1000 0000<\/code> in binary,<\/li>\n<\/ul>\n<p>* <code>-16<\/code> (decimal from signed 2&#8217;s complement) &#8212; read more about the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Two%27s_complement\" target=\"_blank\" rel=\"noopener\">Two&#8217;s complement<\/a> operation.<\/p>\n<\/div>\n<div class=\"small-12 large-6 columns\">\n<p>&nbsp;<\/p>\n<p><strong>Exercise 1<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true\">x = 1\r\ny = 0\r\nz = ((x == y) and (x == y)) or not(x == y)\r\n\r\nprint(not(z))<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p id=\"sol\"><code class=\"codep \">False<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 2<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">x = 4\r\ny = 1\r\n\r\na = x &amp; y\r\nb = x | y\r\nc = ~x # tricky!\r\nd = x ^ 5\r\ne = x &gt;&gt; 2\r\nf = x &lt;&lt; 2\r\n\r\nprint(a, b, c, d, e, f)<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol2\"><code class=\"codep \">0 5 -5 1 1 16<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 3<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">print((3, 1) in {1: 'a', 'b': 2, (3, 1): 10}, end = \" \")\r\nprint(3 not in {1: 'a', 'b': 2, (3, 1): 10}, end = \" \")\r\nprint('a' in {1: 'a', 'b': 2, (3, 1): 10}, end = \" \")\r\nprint('a' in {1: 'a', 'b': 2, (3, 1): 10}.values(), end = \" \")<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>True True False True<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>True False False False<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>False False True True<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>True False False True<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>True True False True<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The <code><strong>in<\/strong><\/code> and <code><strong>not in<\/strong><\/code> operators check whether a given <strong>key<\/strong> is present in a dictionary or not.<\/li>\n<li>In <strong>line 1<\/strong>, the <strong>key<\/strong> <code><strong>(3, 1)<\/strong><\/code> is a <strong>tuple<\/strong> and is present in the dictionary. Therefore, the <code><strong>print<\/strong><\/code> statement sends <code><strong>True<\/strong><\/code> to the screen.<\/li>\n<li>In <strong>line 2<\/strong>, number <strong>3<\/strong> is not a <strong>key<\/strong>, and the <code><strong>not in<\/strong><\/code> operator returns <code><strong>True<\/strong><\/code>.<\/li>\n<li>In the dictionary, <code><strong>'a'<\/strong><\/code> is a <strong>value<\/strong> and not a <strong>key<\/strong>. So, <strong>line 3<\/strong> of the code prints <code><strong>False<\/strong><\/code>.<\/li>\n<li>In <strong>line 4<\/strong>, the code again checks if <code><strong>'a'<\/strong><\/code> is in the dictionary. But in this case, the <code><strong>in<\/strong><\/code> operator does not look for <code><strong>'a'<\/strong><\/code> in the dictionary <strong>keys<\/strong> but among the <strong>values <\/strong>returned by the <code><strong>values()<\/strong><\/code> method. So, <strong>line 4 <\/strong>prints <code><strong>True<\/strong><\/code>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 4<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">a = 3\r\nb = 6\r\n\r\nx = 2 * a &gt;= b == (b % a != 0)\r\n\r\nprint(x)<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>0<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>True<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>1<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>False<\/code><\/p>\n<div><\/div>\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>False<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<p>&nbsp;<\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li><strong>Comparison<\/strong> operators perform a comparison between two values and return <code><strong>True<\/strong><\/code> or <code><strong>False<\/strong><\/code>.<\/li>\n<\/ul>\n<ul>\n<li>The code in the example uses the operators <strong>greater than<\/strong> <strong>or<\/strong> <strong>equal to<\/strong> (<code><strong>&gt;=<\/strong><\/code>), <strong>equal<\/strong> (<code><strong>==<\/strong><\/code>) and not equal <code>(<strong>!=<\/strong><\/code>).<\/li>\n<\/ul>\n<ul>\n<li>Expressions in parentheses always have higher evaluation precedence, and comparison operators have lower precedence than arithmetic ones.<\/li>\n<\/ul>\n<ul>\n<li>The <strong>modulo<\/strong> operator returns the remainder of a division.<\/li>\n<\/ul>\n<ul>\n<li>Because of the above, <strong>line 4<\/strong> of the code works as follows:<strong>step by step\u00a0 <\/strong>\n<p>Step 1 &#8211;\u00a0 <code><strong>x = 2 * a &gt;= b == (b % a != 0)<\/strong><\/code><\/p>\n<p>Step 2 &#8211; <code><strong>x = 2 * 3 &gt;= 6 == (6 % 3 != 0)<\/strong><\/code><\/p>\n<p>Step 3 &#8211; <code><strong>x = 2 * 3 &gt;= 6 == (0 != 0)<\/strong><\/code><\/p>\n<p>Step 4 &#8211; <code><strong>x = 2 * 3 &gt;= 6 == False<\/strong><\/code><\/p>\n<p>Step 5 &#8211; <code><strong>x = 6 &gt;= 6 == False<\/strong><\/code><\/p>\n<p>Step 6 &#8211; <code><strong>x = True == False<\/strong><\/code><\/p>\n<p>Result &#8211; <code><strong>x = False<\/strong><\/code><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Python supports the following logical operators:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5767"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5767"}],"version-history":[{"count":9,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5767\/revisions"}],"predecessor-version":[{"id":5919,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5767\/revisions\/5919"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5767"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}