<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anton Kalinin on Medium]]></title>
        <description><![CDATA[Stories by Anton Kalinin on Medium]]></description>
        <link>https://medium.com/@evil.bot3000?source=rss-afebf19d36e7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*eb6zzV8H_yZI8YfN.jpg</url>
            <title>Stories by Anton Kalinin on Medium</title>
            <link>https://medium.com/@evil.bot3000?source=rss-afebf19d36e7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 16:18:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@evil.bot3000/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An Unconventional Way of Using Bitwise Operators in JavaScript with React.]]></title>
            <link>https://medium.com/@evil.bot3000/why-the-heck-do-you-need-bitwise-operations-in-javascript-52e1775df6f2?source=rss-afebf19d36e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/52e1775df6f2</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Anton Kalinin]]></dc:creator>
            <pubDate>Wed, 12 Jun 2019 01:44:08 GMT</pubDate>
            <atom:updated>2020-03-27T11:15:55.288Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*q4aOaa4phLuNSqIW" /><figcaption>Photo by <a href="https://unsplash.com/@mattartz?utm_source=medium&amp;utm_medium=referral">Matt Artz</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article I’m going to show you a small experiment and my approach for a practical usage of the bitwise operations and bit masks in JavaScript. I’m going to use React in order to create list of checkboxes and three buttons: “Clear all”, “Select all” and “Invert all”. Here is the final result: <a href="https://codepen.io/ssh/pen/LKPgBK">conventional approach</a> and the <a href="https://codepen.io/ssh/pen/xoKmJp">approach</a> which I will describe in the article.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b935e7872b5958cde60885f5d9c49da/href">https://medium.com/media/7b935e7872b5958cde60885f5d9c49da/href</a></iframe><p>We are going to use zero as an initial value for our state, to store checkbox values.</p><pre>const [selected, setSelected] = React.useState(0);</pre><p>But to store all values into a number we have to use a binary representation of this number, which size is limited to 32 bits in JavaScript.</p><pre>In JavaScript all numbers are <a href="https://en.wikipedia.org/wiki/IEEE_754">floating-point numbers</a> according to the <a href="https://www.ecma-international.org/ecma-262/5.1/#sec-8.5">ECMA 5.1</a> and all bitwise operators are limited to work with 32 bits.</pre><p>So you can think about this state as an array of Boolean values, but instead of true and false we are going to use 1 and 0.</p><pre>[true, false, false, true] === [1, 0, 0, 1]</pre><p>For the data, we are using an array of objects, which shape looks like this: { name: &#39;Mr.Peanutbutter&#39;, id: 2 } As you may notice, there is a kind of magic happening to calculate ID: 1 &lt;&lt; index This is how it might look for the first 4 indexes.</p><pre>1         0001    (same as 1&lt;&lt;0)<br>1&lt;&lt;1      0010<br>1&lt;&lt;2      0100<br>1&lt;&lt;3      1000</pre><p>What we do is shifting bits, we need that in order to check that our item is set to “true” in the state.</p><p>And after we’ve created our state and did set IDs for our items we can do a lot of cool stuff which is a Boolean algebra provides us 😅</p><ul><li>Inverting all bits: -1 or ~0</li><li>Toggling a bit at nth position: selected ^ item.id</li><li>Checking if a bit at nth position is set/unset: selected &amp; item.id</li></ul><p><strong>Advantages:</strong></p><ul><li>This is an elegant solution which simpler and shorter than manipulation of array of objects in my opinion.</li><li>This is supposed to be faster than a conventional solution. This depends on a browser engine, of course.</li><li>You can save and restore values for the list of checkboxes pretty easily.</li></ul><p><strong>Disadvantages:</strong></p><ul><li>Colleagues of yours probably will be stunted for a quite some time after seeing this 😱</li><li>In JavaScript, you are limited to 32 bits, which is not good, not terrible, but could be enough in most of the cases, and don’t forget that <a href="https://github.com/tc39/proposal-bigint">BigInt</a> is coming!</li></ul><p>I’m not calling you to drop everything and rewrite all your projects using bitwise operators and Boolean algebra. This is just a fun experiment to do and an attempt to think out of the box. Cheers! 🎉</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fw5FTwwiweGqDm%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fw5FTwwiweGqDm%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fw5FTwwiweGqDm%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="338" frameborder="0" scrolling="no"><a href="https://medium.com/media/916c3fa7a5edba977de23e7420453912/href">https://medium.com/media/916c3fa7a5edba977de23e7420453912/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52e1775df6f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>