<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mahmoud Galal</title>
    <description>The latest articles on DEV Community by Mahmoud Galal (@mahmoudgalal).</description>
    <link>https://dev.to/mahmoudgalal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F765371%2Fa74051c1-fe0b-4a3c-843b-cd190b0b6411.png</url>
      <title>DEV Community: Mahmoud Galal</title>
      <link>https://dev.to/mahmoudgalal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahmoudgalal"/>
    <language>en</language>
    <item>
      <title>Why you Should join Hackathons</title>
      <dc:creator>Mahmoud Galal</dc:creator>
      <pubDate>Sat, 11 Feb 2023 19:08:36 +0000</pubDate>
      <link>https://dev.to/mahmoudgalal/why-you-should-join-hackathons-21k8</link>
      <guid>https://dev.to/mahmoudgalal/why-you-should-join-hackathons-21k8</guid>
      <description>&lt;p&gt;MLH is a platform where you could join their programs and Hackathons &lt;/p&gt;

&lt;p&gt;They have launched the Global Hack Week 6 days ago&lt;/p&gt;

&lt;p&gt;And its about AI/ML, and It has a lot of sessions and challenges &lt;/p&gt;

&lt;p&gt;There's tons of hackathons per year and you definitely should join them&lt;/p&gt;

&lt;h2&gt;
  
  
  Why you should participate
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Swags&lt;/li&gt;
&lt;li&gt;Prizes&lt;/li&gt;
&lt;li&gt;Learning new things will boost your career&lt;/li&gt;
&lt;li&gt;connections
All these benefits come together in a one package just by joining a Hackathon&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They will add A lot to you and your knowledge&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learnt in AI/ML week ?
&lt;/h2&gt;

&lt;p&gt;I built many projects with different categories&lt;br&gt;
I learnt how to use NLP and speech to text algorithms&lt;br&gt;
I explored how to clean Data &lt;/p&gt;

&lt;p&gt;even all of that was not part of my career but they opened a lot of doors to new Ideas &lt;/p&gt;

&lt;p&gt;I'm now ranked the top 7% of the total hackers, and I hope to be the 1% &lt;/p&gt;

&lt;p&gt;and You should definitely check their website all the time&lt;br&gt;
&lt;a href="https://mlh.io" rel="noopener noreferrer"&gt;MLH&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and you should register for the coming hacks &lt;br&gt;
&lt;a href="https://ghw.mlh.io/events/social-good-week" rel="noopener noreferrer"&gt;MLH-Hack&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Happy Hacking
&lt;/h1&gt;

</description>
      <category>chatgpt</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Puppeteer: Crawling data for newbies</title>
      <dc:creator>Mahmoud Galal</dc:creator>
      <pubDate>Thu, 15 Sep 2022 14:04:05 +0000</pubDate>
      <link>https://dev.to/mahmoudgalal/puppeteer-crawling-data-for-newbies-3bik</link>
      <guid>https://dev.to/mahmoudgalal/puppeteer-crawling-data-for-newbies-3bik</guid>
      <description>&lt;p&gt;Puppeteer is a NodeJs library which provide a High-level API to Chrome over DevToolsProtocol &lt;/p&gt;




&lt;h2&gt;
  
  
  It's do a lot
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Crawl data from webapps&lt;/li&gt;
&lt;li&gt;Generate a screenshots and PDFs&lt;/li&gt;
&lt;li&gt;Create an up-to-date, automated testing environment. Run your tests directly in the latest version of Chrome using the latest JavaScript and browser features.&lt;/li&gt;
&lt;li&gt;Automate form submission, UI testing, keyboard input, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Let's do a tutorial about Automation and Crawling
&lt;/h3&gt;

&lt;h2&gt;
  
  
  Puppeter setup
&lt;/h2&gt;

&lt;p&gt;Is common to know how the structure look to run any lib&lt;br&gt;
so Puppeter rely on promises&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const puppeteer = require('puppeteer');

(async () =&amp;gt; {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we call puppeteer.launch() method to open a browser for us&lt;br&gt;
then asking it to open a page which we will do all stuff there.&lt;br&gt;
and goto() to navigate to a website&lt;br&gt;
all methods that we'll use is provide to the page object&lt;/p&gt;
&lt;h2&gt;
  
  
  Crawling data
&lt;/h2&gt;

&lt;p&gt;if we want to crawl data, we need to know&lt;br&gt;
what we deal with, So we want to know that we select using the id's or tags or even attributes we deal with DOM&lt;/p&gt;

&lt;p&gt;so let's make the environment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir book-scraper
cd book-scraper
npm init -y
npm install --save puppeteer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  setup the browser instance
&lt;/h2&gt;

&lt;p&gt;When you open browser you can interact with it like doing clicks and type etc. Headless chrome will do the same thing, but programmatically.&lt;/p&gt;

&lt;p&gt;we will need &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;four .js file &lt;br&gt;
browser.js&lt;br&gt;
main.js&lt;br&gt;
pageScraper.js&lt;br&gt;
PageController.js&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;let's start with browser.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pupt= require('puppeteer');

async function startBrowser(){
    let browser
    try{
        browser = await pupt.launch({
            headless:false, // defualt
            'ignoreHTTPSErrors':true
        })
    }
    catch(err){
        console.error(err)
    }

    return browser;
}

module.exports = {
    startBrowser
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Making the headless:false that's mean you will see the interface while your script is running to see the process&lt;/p&gt;

&lt;p&gt;let's do the &lt;code&gt;main.js&lt;/code&gt; file to run our browser instance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const browser= require('./bowser')
const scraperController= require('./pageController'); // we will make it later

let browserInstance = browser.startBrowser();

// we want to to controle this browser, so we will do a function to do that
// so we have to pass it there

scraperController(browserInstance);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's do the 'pageController.js' file to see how to interact with our browser&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pageScraper= require('./pageScraper');

async function scrapeAll(browserInstance){
    let browser;

    try{
        browser=await browserInstance;
        await pageScraper.scraper(browser);
    }
    catch(err){
        console.error("There's a problem with browser Instance =&amp;gt; ", err)
    }
}

module.exports=(browserInstance) =&amp;gt; scrapeAll(browserInstance)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we export the method to take the browserInstance as argument&lt;/p&gt;

&lt;p&gt;let's make the &lt;code&gt;pageScraper.js&lt;/code&gt; file to scrape the data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const scrapeObject={
    url:'http://books.toscrape.com',
    async scraper(browser){
        let page= await browser.newPage();
        console.log(`navigating to ${this.url}..`)
        await page.goto(this.url);
    }
}
module.exports=scrapeObject
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  This the basic structure to build your web crawler
&lt;/h3&gt;

&lt;p&gt;you can clone it through github &lt;a href="https://github.com/Mahmoudgalalz/pupt" rel="noopener noreferrer"&gt;pupt starter&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Scrape the data from a page
&lt;/h2&gt;

&lt;p&gt;First, if you inspect the source code for the homepage using the Dev Tools inside your browser, you will notice that the page lists each book’s data under a section tag. Inside the section tag every book is under a list (li) tag, and it is here that you find the link to the book’s dedicated page, the price, and the in-stock availability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiklf44yaj1glxqz0az9k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiklf44yaj1glxqz0az9k.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let's edit our files, reopen &lt;code&gt;pageScraper.js&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const scrapeObject={
    url:'http://books.toscrape.com',
    async scraper(browser){
        let page= await browser.newPage();
        console.log(`navigating to ${this.url}..`)
        await page.goto(this.url);

        // wait fot the DOM to render
        await page.waitForSelector('.page_inner')
        //Get the link to all selected books
        let urls= await page.$$eval('section ol &amp;gt; li',links =&amp;gt;{
            // Make sure the book is in stock 
            links=links.filter(link =&amp;gt; link.querySelector('.instock.availability &amp;gt; i').textContent !== "In stock")
            // Extract the links from the data
            links=links.map(el =&amp;gt; el.querySelector('h3 &amp;gt; a').href)
            return links;
        })
        console.log(urls);
    }
}
module.exports=scrapeObject;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// run this command
node main.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it should work clearly&lt;/p&gt;

&lt;h2&gt;
  
  
  let's scrap all data about the books
&lt;/h2&gt;

&lt;p&gt;modifie the &lt;code&gt;pageScraper.js&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const scraperObject = {
    url: 'http://books.toscrape.com',
    async scraper(browser){
        let page = await browser.newPage();
        console.log(`Navigating to ${this.url}...`);
        // Navigate to the selected page
        await page.goto(this.url);
        // Wait for the required DOM to be rendered
        await page.waitForSelector('.page_inner');
        // Get the link to all the required books
        let urls = await page.$$eval('section ol &amp;gt; li', links =&amp;gt; {
            // Make sure the book to be scraped is in stock
            links = links.filter(link =&amp;gt; link.querySelector('.instock.availability &amp;gt; i').textContent !== "In stock")
            // Extract the links from the data
            links = links.map(el =&amp;gt; el.querySelector('h3 &amp;gt; a').href)
            return links;
        });


        // Loop through each of those links, open a new page instance and get the relevant data from them
        let pagePromise = (link) =&amp;gt; new Promise(async(resolve, reject) =&amp;gt; {
            let dataObj = {};
            let newPage = await browser.newPage();
            await newPage.goto(link);
            dataObj['bookTitle'] = await newPage.$eval('.product_main &amp;gt; h1', text =&amp;gt; text.textContent);
            dataObj['bookPrice'] = await newPage.$eval('.price_color', text =&amp;gt; text.textContent);
            dataObj['noAvailable'] = await newPage.$eval('.instock.availability', text =&amp;gt; {
                // Strip new line and tab spaces
                text = text.textContent.replace(/(\r\n\t|\n|\r|\t)/gm, "");
                // Get the number of stock available
                let regexp = /^.*\((.*)\).*$/i;
                let stockAvailable = regexp.exec(text)[1].split(' ')[0];
                return stockAvailable;
            });
            dataObj['imageUrl'] = await newPage.$eval('#product_gallery img', img =&amp;gt; img.src);
            dataObj['bookDescription'] = await newPage.$eval('#product_description', div =&amp;gt; div.nextSibling.nextSibling.textContent);
            dataObj['upc'] = await newPage.$eval('.table.table-striped &amp;gt; tbody &amp;gt; tr &amp;gt; td', table =&amp;gt; table.textContent);
            resolve(dataObj);
            await newPage.close();
        });

        for(link in urls){
            let currentPageData = await pagePromise(urls[link]);
            // scrapedData.push(currentPageData);
            console.log(currentPageData);
        }
        await browser.close();
    }
}

module.exports = scraperObject;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is all about crawling the data from a webpage&lt;/p&gt;

&lt;p&gt;You can find this on github &lt;a href="https://github.com/Mahmoudgalalz/pupt/tree/main/example" rel="noopener noreferrer"&gt;Full code&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>node</category>
    </item>
    <item>
      <title>How I automated git workflow!</title>
      <dc:creator>Mahmoud Galal</dc:creator>
      <pubDate>Sat, 06 Aug 2022 16:36:00 +0000</pubDate>
      <link>https://dev.to/mahmoudgalal/how-i-automated-git-workflow-42c</link>
      <guid>https://dev.to/mahmoudgalal/how-i-automated-git-workflow-42c</guid>
      <description>&lt;p&gt;everyday we use git, and we do a lot of &lt;code&gt;git add -A&lt;/code&gt; and a lot of commits also pushing and pulling, so I was thinking to make this easy for me, and I said why not for all developers!&lt;/p&gt;

&lt;p&gt;so I come up with &lt;strong&gt;CLI&lt;/strong&gt; for doing that!&lt;/p&gt;

&lt;h1&gt;
  
  
  What this CLI do?
&lt;/h1&gt;

&lt;p&gt;let's describe your flow when you want to start a project&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;make a dir&lt;/li&gt;
&lt;li&gt;init git&lt;/li&gt;
&lt;li&gt;make a remote
so with gite you can make this with just &lt;code&gt;gite repo&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;what if you want to push?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add -A to the stage&lt;/li&gt;
&lt;li&gt;commit with signed or not signed commit, I will mention it later&lt;/li&gt;
&lt;li&gt;then push, but whooo, there's stuff to fetch! 
with &lt;code&gt;gite push&lt;/code&gt; you just have to write where you want to push and which branch you want to push it and the &lt;strong&gt;biggest thing here&lt;/strong&gt; we handle fetching error, with automated one&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  So how can I install this CLI?
&lt;/h1&gt;

&lt;p&gt;you just want to have &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NPM&lt;/li&gt;
&lt;li&gt;nodejs&lt;/li&gt;
&lt;li&gt;GPG (optional for signed commit)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;this CLI tested on Linux ecosystem, and it will be available for windows later, it will work for macOS&lt;/p&gt;

&lt;h1&gt;
  
  
  installation guide
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;open your terminal 
&lt;code&gt;sudo npm i -g cli-gite&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;after download it install it&lt;br&gt;
&lt;code&gt;sudo ~/../../usr/local/lib/node_modules/cli-gite/install.sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if you want to use gpg key settings, you have to know more about it &lt;br&gt;
so read my  &lt;a href="https://github.com/Mahmoudgalalz/CLI-gite"&gt;documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OyzR1_vf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7v4aaa2vtla8jklykv5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OyzR1_vf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7v4aaa2vtla8jklykv5.png" alt="Image description" width="880" height="611"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  commands
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;gite repo&lt;/li&gt;
&lt;li&gt;gite push&lt;/li&gt;
&lt;li&gt;gite -v&lt;/li&gt;
&lt;li&gt;gite&lt;/li&gt;
&lt;li&gt;gite update&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  what is GPG Key
&lt;/h1&gt;

&lt;p&gt;This key used to make a verified commit in GitHub, it's important for people who work on company to make sure the make the push authorized commit &lt;/p&gt;

&lt;p&gt;you can setup it and make your on signature&lt;/p&gt;

&lt;h1&gt;
  
  
  contribution
&lt;/h1&gt;

&lt;p&gt;feel free to contribute with anything you want &lt;br&gt;
&lt;a href="https://github.com/Mahmoudgalalz/CLI-gite"&gt;repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>bash</category>
      <category>git</category>
      <category>github</category>
      <category>npm</category>
    </item>
    <item>
      <title>Why JavaScript is confusing?, Learn it by knowing the difference</title>
      <dc:creator>Mahmoud Galal</dc:creator>
      <pubDate>Fri, 01 Jul 2022 16:32:41 +0000</pubDate>
      <link>https://dev.to/mahmoudgalal/why-javascript-is-confusing-learn-it-by-knowing-the-difference-4p91</link>
      <guid>https://dev.to/mahmoudgalal/why-javascript-is-confusing-learn-it-by-knowing-the-difference-4p91</guid>
      <description>&lt;h1&gt;
  
  
  JavaScript is a superstar those days with its friend typescript
&lt;/h1&gt;

&lt;p&gt;They have a lot of libs and their community is really big, and this made it very useful as there are a lot of frameworks.&lt;/p&gt;

&lt;p&gt;So you can create everything with JavaScript, mobile apps, desktop, web apps, even AI models, IoT, Backend and Frontend. &lt;/p&gt;

&lt;p&gt;Those reasons maybe make you learn it.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why it’s confusing for us?
&lt;/h1&gt;

&lt;p&gt;really this has a lot to know, but mainly the big difference is the syntax, and some other functionality, I will cover all of them here.&lt;/p&gt;




&lt;h1&gt;
  
  
  Declaring a variable
&lt;/h1&gt;

&lt;p&gt;In other languages you know there’s a primitive Data Types&lt;/p&gt;

&lt;p&gt;like int,char,string,float,etc.&lt;/p&gt;

&lt;p&gt;but JavaScript have something else to say about &lt;strong&gt;Declaring Variables&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;they called it dynamically typed, and that means JavaScript will auto-understand what is inside this variable, like Python.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="c1"&gt;// int&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// string with single quote&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Str&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// string with double quote&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There’s no char here the string can be one character as you want.&lt;/p&gt;

&lt;p&gt;Other datatypes like boolean&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isWorking&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  What is confusing in Declaring a variable
&lt;/h1&gt;

&lt;p&gt;there’s an old-school keyword to declare a variable &lt;strong&gt;var&lt;/strong&gt;&lt;br&gt;
This keyword make a confusion&lt;br&gt;
let’s see&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// making a local scope with brackts&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;// undfinded &lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that’s happened because &lt;strong&gt;var&lt;/strong&gt; is a globally-scoped variable&lt;/p&gt;

&lt;p&gt;it’s seen everywhere&lt;/p&gt;

&lt;p&gt;to fix something like that we use ‘use strict’ I will talk about it soon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// making a local scope with brackts&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;// undfinded &lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;// undfinded&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Functions
&lt;/h1&gt;

&lt;p&gt;declare a function is everywhere but here’s the difference&lt;/p&gt;

&lt;p&gt;you can pass any type of value to the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; Kro&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//Hi Kro&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;maybe you see that’s cool but in many cases isn’t cool cause it will make your function fall into bugs, you have to be careful when you deal with it.&lt;/p&gt;

&lt;h1&gt;
  
  
  The ==
&lt;/h1&gt;

&lt;p&gt;really there’s another comparison operator is ===&lt;br&gt;
yeah you see&lt;/p&gt;

&lt;p&gt;the difference is really because the variable &lt;/p&gt;

&lt;p&gt;as you know we can declare anything using let&lt;/p&gt;

&lt;p&gt;so when we compare we may compare with different data types&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;6&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;//false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the == operator is just to see if they are the same without begin aware if they are different data types or not &lt;/p&gt;

&lt;p&gt;but the other one, === want the exact match in the look and the data type&lt;/p&gt;

&lt;h1&gt;
  
  
  Things to avoid
&lt;/h1&gt;

&lt;p&gt;As we know JavaScript does not require a &lt;strong&gt;semicolon&lt;/strong&gt; to end your statement&lt;/p&gt;

&lt;p&gt;but it must sometimes,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;really because JavaScript parser or engine ( similar to the compiler )&lt;/p&gt;

&lt;p&gt;put a semicolon on every good ending expression&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 13&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see here Js parser didn’t put a semicolon in this case, cause when he sees no good ending expression will ignore it.&lt;/p&gt;

&lt;p&gt;for more tutorials about JavaScript&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.info/"&gt;The Modern JavaScript Tutorial&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Master of Linked Lists data structure</title>
      <dc:creator>Mahmoud Galal</dc:creator>
      <pubDate>Sat, 04 Jun 2022 21:17:22 +0000</pubDate>
      <link>https://dev.to/mahmoudgalal/master-of-linked-lists-data-structure-edc</link>
      <guid>https://dev.to/mahmoudgalal/master-of-linked-lists-data-structure-edc</guid>
      <description>&lt;h2&gt;
  
  
  Master anything however it's so confusing
&lt;/h2&gt;




&lt;p&gt;I'm going to tell you the &lt;strong&gt;secret&lt;/strong&gt; of Mastering anything you want however is so hard to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  The story of how I realized this powerful method
&lt;/h2&gt;

&lt;p&gt;I started a challenge on one of problem-solving websites&lt;br&gt;
for solving 2 or 3 problems each day and after 2 months I realized that I'm a &lt;strong&gt;Master&lt;/strong&gt; ⚡ of using and solving problems that have &lt;strong&gt;Linked list&lt;/strong&gt; and I solve it very quick without any hinting, otherwise on any problem with any data structure&lt;br&gt;
like &lt;strong&gt;graph&lt;/strong&gt; I start struggling and google stuff and the topic is so confusing in my head, So I started to learn &lt;strong&gt;graph&lt;/strong&gt; with the same way I studied &lt;strong&gt;linked list&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Know the science first 🎓
&lt;/h2&gt;

&lt;p&gt;When you try to learn anything you should start understand the concept and the science of this thing.&lt;/p&gt;

&lt;p&gt;and those some tricks that will help you find out.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Head up to google and try to search why they did this thing.&lt;/li&gt;
&lt;li&gt;start compare it to something that do the same like linked list &lt;strong&gt;VS&lt;/strong&gt; arrays.&lt;/li&gt;
&lt;li&gt;look for difference between them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Baby steps 🐣
&lt;/h2&gt;

&lt;p&gt;When you do anything for your first time you should go slow as possible as you can.&lt;br&gt;
It's will help you to clear your thinking of many things you will face while learning.&lt;/p&gt;




&lt;h2&gt;
  
  
  Imagine it
&lt;/h2&gt;

&lt;p&gt;While you try to understand something confusing to you, The best thing to do it's to imagine how it would be like in the real world.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Relate it&lt;/strong&gt; to thing you know very will&lt;/p&gt;




&lt;h2&gt;
  
  
  Teach someone 📖
&lt;/h2&gt;

&lt;p&gt;even you didn't learn a lot you have to do &lt;strong&gt;multiple teaching&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;in the early step &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Teach someone know the topic will&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Talk to your rubber duck (any cute thing) and explain what you learned &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;try to explain it to one who has the intention to learn this topic &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practice on the topic a lot
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;innovate when you practice
when you solving a problem and you got a solution try to solve it again later days when you forgot the other solution you will find this so impressive,&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because your mind will be more different and more smarter than the before days   &lt;/p&gt;




&lt;p&gt;If you know a trick don't miss to share it with us in the comments&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Custom Commands in your Linux or Mac</title>
      <dc:creator>Mahmoud Galal</dc:creator>
      <pubDate>Wed, 01 Jun 2022 14:48:35 +0000</pubDate>
      <link>https://dev.to/mahmoudgalal/custom-commands-in-your-linux-or-mac-368m</link>
      <guid>https://dev.to/mahmoudgalal/custom-commands-in-your-linux-or-mac-368m</guid>
      <description>&lt;h1&gt;
  
  
  Terminals are the superpower of developers
&lt;/h1&gt;

&lt;p&gt;I have been in love with using terminals all the time even on windows.&lt;br&gt;
The true love comes after I understand most of the commands and how to write &lt;strong&gt;Bash&lt;/strong&gt; Scripts, and If you are not a Linux or Mac user using the terminal, you missed a lot.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can I make my own commands?
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your terminal using &lt;code&gt;CTRL + ALT + T&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;sudo vim ~/.bashrc&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hA8Sp-jO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1n3lesk047r0iioevh4q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hA8Sp-jO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1n3lesk047r0iioevh4q.png" alt="Image description" width="786" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if you get something else so, you should install Vim&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt install vim&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and run the command before &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go down and write your bash script
&lt;code&gt;click I to insert in the file&lt;/code&gt;
write 
&lt;code&gt;alias [your command]='[ the executable command in Linux ] '&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W52uF6WY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1e2528qxc80auhbntbw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W52uF6WY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1e2528qxc80auhbntbw.png" alt="Image description" width="786" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then &lt;code&gt;ESC&lt;/code&gt; and &lt;code&gt;:wq&lt;/code&gt; &lt;code&gt;Enter&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  if you want to make clean aliases
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;create a new file in the home ~ dir `touch .bash_aliases'&lt;/li&gt;
&lt;li&gt;go into it using &lt;code&gt;vim .bash_aliases&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;put your aliases there &lt;/li&gt;
&lt;li&gt;save it by &lt;code&gt;ESC&lt;/code&gt; and &lt;code&gt; :wq &lt;/code&gt; &lt;code&gt;Enter&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;close the current terminal to refresh your bashrc&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;open a new terminal and test your new commands&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CZ3hNiUb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubvhaxz5vkmn7sjjbuc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CZ3hNiUb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubvhaxz5vkmn7sjjbuc3.png" alt="Image description" width="786" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;this is the end of the tutorial *&lt;/em&gt; &lt;/p&gt;

&lt;h1&gt;
  
  
  How does it works?
&lt;/h1&gt;

&lt;p&gt;when we open a terminal there's a bash script run to provide a path of everything we install,&lt;br&gt;
when you write &lt;code&gt;code .&lt;/code&gt; this opens your VScode so this command is saved in the bashrc&lt;br&gt;
this makes a configuration for everything in the current terminal &lt;/p&gt;

&lt;p&gt;so this script begins to execute every time you open the terminal, what if I want to print something every time a open a terminal &lt;/p&gt;

&lt;p&gt;log to your bashrc again and write what you want using &lt;br&gt;
&lt;code&gt;echo Hi KroKing&lt;/code&gt; &lt;br&gt;
save and restart the terminal &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GiAcG2f_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbsxv7wg4urzfp9coa81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GiAcG2f_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbsxv7wg4urzfp9coa81.png" alt="Image description" width="786" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*Follow for more Tricks in Linux *&lt;/p&gt;

</description>
      <category>linux</category>
      <category>ubuntu</category>
      <category>tutorial</category>
      <category>bash</category>
    </item>
    <item>
      <title>Low-Level Trick to solve problems</title>
      <dc:creator>Mahmoud Galal</dc:creator>
      <pubDate>Sun, 29 May 2022 18:59:24 +0000</pubDate>
      <link>https://dev.to/mahmoudgalal/low-level-trick-to-solve-problems-69o</link>
      <guid>https://dev.to/mahmoudgalal/low-level-trick-to-solve-problems-69o</guid>
      <description>&lt;p&gt;&lt;strong&gt;Solve programming problems with one of the most popular Low-Level Technics&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Bit Manipulation &amp;amp; Bitwise Operators
&lt;/h1&gt;

&lt;p&gt;When you compile anything with your favorite programming language it’s converted to machine code &lt;/p&gt;

&lt;p&gt;There’s a very impressive trick that could help you to solve problems 😎&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;what are the Bitwise operators?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;&amp;amp; (bitwise AND)&lt;/strong&gt; in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;| (bitwise OR)&lt;/strong&gt; in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;^ (bitwise XOR)&lt;/strong&gt; in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;&amp;lt;&amp;lt; (left shift)&lt;/strong&gt; in C or C++ takes two numbers, the left shifts the bits of the first operand, and the second operand decides the number of places to shift.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;&amp;gt;&amp;gt; (right shift)&lt;/strong&gt; in C or C++ takes two numbers, the right shifts the bits of the first operand, and the second operand decides the number of places to shift.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;~ (bitwise NOT)&lt;/strong&gt; in C or C++ takes one number and inverts all bits of it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let’s know how it works by example
&lt;/h2&gt;

&lt;p&gt;Put in your mind that every number you will deal with will be converted into &lt;strong&gt;binary&lt;/strong&gt; &lt;br&gt;
and If you are familiar with &lt;strong&gt;digital circuits&lt;/strong&gt;, it will be so easy to understand how they work&lt;/p&gt;
&lt;h3&gt;
  
  
  Example #1
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;// a = 5(00000101), b = 9(00001001)&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// The result is 00000001&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"a = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;","&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" b = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"a &amp;amp; b = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;let’s put a sample rule 
0 is false
1 is true 
every number will be reconverted into decimals again when printing it.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Explanation #1
&lt;/h3&gt;

&lt;p&gt;The &amp;amp; operator work like if the two cases are 1 it will return 1&lt;br&gt;
so &lt;br&gt;
1&amp;amp;1 = 1&lt;br&gt;
1&amp;amp;0 = 0&lt;br&gt;
0&amp;amp;1 = 0&lt;br&gt;
0&amp;amp;0 = 0  &lt;/p&gt;
&lt;h3&gt;
  
  
  Example #1
&lt;/h3&gt;

&lt;p&gt;a = 5 , in binary it will be 00000101&lt;/p&gt;

&lt;p&gt;b = 9 , in binary it will be 00001001&lt;/p&gt;

&lt;p&gt;so we take each digit and apply our &amp;amp; rule &lt;/p&gt;

&lt;p&gt;1 &amp;amp; 1 = 1, and so on..&lt;/p&gt;

&lt;p&gt;so the result will be a &amp;amp; b = 00000001&lt;/p&gt;


&lt;h3&gt;
  
  
  Example #2
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The result is 00001101&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"a | b = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Explanation #2
&lt;/h3&gt;

&lt;p&gt;The | operator work if one of the cases is 1 it will return 1&lt;br&gt;
1|1 = 1&lt;br&gt;
1|0 = 1&lt;br&gt;
0|1 = 1&lt;br&gt;
0|0 = 0&lt;/p&gt;
&lt;h3&gt;
  
  
  Example #2
&lt;/h3&gt;

&lt;p&gt;a = 5 , in binary it will be 00000101&lt;/p&gt;

&lt;p&gt;b = 9 , in binary it will be 00001001&lt;/p&gt;

&lt;p&gt;so we take each digit and apply our | rule &lt;/p&gt;

&lt;p&gt;1 | 1 = 1, and so on..&lt;/p&gt;

&lt;p&gt;so the result will be a | b = 00001101&lt;/p&gt;


&lt;h3&gt;
  
  
  Example #3
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The result is 00001100&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"a ^ b = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Explanation #3
&lt;/h3&gt;

&lt;p&gt;The ^ operator it’s a little bit confusing, it works if the two numbers are different it will return 1&lt;br&gt;
1^1 = 0&lt;br&gt;
1^0 = 1&lt;br&gt;
0^1 = 1&lt;br&gt;
0^0 = 0&lt;/p&gt;
&lt;h3&gt;
  
  
  Example #3
&lt;/h3&gt;

&lt;p&gt;a = 5, in binary it will be 00000101&lt;/p&gt;

&lt;p&gt;b = 9, in binary it will be 00001001&lt;/p&gt;

&lt;p&gt;so we take each digit and apply our ^ rule &lt;/p&gt;

&lt;p&gt;1 ^ 1 = 0, and so on..&lt;/p&gt;

&lt;p&gt;so the result will be a ^ b = 00001100 = 12 in decimal &lt;/p&gt;


&lt;h3&gt;
  
  
  Example #4
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The result is 11111010&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"~a = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Explanation #4
&lt;/h3&gt;

&lt;p&gt;The ~ operator gets the opposite of the number so,&lt;br&gt;
~1 = 0&lt;br&gt;
~0 = 1&lt;/p&gt;
&lt;h3&gt;
  
  
  Example #4
&lt;/h3&gt;

&lt;p&gt;a = 5, in binary it will be 00000101&lt;/p&gt;

&lt;p&gt;so we take each digit and apply our ~ rule &lt;/p&gt;

&lt;p&gt;~1 = 0 , and so on..&lt;/p&gt;

&lt;p&gt;so the result will be  ~a  = 11111010 = -6  in decimal it’s &lt;strong&gt;too way hard to convert it to decimal&lt;/strong&gt; with basic rules so search for 2’s complement, 1’s complement and Signed magnitude&lt;br&gt;&lt;br&gt;
&lt;strong&gt;keep in mind - the last left digit is to represent if it is positive = 0 or negative = 1, when you apply this operator you should add +1 to the result if you want to get the negative value of the number&lt;/strong&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Example #5
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// b=9 (00001001)&lt;/span&gt;
&lt;span class="c1"&gt;// The result is 00010010&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"b &amp;lt;&amp;lt; 1"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;" = "&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// The result is 00000100&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"b &amp;gt;&amp;gt; 1 "&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"= "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Explanation #5
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&amp;lt;&amp;lt; operator&lt;/strong&gt; shift the bit pattern n time to &lt;strong&gt;The Left&lt;/strong&gt; which in our case n=1 and append 0 at the end of the number, the Left shift is equivalent to &lt;strong&gt;multiplying&lt;/strong&gt; the bit pattern with $2^n$&lt;br&gt;
 ( if we are shifting n bits ).&lt;br&gt;
1 = 00000001&lt;br&gt;
1 &amp;lt;&amp;lt; 1 = 00000010 = 2 = $(1*2^1)$ and so on ..&lt;br&gt;
in the &lt;strong&gt;example&lt;/strong&gt; &lt;br&gt;
9 &amp;lt;&amp;lt; 1 = 00010010 = $(9 * 2^1)$ = 18&lt;/p&gt;



&lt;p&gt;The &lt;strong&gt;&amp;gt;&amp;gt; operator&lt;/strong&gt; shifts the bit pattern n time to &lt;strong&gt;The Right&lt;/strong&gt; which in our case n=1 and append 1 at the end of the number, the Left shift is equivalent to dividing the bit pattern with $2^n$&lt;br&gt;
 ( if we are shifting n bits ). &lt;strong&gt;Keep in mind Binary has no float points in this case&lt;/strong&gt;&lt;br&gt;
4 = 00000100&lt;br&gt;
4 &amp;gt;&amp;gt; 1 = 00000010 = 2 = $(4/2^1)$&lt;/p&gt;

&lt;p&gt;5 = 00000101&lt;br&gt;
6 &amp;gt;&amp;gt; 1 = 00000010 = 2 = $(5/2^1)$ in decimal and so on ..&lt;/p&gt;

&lt;p&gt;in the &lt;strong&gt;example&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;9 &amp;gt;&amp;gt; 1 = 00000100 = 4 = $( 9/2^n )$ in decimal&lt;/p&gt;


&lt;h2&gt;
  
  
  Let’s know how Bit manipulation works with Example
&lt;/h2&gt;

&lt;p&gt;and why is so helpful for an optimized solution, its works like a magic &lt;strong&gt;🪄&lt;/strong&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  let’s start with an easy example
&lt;/h3&gt;

&lt;p&gt;we want to know if x is a power of 2 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;implementation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;isPowerOfTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code has a time complexity O(log N)&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Is there an optimal approach?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The same problem can be solved using bit &lt;strong&gt;manipulation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider a number x that we need to check for being a power for 2.&lt;/p&gt;

&lt;p&gt;Now think about the binary representation of (x-1).&lt;/p&gt;

&lt;p&gt;(x-1) will have all the bits the same as x, except for the &lt;strong&gt;rightmost&lt;/strong&gt; 1 in x and all the bits to the right of the rightmost 1.&lt;/p&gt;

&lt;p&gt;Let, x = 4 = (100)2   x - 1 = 3 = (011)2&lt;/p&gt;

&lt;p&gt;Let, x = 6 = (110)2   x - 1 = 5 = (101)2&lt;/p&gt;

&lt;p&gt;It might not seem obvious with these examples, but the binary representation of (x-1) can be obtained by simply &lt;strong&gt;flipping&lt;/strong&gt; all the bits to the right of rightmost 1 in x and also including the rightmost 1.&lt;/p&gt;

&lt;p&gt;Now think about x &amp;amp; (x-1). &lt;/p&gt;

&lt;p&gt;x &amp;amp; (x-1) will have all the bits equal to the x except for the rightmost 1 in x.&lt;/p&gt;

&lt;p&gt;Let, x = 4 = (100)2   x - 1 = 3 = (011)2   x &amp;amp; (x-1) = 4 &amp;amp; 3 = (100)2 &amp;amp; (011)2 = (000)2&lt;/p&gt;

&lt;p&gt;Let, x = 6 = (110)2   x - 1 = 5 = (101)2   x &amp;amp; (x-1) = 6 &amp;amp; 5 = (110)2 &amp;amp; (101)2 = (100)2&lt;/p&gt;

&lt;p&gt;in x= 6, 6 is not a power of 2 so when x &amp;amp; (x-1) = (100)2 that mean it’s not what we want &lt;/p&gt;

&lt;p&gt;Properties for numbers which are powers of 2, is that they have one and only one bit set in their binary representation. &lt;/p&gt;

&lt;p&gt;If the number is neither zero nor a power of two, it will have 1 in more than one place. So if x is a power of 2 then x &amp;amp; (x-1) will be 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;isPowerOfTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// x will check if x == 0 and !(x &amp;amp; (x - 1)) will check if x is a power of 2 or not&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the above code has time complexity O(1)&lt;/p&gt;




&lt;h2&gt;
  
  
  Another Hard Example to see how much bit manipulation is powerful
&lt;/h2&gt;

&lt;p&gt;Given a string array of &lt;code&gt;words&lt;/code&gt;&lt;br&gt;
, return &lt;em&gt;the maximum value of&lt;/em&gt;&lt;br&gt;
 &lt;code&gt;length(word[i]) * length(word[j])&lt;/code&gt;&lt;br&gt;
 &lt;em&gt;where the two words do not share common letters&lt;/em&gt;&lt;br&gt;
. If no such two words exist, return &lt;code&gt;0&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;Idea is to create a bitmask of each string and compare it with the bitmask of other strings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bitmask Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&lt;/strong&gt;Let's say we have three strings "abc", "def" and "abde", corresponding bitmasks of these strings will be "111", "111000", and "11011". how?&lt;/p&gt;

&lt;p&gt;For each character, we'll need to find the index where we need to set 1 in our bitmask. So for character "a" index will be 0, for "b" it will be 1, for "c" it will be 2 and so on.&lt;/p&gt;

&lt;p&gt;The index can be easily found by subtracting each character by 'a' Ex.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="sc"&gt;'b'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="sc"&gt;'c'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next step is to left shift 1 by the index value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="sc"&gt;'b'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="sc"&gt;'c'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After shifting, the last step is to OR the shifted value of current character with the bitmask of current string. So if we OR the shifted values of character "a", "b" and "c".&lt;/p&gt;

&lt;p&gt;&lt;code&gt;001 | 010 | 100 = 111&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check for common characters -&lt;/strong&gt;If two strings s1 and s2 have common characters, then they will have 1 at the same index in the bitmask and if we do AND of the bitmasks of s1 and s2, it will result in a value greater than 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;bitmask&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;bitmask&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;common&lt;/span&gt; &lt;span class="n"&gt;characters&lt;/span&gt;
                            &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;otherwise&lt;/span&gt;

&lt;span class="n"&gt;Ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;bitmask&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="s"&gt;"abc"&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="s"&gt;"def"&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;111&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;111000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;respectively&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="mi"&gt;111&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;111000&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hence&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;common&lt;/span&gt; &lt;span class="n"&gt;characters&lt;/span&gt;

&lt;span class="n"&gt;bitmask&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="s"&gt;"abc"&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="s"&gt;"abde"&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;111&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;11011&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;respectively&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="mi"&gt;111&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;11011&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mo"&gt;00011&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hence&lt;/span&gt; &lt;span class="n"&gt;common&lt;/span&gt; &lt;span class="n"&gt;characters&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Finally, code&lt;/strong&gt; -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;maxProduct&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;bitmask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Calculate bitmask for current word&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// index will be - for a -&amp;gt; 0, b -&amp;gt; 1, c -&amp;gt; 2 and so on&lt;/span&gt;
                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;charAt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

                &lt;span class="c1"&gt;// left shift 1 by index and OR it with the current bitmask&lt;/span&gt;
                &lt;span class="n"&gt;bitmask&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="c1"&gt;// Compare bitmask of current string with previous strings bitmask&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="cm"&gt;/* If there is a 1 at the same index of current string {i} and {j},
                then bitmask of i and j string will result in a number greater than 0,
                else it will result in 0 */&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmask&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;bitmask&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()*&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time Complexity = O(n.(k+n))&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>computerscience</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
