{"id":557,"date":"2007-07-28T08:43:48","date_gmt":"2007-07-28T15:43:48","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/2007\/07\/28\/python-reference-binary-operators\/"},"modified":"2007-07-28T08:43:48","modified_gmt":"2007-07-28T15:43:48","slug":"python-reference-binary-operators","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/python-reference-binary-operators\/","title":{"rendered":"Python Reference: Binary Operators"},"content":{"rendered":"<p>Python binary operators are pretty much the same as in any other language, however I notice most programmers tend to waste a lot of memory by creating lots and lots of properties say in DB tables, or Objects and using the wrong datatypes. I think its elegant to use the concept of binary flags, for example, if you have an object that has around 8 or 16 boolean properties, you can store their state in 1 byte or 2 bytes (1 char or 2 char fields), and turn on\/off the bits on those fields.<\/p>\n<p>Usually, if you have a binary field, and you want to, turn on bits, toggle bits, and check bits, you do the following.<\/p>\n<p>Suppose &#8220;config&#8221; is an 8 bit number, and you want to modify this bits individually, say config=4 (0b100) and you want to turn on the rightmost bit to have 0b101 (which is a 5) you could do the following<\/p>\n<pre>\n>>> #Uses the | operator\n... def turnBitOn(config, binaryFlag):\n...   return config | binaryFlag\n... \n>>> config = 4\n>>> config = turnBitOn(config,1)\n>>> print config\n5\n<\/pre>\n<p>If you want to check if a Bit is turned on, just use the &#8220;&#038;&#8221; operator, if the result is the same as the bit you&#8217;re comparing, then its turned on.<\/p>\n<pre>\n>>> def checkBit(config,binaryFlag):\n...   return binaryFlag == config & binaryFlag\n... \n>>> print checkBit(config,1)\nTrue\n>>> print checkBit(config,2) #checks 0b010 in 0b101\nFalse\n<\/pre>\n<p>What if you just want to toggle a bit, no matter what&#8217;s in there?<br \/>\nJust use binary XOR, the &#8220;^&#8221; operator.<\/p>\n<pre>\n>>> def toggleBit(config,binaryFlag):\n...   return config ^ binaryFlag\n... \n>>> print config\n5\n>>> config = toggleBit(config,1)\n>>> print config\n4\n>>> config = toggleBit(config,1)\n>>> print config\n5\n<\/pre>\n<p>And now, the last basic operation would be to turn off a bit. For this, you should do a NAND operation. The Binary Not in python as in most programming languages is the &#8220;~&#8221; operator. This is how you can use it to turn off bits.<\/p>\n<p>You have to do it in conjunction with the &#038; operator, sort of doing a NAND<\/p>\n<pre>\n>>> 5 & (~1)\n4\n>>> 5 &~ 1\n4\n<\/pre>\n<p>So we could define our turnOffBit function as follows:<\/p>\n<pre>\n>>> def turnOffBit(config,binaryFlag):\n...   return config & (~binaryFlag)\n... \n>>> print config\n5\n>>> config = turnOffBit(config,1)\n>>> print config\n4\n>>> config = turnOffBit(config,1)\n>>> print config\n4\n<\/pre>\n<p>Hope this makes a good reference for those trying to make the most out of their bytes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python binary operators are pretty much the same as in any other language, however I notice most programmers tend to waste a lot of memory by creating lots and lots of properties say in DB tables, or Objects and using the wrong datatypes. I think its elegant to use the concept of binary flags, for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,30,43,65],"tags":[],"class_list":["post-557","post","type-post","status-publish","format-standard","hentry","category-code","category-geeklife","category-linux","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-8Z","jetpack-related-posts":[{"id":1526,"url":"https:\/\/www.gubatron.com\/blog\/droid-vs-nexus-1-sunspider-javascript-benchmark-showdown\/","url_meta":{"origin":557,"position":0},"title":"Droid vs Nexus 1 &#8211; SunSpider JavaScript Benchmark showdown","author":"gubatron","date":"January 10, 2010","format":false,"excerpt":"Nexus One runs JavaScript 2.32 times faster than the Motorola Droid. The SunSpider JavaScript Benchmark was run several times on both phones and results were consistent. It'd be great to compare with the iPhone 3Gs, If someone has an iPhone 3Gs, please run the test and send me the results\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":747,"url":"https:\/\/www.gubatron.com\/blog\/safari-31-benchmark-part-ii-vs-firefox-30b4\/","url_meta":{"origin":557,"position":1},"title":"Safari 3.1 Benchmark part II &#8211; VS Firefox 3.0b4","author":"gubatron","date":"March 20, 2008","format":false,"excerpt":"This is the Part II of Benchmarks between Safari 3.1's JavaScript engine and Firefox. Last Benchmark was done against Firefox 2, and Safari destroyed Firefox's Javascript engine, in some aspects being up to 7 times faster. So I was curious and I downloaded and tried the benchmark on Firefox 3.0b4.\u2026","rel":"","context":"In &quot;AJAX&quot;","block_context":{"text":"AJAX","link":"https:\/\/www.gubatron.com\/blog\/category\/ajax\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3616,"url":"https:\/\/www.gubatron.com\/blog\/codejava-argb_8888-pixel-abstraction\/","url_meta":{"origin":557,"position":2},"title":"[CODE\/JAVA] ARGB_8888 Pixel Abstraction","author":"gubatron","date":"December 4, 2016","format":false,"excerpt":"This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels. ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8's stand for the number of bits per channel. In Android, signed int's are used to represent pixel's alpha\/color information. Since Android's\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2749,"url":"https:\/\/www.gubatron.com\/blog\/deploying-html5-apps-on-cloudfront-with-efficient-invalidation-requests\/","url_meta":{"origin":557,"position":3},"title":"Deploying HTML5 apps on CloudFront with efficient invalidation requests","author":"gubatron","date":"June 8, 2012","format":false,"excerpt":"So you decided to build your next web app\/site using nothing but HTML5 and Javascript. No server side processing for anything related to UI. This means you will be coding a lot of JavaScript. Wouldn't it be nice to put all that static HTML and JS on your CloudFront CDN\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":744,"url":"https:\/\/www.gubatron.com\/blog\/new-safaris-javascript-engine-kicks-ass\/","url_meta":{"origin":557,"position":4},"title":"New Safari&#8217;s JavaScript engine Kicks Ass!","author":"gubatron","date":"March 19, 2008","format":false,"excerpt":"So I downloaded yesterday the latest Software Update for Mac OSX and it included an update of the Safari Web Browser, which I had taken for dead ages ago, I'm a hardcore Firefox user. Today I read about the new updates, and I read something that caught my eye at\u2026","rel":"","context":"In &quot;AJAX&quot;","block_context":{"text":"AJAX","link":"https:\/\/www.gubatron.com\/blog\/category\/ajax\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":788,"url":"https:\/\/www.gubatron.com\/blog\/blooploader-06-is-hardy-compatible\/","url_meta":{"origin":557,"position":5},"title":"Blooploader 0.6 is Hardy compatible","author":"gubatron","date":"April 26, 2008","format":false,"excerpt":"Blooploader 0.6 running on Hardy. Currently available only via subversion. For our Linux users, you can safely update to Ubuntu Hardy if the one thing holding your breath was compatibility with the Blooploader. Currently we run the Blooploader in Linux from source, you just need to have installed, Qt4, sip4,\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=557"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/557\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}