<?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 Abdulrahman Gamal on Medium]]></title>
        <description><![CDATA[Stories by Abdulrahman Gamal on Medium]]></description>
        <link>https://medium.com/@abdulrahman-gamal?source=rss-3d99aea29cec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mUbbP5Ho7NjgL6cZeGkNUw.jpeg</url>
            <title>Stories by Abdulrahman Gamal on Medium</title>
            <link>https://medium.com/@abdulrahman-gamal?source=rss-3d99aea29cec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 16:00:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdulrahman-gamal/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[Bit Manipulation Notes]]></title>
            <link>https://abdulrahman-gamal.medium.com/bit-manipulation-notes-bd01efec8eca?source=rss-3d99aea29cec------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd01efec8eca</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[bit-manipulation]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[study-notes]]></category>
            <dc:creator><![CDATA[Abdulrahman Gamal]]></dc:creator>
            <pubDate>Thu, 20 May 2021 23:51:31 GMT</pubDate>
            <atom:updated>2021-05-20T23:51:31.449Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*rAnjB3VvWCNl0LctslJK8A.png" /></figure><blockquote><strong>1 — Shifting</strong></blockquote><p>Left shift:- move everything to the left</p><p>example:- Binary representation 01010 → 10100</p><p>Decimal representation 10 → 20</p><p>How to apply in code?</p><pre>int x = 10;<br>x&lt;&lt;1;</pre><p>Right shift:- move everything to the right</p><p>example:- Binary representation 01010 → 00101</p><p>Decimal representation 10 → 5</p><p>How to apply in code?</p><pre>int x = 10;<br>x&gt;&gt;1;</pre><p>Notice:-</p><p>Left shifting is multiplying the N number decimal by 2</p><p>Right shifting is Divide the N number by 2</p><blockquote><strong>2 — Shifting With Sign ( Arithmetic Shifting )</strong></blockquote><p>As we may know that signed integers are having left bit as 1 as a minus sign</p><p>We can convert the number to negative by Tow’s complement.</p><p>So What is Two’s Complement (very simple steps )</p><p>1- inverse</p><p>2- add one</p><p>Ex:-</p><p>18 == 00010010</p><p>Applying 1- → 11101101</p><p>Applying 2- → 00000001</p><p>Result → 11101110</p><p>So Decimal from signed 2’s complement:-</p><p>-18 → 11101110</p><p>Back to our topic ( Shifting ):-</p><p>Negative has 1 on the left</p><p>1- shift all bits normally</p><p>2- replace the last bit with one</p><p>Example:-</p><p>-23 == 11101001</p><p>1-Shifting to right → 01110100</p><p>2- add sign → 11110100</p><blockquote><strong>3 — Bitwise Operators</strong></blockquote><p>Bitwise operators are good for saving space and sometimes to cleverly remove dependencies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/0*2luhanw4y3rd2-rs" /></figure><p>check if a given number is a power of 2</p><p>Consider a number N and you need to find if N is a power of 2.</p><ul><li>Now think about the binary representation of (x-1).</li><li>(x-1) can be obtained by simply flipping all the bits to the right of 1</li></ul><p>Ex : 16 = 10000–15 = 1111</p><p>10000 &amp; 01111 → 00000</p><p>So the Rule is the number is power of 2 if ( x &amp; (x-1) ) = 0</p><p>To make sure let’s take a look of power 2 binaries</p><p>2, 4, 8, 16 ,32</p><p>10 , 100, 1000 ,10000 ,100000</p><p>Replace 1 with zero and flip zeros we easily get ( X -1 )</p><pre>!(x &amp; (x-1) ==!0 == true</pre><p>Simple Condition we can check if is the power of 2 or not without needing to loop and make O(logN).time now its constant time:-</p><pre>bool isPowerOfTwoUsingBitWise(int x) {<br>    // x will check if x == 0 and !(x &amp; (x - 1)) will check if x is a power of 2 or not<br>    return (x &amp;&amp; !(x &amp; (x - 1)));<br>}</pre><p>let’s find more bit manipulation problems here</p><p><a href="https://leetcode.com/tag/bit-manipulation/">https://leetcode.com/tag/bit-manipulation/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd01efec8eca" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>