{"componentChunkName":"component---src-pages-author-author-yaml-id-js","path":"/author/kheenvraj-lomror/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"f9247396-c4d1-5c04-b7e8-9f161f2b1770","html":"<h2 id=\"how-does-bitwise--xor-work\" style=\"position:relative;\"><a href=\"#how-does-bitwise--xor-work\" aria-label=\"how does bitwise  xor work permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How does bitwise ^ (XOR) work?</h2>\n<p>XOR is a bitwise operator, and it stands for \"exclusive or.\" It performs <strong>logical</strong> operation. If input bits are the same, then the output will be false(0) else true(1).</p>\n<p>XOR table:</p>\n<table>\n<thead>\n<tr>\n<th>X</th>\n<th>Y</th>\n<th>X^Y</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>0</td>\n<td>0</td>\n<td>0</td>\n</tr>\n<tr>\n<td>0</td>\n<td>1</td>\n<td>1</td>\n</tr>\n<tr>\n<td>1</td>\n<td>0</td>\n<td>1</td>\n</tr>\n<tr>\n<td>1</td>\n<td>1</td>\n<td>0</td>\n</tr>\n</tbody>\n</table>\n<p>Example:  <code>4^3 = 7</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">In</span><span class=\"mtk1\"> binary: </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t     </span><span class=\"mtk7\">0100</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t   ^ </span><span class=\"mtk7\">0011</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t    ------</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> Result: </span><span class=\"mtk7\">0111</span><span class=\"mtk1\">  </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> (</span><span class=\"mtk7\">7</span><span class=\"mtk1\">)</span></span></code></pre>\n<h4 id=\"xor-with-negative-numbers\" style=\"position:relative;\"><a href=\"#xor-with-negative-numbers\" aria-label=\"xor with negative numbers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>XOR with negative numbers</h4>\n<p>Let's understand with an example <code>-4^-2 = 2</code>\nIn the above example, we can see <code>-4^-2</code>output will be <code>2</code>\nbut the question arises how? Because if we represent both inputs in the binary form, then we do XOR of bits, then the output will be <code>0000 0110</code> and in decimal, it will be <code>6</code> but as we know output should be <code>2</code>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk7\">1000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0100</span><span class=\"mtk1\"> (-</span><span class=\"mtk7\">4</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">^ </span><span class=\"mtk7\">1000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0010</span><span class=\"mtk1\"> (-</span><span class=\"mtk7\">2</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">------------------</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk7\">0000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0110</span><span class=\"mtk1\"> </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> (</span><span class=\"mtk7\">6</span><span class=\"mtk1\">) </span><span class=\"mtk3\">// incorrect output</span></span></code></pre>\n<blockquote>\n<p>Note: Here, the leftmost bit position is reserved for the sign of the\nvalue (positive or negative) and doesn't contribute towards the value of the number.</p>\n</blockquote>\n<p> Let's understand how the XOR operation works with negative numbers.</p>\n<h5 id=\"how-xor-operation-works-with-negative-numbers\" style=\"position:relative;\"><a href=\"#how-xor-operation-works-with-negative-numbers\" aria-label=\"how xor operation works with negative numbers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How XOR operation works with negative numbers?</h5>\n<p>First, the XOR operation is to XOR each bit (the same is 0, the difference is 1), but you need to convert the number into a complement first.</p>\n<ol>\n<li>The complement of a positive number is itself</li>\n<li>\n<p>The complement of the negative number is reversed for each bit and then incremented by 1 (the highest is kept at 1)</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">// Lets take -4</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">In</span><span class=\"mtk1\"> binary: \t\t\t</span><span class=\"mtk7\">1000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0100</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">Reverse: \t\t\t</span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1011</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">complement</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">Increment</span><span class=\"mtk1\"> </span><span class=\"mtk12\">by</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1</span><span class=\"mtk1\">): \t</span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1100</span></span></code></pre>\n</li>\n</ol>\n<p>// Now -2\nIn binary: \t\t\t1000 0010\nReverse: \t\t\t1111 1101\ncomplement (Increment by 1): \t1111 1110</p>\n<p>Final result:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk4\">of</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">4</span><span class=\"mtk1\"> : </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1100</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk4\">of</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">2</span><span class=\"mtk1\"> : </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1110</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                  -----------</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">Result:            </span><span class=\"mtk7\">0000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0010</span><span class=\"mtk1\"> \t</span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> </span><span class=\"mtk7\">2</span></span></code></pre>\n<p><em>Here, the MSB bit of result will denote the sign, and the rest of the bits will denote the value of the final result.</em>\nXOR sign table could be useful to understand the sign of result:</p>\n<table>\n<thead>\n<tr>\n<th>X</th>\n<th>Y</th>\n<th>X^Y</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>+</td>\n<td>+</td>\n<td>+</td>\n</tr>\n<tr>\n<td>+</td>\n<td>-</td>\n<td>-</td>\n</tr>\n<tr>\n<td>-</td>\n<td>+</td>\n<td>-</td>\n</tr>\n<tr>\n<td>-</td>\n<td>-</td>\n<td>+</td>\n</tr>\n</tbody>\n</table>\n<p>The above approach will work with negative inputs, but if we have positive and negative, then? </p>\n<h5 id=\"how-xor-operation-works-with-positive-and-negative-numbers\" style=\"position:relative;\"><a href=\"#how-xor-operation-works-with-positive-and-negative-numbers\" aria-label=\"how xor operation works with positive and negative numbers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How XOR operation works with positive and negative numbers?</h5>\n<p>Let's performs <code>-5 ^ 2</code>\nFollow the same above approach to get a complement code of <code>-5</code>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk4\">of</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">5</span><span class=\"mtk1\"> :          </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1011</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">// Note: complement of a positive number is itself</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk4\">of</span><span class=\"mtk1\"> </span><span class=\"mtk7\">2</span><span class=\"mtk1\"> :           </span><span class=\"mtk7\">0000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0010</span><span class=\"mtk1\">  </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> result:          </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1001</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Now</span><span class=\"mtk1\">, </span><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk12\">result</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">1</span><span class=\"mtk1\"> : </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1000</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                           -----------</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Final</span><span class=\"mtk1\"> </span><span class=\"mtk11\">result</span><span class=\"mtk1\">(</span><span class=\"mtk12\">inverse</span><span class=\"mtk1\"> </span><span class=\"mtk12\">code</span><span class=\"mtk1\">): </span><span class=\"mtk7\">1000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0111</span><span class=\"mtk1\">   </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">7</span></span></code></pre>\n<h4 id=\"some-interesting-use-cases-of-xor-operator\" style=\"position:relative;\"><a href=\"#some-interesting-use-cases-of-xor-operator\" aria-label=\"some interesting use cases of xor operator permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Some interesting use cases of XOR operator</h4>\n<ul>\n<li>\n<p>Swap two number without the third variable\nExample:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">swapWithXOR</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">a</span><span class=\"mtk1\">, </span><span class=\"mtk12\">b</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">a</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">a</span><span class=\"mtk1\">^</span><span class=\"mtk12\">b</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">b</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">a</span><span class=\"mtk1\">^</span><span class=\"mtk12\">b</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">a</span><span class=\"mtk1\">=</span><span class=\"mtk12\">a</span><span class=\"mtk1\">^</span><span class=\"mtk12\">b</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;result =&gt; a: &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk12\">a</span><span class=\"mtk1\"> + </span><span class=\"mtk8\">&quot;, b: &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk12\">b</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">swapWithXOR</span><span class=\"mtk1\">(</span><span class=\"mtk7\">4</span><span class=\"mtk1\">, </span><span class=\"mtk7\">1</span><span class=\"mtk1\">); </span><span class=\"mtk3\">// result =&gt; a: 1, b: 4</span></span></code></pre>\n</li>\n<li>RAID Drive Backups (Systems)</li>\n<li>Cryptography (XOR cipher)</li>\n<li>Array operations</li>\n</ul>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n</style>","frontmatter":{"title":"How does bitwise ^ (XOR) work?","author":{"id":"Kheenvraj Lomror","github":"rajlomror","avatar":null},"date":"November 24, 2020","updated_date":null,"tags":["bitwise","XOR"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/3dc791a9bfecb4dc03ef8263118ccf7d/58556/xor.webp","srcSet":"/static/3dc791a9bfecb4dc03ef8263118ccf7d/61e93/xor.webp 200w,\n/static/3dc791a9bfecb4dc03ef8263118ccf7d/1f5c5/xor.webp 400w,\n/static/3dc791a9bfecb4dc03ef8263118ccf7d/58556/xor.webp 800w,\n/static/3dc791a9bfecb4dc03ef8263118ccf7d/99238/xor.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"fields":{"authorId":"Kheenvraj Lomror","slug":"/engineering/how-does-bitwise-xor-work/"}}},{"node":{"id":"ed0652a4-d39b-587e-b5b5-d871ba867a5d","html":"<p>Have you ever heard of SonarQube? Would you like to know What it is? And how to use it? And its benefits to the production phase of software development. Then you are at the right place; in this tutorial of SonarQube, I will give you a walkthrough of every aspect of SonarQube.</p>\n<p>Let's dive in!!!</p>\n<h2 id=\"what-is-sonarqube\" style=\"position:relative;\"><a href=\"#what-is-sonarqube\" aria-label=\"what is sonarqube permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is SonarQube?</h2>\n<p>SonarQube is an open-source platform developed by SonarSource for continuous inspection of code quality. Sonar does static code analysis, which provides a detailed report of bugs, code smells, vulnerabilities, code duplications. </p>\n<p>It supports 25+ major programming languages through built-in rulesets and can also be extended with various plugins.</p>\n<h2 id=\"benefits-of-sonarqube\" style=\"position:relative;\"><a href=\"#benefits-of-sonarqube\" aria-label=\"benefits of sonarqube permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Benefits of SonarQube</h2>\n<ol>\n<li><strong>Sustainability</strong> - Reduces complexity, possible vulnerabilities, and code duplications, optimising the life of applications.</li>\n<li><strong>Increase productivity</strong> - Reduces the scale, cost of maintenance, and risk of the application; as such, it removes the need to spend more time changing the code</li>\n<li><strong>Quality code</strong> - Code quality control is an inseparable part of the process of software development.</li>\n<li><strong>Detect Errors</strong> - Detects errors in the code and alerts developers to fix them automatically before submitting them for output.</li>\n<li><strong>Increase consistency</strong> - Determines where the code criteria are breached and enhances the quality</li>\n<li><strong>Business scaling</strong> - No restriction on the number of projects to be evaluated</li>\n<li><strong>Enhance developer skills</strong> - Regular feedback on quality problems helps developers to improve their coding skills</li>\n</ol>\n<h2 id=\"why-sonarqube\" style=\"position:relative;\"><a href=\"#why-sonarqube\" aria-label=\"why sonarqube permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why SonarQube?</h2>\n<p>Developers <a href=\"/agile-development-team/\">working with hard deadlines</a> to deliver the required functionality to the customer. It is so important for developers that many times they compromise with the code quality, potential bugs, code duplications, and bad distribution of complexity.</p>\n<p>Additionally, they tend to leave unused variables, methods, etc. In this scenario, the code would work in the desired way.</p>\n<p>Do you think this is a proper way to deliver functionality? </p>\n<p>The answer is NO.</p>\n<p>To avoid these issues in code, developers should always follow the good coding practice, but sometimes it is not possible to follow the rules and maintain the good quality as there may be many reasons.</p>\n<p>In order to achieve continuous code integration and deployment, developers need a tool that not only works once to check and tell them the problems in the code but also to track and control the code to check continuous code quality. To satisfy all these requirements, here comes SonarQube in the picture.</p>\n<h2 id=\"how-to-setup-and-use-the-sonarqube-plugin\" style=\"position:relative;\"><a href=\"#how-to-setup-and-use-the-sonarqube-plugin\" aria-label=\"how to setup and use the sonarqube plugin permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to setup and use the sonarqube plugin</h2>\n<p>This section will explain the steps or procedures to configure the sonarqube plugin for all the major programming languages.</p>\n<h3 id=\"prerequisites\" style=\"position:relative;\"><a href=\"#prerequisites\" aria-label=\"prerequisites permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Prerequisites:</h3>\n<p>The only prerequisite for running SonarQube is to have Java (Oracle JRE 11 or OpenJDK 11) installed on your machine. [<a href=\"https://docs.sonarsource.com/sonarqube-server/latest/setup-and-upgrade/installation-requirements/server-host/\">details</a>]</p>\n<p>Set the PATH system variable: <a href=\"https://www.java.com/en/download/help/path.xml\">How do I set or change the PATH system variable?</a></p>\n<blockquote>\n<p>Note: In order to analyze JavaScript code, you need to have Node.js >= 8 installed on the machine running the scan. If a standard node is not available, you have to set the property <code>sonar.nodejs.executable</code> to an absolute path to Node.js executable. [<a href=\"https://docs.sonarqube.org/latest/analysis/languages/javascript/\">details</a>]</p>\n</blockquote>\n<h3 id=\"downloads-sonarqube-and-sonar-scanner\" style=\"position:relative;\"><a href=\"#downloads-sonarqube-and-sonar-scanner\" aria-label=\"downloads sonarqube and sonar scanner permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Downloads Sonarqube and Sonar Scanner:</h3>\n<ul>\n<li><a href=\"https://www.sonarqube.org/downloads\">LTS version of sonarqube</a></li>\n<li><a href=\"https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/\">Download sonar-scanner</a> based on your platform\nAfter the above process, Add the sonar scanner path to environment variables.</li>\n</ul>\n<h3 id=\"run-sonarqube-server\" style=\"position:relative;\"><a href=\"#run-sonarqube-server\" aria-label=\"run sonarqube server permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Run Sonarqube server:</h3>\n<ul>\n<li>\n<p>To start the sonar server open cmd or terminal and set sonarqube bin folder path and choose the platform and run the following command. Examples :</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">#For Windows(cmd):</span>\n<span class=\"grvsc-line\">C:\\sonarqube\\bin\\windows-x86-64&gt;StartSonar.bat</span>\n<span class=\"grvsc-line\">#For other OS (terminal): </span>\n<span class=\"grvsc-line\">C:\\sonarqube\\bin\\[OS]&gt;sonar.sh</span></code></pre>\n<p>Once the sonar server up successfully, then Log in to <code>http://localhost:9000</code> with System Administrator credentials (login=admin, password=admin).</p>\n</li>\n</ul>\n<h3 id=\"creating-and-analysing-a-project\" style=\"position:relative;\"><a href=\"#creating-and-analysing-a-project\" aria-label=\"creating and analysing a project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Creating and Analysing a Project:</h3>\n<ul>\n<li>Go to create a new project and enter the project key, and display name details. (+ sign on the top right side).</li>\n<li>Provide a token by either generating a token or using an existing token (find existing token on  <code>http://localhost:9000/account/security</code> page).</li>\n<li>Run analysis on the project by selecting the programming language used in the repository and your system OS. </li>\n<li>As we already downloaded and set up the sonar-scanner path, No need to download it again.</li>\n<li>\n<p>Now copy the displaying command and Go to your project path and Run the copied command. It looks like below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">sonar-scanner.bat -D&quot;sonar.projectKey=rtetre&quot; -D&quot;sonar.sources=.&quot;-D&quot;sonar.host.url=http://localhost:9000&quot; -D&quot;sonar.login=e932dxxxxx9a8e5c7903xxxxxx96928xxxxxxx&quot;</span></code></pre>\n</li>\n</ul>\n<p>After the analysis is done, you can either browse the provided link to see the sonar report directly [Ex.: <code>http://localhost:9000/dashboard?id=</code>] or go to the project section to see the newly generated sonar report of your project. </p>\n<p>If you want to exclude some files from analysis(like test files), then go to the administration section and navigate to the General setting, select the programming language used, and set a list of file path patterns to be excluded from the analysis.</p>\n<p>Now re-run the analysis command given above for an updated sonar report.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>The goal of SonarQube is to empower developers first and to grow an open community around the quality and security of code. I hope with this quick tutorial of sonarqube you have the basic idea of the functionalities, and If you believe so, drop a comment and show your support.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"title":"Sonarqube: What it is and why to use it?","author":{"id":"Kheenvraj Lomror","github":"rajlomror","avatar":null},"date":"July 11, 2020","updated_date":null,"tags":["Code Quality","SonarQube"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.7699115044247788,"src":"/static/003f532e69e3da13ba2847a99744ade0/58556/sonarqube.webp","srcSet":"/static/003f532e69e3da13ba2847a99744ade0/61e93/sonarqube.webp 200w,\n/static/003f532e69e3da13ba2847a99744ade0/1f5c5/sonarqube.webp 400w,\n/static/003f532e69e3da13ba2847a99744ade0/58556/sonarqube.webp 800w,\n/static/003f532e69e3da13ba2847a99744ade0/99238/sonarqube.webp 1200w,\n/static/003f532e69e3da13ba2847a99744ade0/135cd/sonarqube.webp 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"fields":{"authorId":"Kheenvraj Lomror","slug":"/engineering/sonarqube/"}}}]},"authorYaml":{"id":"Kheenvraj Lomror","bio":"Software Developer @LoginRadius. JavaScript Lover, love to work with Angular, React","github":"rajlomror","stackoverflow":"13910473","linkedin":"kheevraj-lomror-26936982","medium":null,"twitter":null,"avatar":null}},"pageContext":{"id":"Kheenvraj Lomror","__params":{"id":"kheenvraj-lomror"}}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}