{"id":259,"date":"2024-03-09T00:42:10","date_gmt":"2024-03-09T00:42:10","guid":{"rendered":"https:\/\/offensivepython.com\/?p=259"},"modified":"2024-03-15T02:01:26","modified_gmt":"2024-03-15T02:01:26","slug":"bitwise-operators","status":"publish","type":"post","link":"https:\/\/offensivepython.com\/index.php\/2024\/03\/09\/bitwise-operators\/","title":{"rendered":"Bitwise Operators with f-strings and format()"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"259\" class=\"elementor elementor-259\">\n\t\t\t\t<div class=\"elementor-element elementor-element-3934bec4 e-flex e-con-boxed e-con e-parent\" data-id=\"3934bec4\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-48b08f7d elementor-widget elementor-widget-text-editor\" data-id=\"48b08f7d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><\/p>\n<div>\u00a0 \u00a0 \u00a0Since Python 3.6, we can use f-strings to evaluate expressions inside string literals and easily print out the results. This works great for printing Bitwise Operations. If we prefer, we can also use the older method of using the format function. Below are some examples of how this can be done.<\/div>\n<div>\u00a0<\/div>\n<div><span style=\"text-decoration: underline;\">Example code:<\/span><\/div>\n<div><br \/><strong>bin_1 = 0b11101011<\/strong><br \/><strong>bin_2 = 0b10001111<\/strong><br \/><br \/><strong>print(f&#8221;{bin_1:08b}&#8221;)<\/strong><br \/><strong>print(f&#8221;{bin_2:08b}&#8221;)<\/strong><br \/><strong>print(&#8220;&#8212;&#8212;&#8211;&#8220;)<\/strong><br \/><br \/><strong>print(f&#8221;{(bin_1 &amp; bin_2):08b} is the AND result using an f-string.&#8221;)<\/strong><br \/><strong>print(format(bin_1 &amp; bin_2, &#8220;08b&#8221;), &#8220;is the AND result using the format function.\\n&#8221;)<\/strong><br \/><br \/><strong>print(f&#8221;{(bin_1 | bin_2):08b} is the OR result using an f-string.&#8221;)<\/strong><br \/><strong>print(format(bin_1 | bin_2, &#8220;08b&#8221;), &#8220;is the OR result using the format function.\\n&#8221;)<\/strong><br \/><br \/><strong>print(f&#8221;{(bin_1 ^ bin_2):08b} is the XOR result using an f-string.&#8221;)<\/strong><br \/><strong>print(format(bin_1 ^ bin_2, &#8220;08b&#8221;), &#8220;is the XOR result using the format function.\\n&#8221;)<\/strong><br \/><br \/># Notice that 10 bits are printed; the mask of 08b prints 8 bits at a minimum with leading zeros<br \/><strong>print(f&#8221;{(bin_1 &lt;&lt; 2):08b} is the SHIFT LEFT 2 bits result for bin_1 using an f-string.&#8221;)<\/strong><br \/><strong>print(format(bin_1 &lt;&lt; 2, &#8220;08b&#8221;), &#8220;is the SHIFT LEFT 2 bits result for bin_1 using the format function.\\n&#8221;)<\/strong><br \/><br \/><strong>print(f&#8221;{(bin_2 &gt;&gt; 2):08b} is the SHIFT RIGHT 2 bits result for bin_2 using an f-string.&#8221;)<\/strong><br \/><strong>print(format(bin_2 &gt;&gt; 2, &#8220;08b&#8221;), &#8220;is the SHIFT RIGHT 2 bits result for bin_2 using the format function.\\n&#8221;)<\/strong><br \/><br \/><strong>print(f&#8221;{~bin_1:08b} is the 2&#8217;s COMPLIMENT result of bin_1 using an f-string.&#8221;)<\/strong><br \/><strong>print(format(~bin_1, &#8220;08b&#8221;), &#8220;is the 2&#8217;s COMPLIMENT result of bin_1 using the format function.&#8221;)<\/strong><\/div>\n<p><span style=\"text-decoration: underline;\">Output:<\/span><\/p>\n<p>11101011<br \/>10001111<br \/>&#8212;&#8212;&#8211;<br \/>10001011 is the AND result using an f-string.<br \/>10001011 is the AND result using the format function.<\/p>\n<p>11101111 is the OR result using an f-string.<br \/>11101111 is the OR result using the format function.<\/p>\n<p>01100100 is the XOR result using an f-string.<br \/>01100100 is the XOR result using the format function.<\/p>\n<p>1110101100 is the SHIFT LEFT 2 bits result for bin_1 using an f-string.<br \/>1110101100 is the SHIFT LEFT 2 bits result for bin_1 using the format function.<\/p>\n<p>00100011 is the SHIFT RIGHT 2 bits result for bin_2 using an f-string.<br \/>00100011 is the SHIFT RIGHT 2 bits result for bin_2 using the format function.<\/p>\n<p>-11101100 is the 2&#8217;s COMPLIMENT result of bin_1 using an f-string.<br \/>-11101100 is the 2&#8217;s COMPLIMENT result of bin_1 using the format function.<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 \u00a0Since Python 3.6, we can use f-strings to evaluate expressions inside string literals and easily print out the results. This works great for printing Bitwise Operations. If we prefer, we can also use the older method of using the format function. Below are some examples of how this can be done. \u00a0 Example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[19],"tags":[20,21],"class_list":["post-259","post","type-post","status-publish","format-standard","hentry","category-binary-and-hexadecimal","tag-f-strings","tag-format"],"_links":{"self":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/comments?post=259"}],"version-history":[{"count":13,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":337,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/259\/revisions\/337"}],"wp:attachment":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}