<?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 rhea singh on Medium]]></title>
        <description><![CDATA[Stories by rhea singh on Medium]]></description>
        <link>https://medium.com/@rheasing22?source=rss-b20b98644bd5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xJyQhEGcwb1O5rRWSS0rIQ.jpeg</url>
            <title>Stories by rhea singh on Medium</title>
            <link>https://medium.com/@rheasing22?source=rss-b20b98644bd5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:51:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rheasing22/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[Bitwise Operators in JavaScript and their Applications]]></title>
            <link>https://medium.com/@rheasing22/bitwise-operators-in-javascript-and-their-applications-b0987c7150fb?source=rss-b20b98644bd5------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0987c7150fb</guid>
            <category><![CDATA[bitwise-operator]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[application-of-bitwise]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[rhea singh]]></dc:creator>
            <pubDate>Sun, 30 May 2021 11:25:05 GMT</pubDate>
            <atom:updated>2021-05-30T11:25:05.805Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JqPcOGiBeHO-ftgVEShjug.jpeg" /></figure><p>JavaScript like many other programming languages, supports bitwise operators. These operators work on the bit level and are useful if we want to manipulate the individual bits. In JavaScript, numbers are always stored as double precision floating point numbers which is the IEEE 754 standard. However, these operators treat them as 32 bit signed binary numbers and operate on them.</p><h4><strong>Bitwise AND (&amp;)</strong></h4><p>The &amp; operator is a binary operator that performs bitwise AND on each pair of the corresponding bits. The &amp; operator returns 1 if and only if both the bits are 1 otherwise it returns 0.</p><p>Here are all the possible values of the AND operation:</p><pre>0&amp;0 = 0<br>0&amp;1 = 0<br>1&amp;0 = 0<br>1&amp;1 = 1</pre><p>For example,5 &amp;6 results in 4</p><pre>5  00000101 <br>6  00000110<br>&amp;------------<br>4  00000100</pre><p><strong>Bitwise OR (|)</strong></p><p>The | operator is a binary operator that performs bitwise OR on each of the corresponding bits. The | operator returns 1 if any of the bits is 1 and it returns 0 only if both bits are 0.</p><p>Here are all the possible values of the OR operation.</p><pre>0 | 0 = 0<br>0 | 1 = 1<br>1 | 0 = 1<br>1 | 1 = 1</pre><p>For example, 5 | 6 results in 7</p><pre>5  00000101 <br>6  00000110<br>|------------<br>7  00000111</pre><p><strong>Bitwise XOR (^)</strong></p><p>Bitwise XOR ( Exclusive -OR) is a binary operator that performs XOR on each pair of corresponding bits. If both the bits are the same it returns 0 and if the bits are different (i.e. one is 1 and the other is 0 ) it returns 1.</p><p>Here are all the possible values of the XOR operation:</p><pre>0 ^ 0 == 0<br>0 ^ 1 == 1<br>1 ^ 0 == 1<br>1 ^ 1 == 0</pre><p>For example, 5 ^6 results in 3</p><pre>5  00000101 <br>6  00000110<br>^------------<br>3  00000011</pre><p><strong>Bitwise NOT (~)</strong></p><p>The ‘~’ operator is a unary operator (one operand) which performs bitwise NOT on each of the bits. The bitwise NOT(~) inverts the bits of the operand and gives us the complement of the number</p><p>Here are all the possible values of the bitwise NOT (~) operation:</p><pre>~0 == 1</pre><pre>~1 == 0 </pre><p>For example ,~5 results in -6</p><pre>5  ==&gt;   00000000000000000000000000000101</pre><pre>~5 ==&gt; ~(00000000000000000000000000000101)</pre><pre>         11111111111111111111111111111010 ==&gt; -6</pre><p>We can check our answer using the following property :</p><pre>~a = -(a+1)<strong> </strong> </pre><pre>~5 = -(5+1) = -6</pre><p><strong>Left Shift (&lt;&lt;)</strong></p><p>The left shift operator takes 2 values. The first is the operand that we need to shift and the second is the number of bits <strong>(</strong>operand &lt;&lt;number of bits<strong> ). </strong>This operator shifts the operand to the left by the number of bits. The number of zero (0) bits shifted in from the right depends on the number of bits we are shifting to the left. Excess bits shifted to the left are discarded.</p><p>For example, 10&lt;&lt; 2 results in 40</p><pre>     10 ==&gt; 00000000000000000000000000001010</pre><pre>10 &lt;&lt; 2 ==&gt; 000000000000000000000000001010<strong>00 </strong>= 40 ( decimal )</pre><p>Bitwise shifting any number a to the left by m bits results in a*(2**m)<br>where ** is the exponentiation .</p><pre>10&lt;&lt;2 can be calculated as 10*(2**2) = 10*(4) = 40 </pre><p><strong>Sign Propagating Right Shift (&gt;&gt;)</strong></p><p>The right shift operator takes 2 values. The first is the operand that we need to shift and the second is the number of bits <strong>(</strong>operand&gt;&gt; number of bits<strong> ). </strong>It copies the sign bit(the leftmost bit) . If the sign bit is 1 ,we fill it with 1’s and if the sign bit is 0 ,we fill it with 0’s. The sign of the integer is always preserved and the excess bits that were shifted off to the right are discarded.</p><p>For example, 32&gt;&gt; 3 results in 4 and -32&gt;&gt; 3 results in -4</p><pre>32      ==&gt; 00000000000000000000000000100<strong>000</strong></pre><pre>32 &gt;&gt;3 ==&gt; <strong>000</strong>00000000000000000000000000100 --&gt; 4(base 10)</pre><pre>We shift it to the right by 3 places and add in (000)to the left side . We discarded 000 on the right.<br>Note :We added 000 as 0 was the sign bit.</pre><pre>-32      ==&gt; 11111111111111111111111111100<strong>000</strong> (in 2’s complement)</pre><pre>-32 &gt;&gt;3 ==&gt;  <strong>111</strong>11111111111111111111111111100 --&gt; -4(base 10)</pre><pre>We shift it to the right by 3 places and add in (111) to the left side. We discarded 000 on the right.<br>Note: we added 111 to the right as 1 is the sign bit </pre><p><strong>Zero-Fill Right Shift (&gt;&gt;&gt;)</strong></p><p>The bitwise unsigned right shift is very similar to the regular bitwise right shift, except that all empty bits on the left are filled with 0s. The excess bits that were shifted off to the right are discarded. This results in our sign bit to be 0 and thus we get an unsigned integer. For positive integers, both &gt;&gt; and &gt;&gt;&gt; will give us the same value.</p><p>For example, 32&gt;&gt;&gt; 3 results in 4 and -32&gt;&gt;&gt; 3 results in 536870908</p><pre>32      ==&gt; 00000000000000000000000000100<strong>000 </strong></pre><pre>32 &gt;&gt;&gt;3 ==&gt; <strong>000</strong>00000000000000000000000000100 --&gt; 4(base 10) </pre><pre>We shift it to the right by 3 places and add in (000) to the left side . We discarded 000 on the right.</pre><pre>-32      ==&gt; 11111111111111111111111111100<strong>000</strong> (in 2’s complement)</pre><pre>-32 &gt;&gt;&gt;3 ==&gt; <strong>000</strong>11111111111111111111111111100 --&gt; 536870908(base 10)</pre><pre>We shift it to the right by 3 places and add in (000) to the left side. We discarded 000 on the right.</pre><h3><strong>Applications of Bitwise Operators</strong></h3><ol><li><strong>Application of bitwise AND(&amp;)</strong></li></ol><p><strong>Check if a number is odd or even</strong></p><p>We can check if a number is odd or even by checking its least significant bit . To understand this lets take a look at even and odd numbers:</p><pre><strong>odd numbers </strong><br>5 is 0101 <br>3 is 0011 <br>==&gt; the least significant bit for both of them is 1</pre><pre><strong>even numbers </strong><br>2 is 0010 <br>4 is 0100 <br>==&gt; the least significant bit for both of them is 0</pre><p>We will observe least significant bits for odd numbers is 1 and for even numbers is 0. The &amp; operator can be used to check this as it will result in 1 if and only if both the bits are 1. If we take the input and AND it with 1, we will be able to check the least significant bit which will result in 1 if it is odd or 0 if it is even:</p><pre><strong>Even Number</strong><br>60 -&gt;  00111100 [input]<br>&amp;1 -&gt;  00000001  <br>---------------------------------------<br> 0 -&gt;  00000000 [output]</pre><pre><strong>Odd Number</strong><br>53 -&gt;  00110101 [input]<br>&amp;1 -&gt;  00000001  <br>---------------------------------------<br> 0 -&gt;  00000001 [output]</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb345c39e1ff69b62bfa204aed92ee49/href">https://medium.com/media/fb345c39e1ff69b62bfa204aed92ee49/href</a></iframe><p><strong>2. Application of bitwise OR (|)</strong></p><p><strong>Convert ASCII character from uppercase to lowercase</strong></p><p>We can convert the ascii values ‘A-Z’ to ‘a-z’ using the bitwise OR (|) operator. To understand we must remember that ‘|’ can be used to set certain bits to 1. If we look at the values below you will notice that all the binary values are same except for the 6th bit ( if we count from the right and consider the least significant bit as the 1st bit ).</p><pre>Letter    Binary       Letter    Binary<br>  a      01<strong>1</strong>00001        A      01<strong>0</strong>00001<br>  b      01<strong>1</strong>00010        B      01<strong>0</strong>00010<br>  c      01<strong>1</strong>00011        C      01<strong>0</strong>00011<br>  d      01<strong>1</strong>00100        D      01<strong>0</strong>00100<br>  e      01<strong>1</strong>00101        E      01<strong>0</strong>00101</pre><p>The 6th bit for the uppercase is 0 while the 6th bit for lowercase values is 1. All we need to do now is use ‘|’ to set the 6th bit to 1 which can be done performing bitwise OR with 00100000 (32 in decimal).</p><p>For example :Lets ‘s convert as ‘A’ to ‘a’</p><pre><strong>A </strong>01<strong>0</strong>00001 [input]<br>  00<strong>1</strong>00000 [mask ]<br>-------------------<br><strong>a</strong> 01<strong>1</strong>00001 [output]</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9535028d35057d7f5469173a031f2798/href">https://medium.com/media/9535028d35057d7f5469173a031f2798/href</a></iframe><p><strong>3. Application of XOR (^)</strong></p><p><strong>Swap numbers in-place without extra variable</strong></p><p><em>It is important to note that swapping in this manner only works for integers , it does not work for strings or even floating point numbers.</em></p><p>We can use XOR to do in-place swapping . For example lets take a = 10 and b =12. We want the result to be a =12 and b =10 which we can achieve by using the ^ operator.</p><p>first it is important to know the following properties :</p><pre>x ^ 0 = x</pre><pre>x ^ x = 0</pre><pre>x ^ y = y ^ x  ( xor is commutative)</pre><pre>x ^ (y ^ z) = (x ^ y)^z ( xor is associative )</pre><ol><li>a = a ^ b</li><li>b = a ^ b. From step 1 we know that a is a ^ b and we can then substitute it into b = a ^ b. Thus <strong>b = (a ^ b) ^ b = a ^ (b ^ b) = a ^ 0 = a</strong>. Now b contains the value of a.</li><li>a = a ^ b. From step 1 , we know that a is a ^ b and from step 2 we know that b contains the value of a. Thus <strong>a = (a ^ b) ^ a = b ^ (a ^ a) = b ^ 0 = b. </strong>Finally, a will contain the value of b.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38c3cb4b81122a3175f0c363f88d1c6a/href">https://medium.com/media/38c3cb4b81122a3175f0c363f88d1c6a/href</a></iframe><p><strong>4.</strong> <strong>Application of Bitwise NOT Not (~)</strong></p><p><strong>Check of presence/absence of a substring in another string</strong></p><p>Using the ‘~’ before indexOf() expression results in a truthy/falsy value instead of numeric index. We can do this when we don’t want the value of the index and just want to check if something is present or not.</p><pre>~someString.indexOf(something)</pre><p>If the index returned is -1, we perform ~(-1) which will result in 0 which is a falsy value. All other values other than -1 , will result in truthy values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6b0d2924497b3b476e65dae4880006b/href">https://medium.com/media/e6b0d2924497b3b476e65dae4880006b/href</a></iframe><p><em>Note : Instead of if(~x.indexOf(substring)) we could have done if(x.indexOf(substring) &gt;= 0) as both ways are correct. However, I was trying to use only bitwise operators.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0987c7150fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>