Bitwise Operators

6+ hours across 72 lessons — Data Structures, Algorithms, Cryptography, Binary, Software Design, and Essential Unix Skills.
All of the logic gates we've been using (AND, XOR, OR, etc) are part of every programming languages as bitwise operators. You've probably seen them before - here they are in JavaScript:
&is AND||is OR^is XOR
And so on. In this video we dive in head-first and answer a pretty common interview question:
Create a routine that adds two positive integers together without using mathematic operators.
I got asked this once and drew a complete blank as I had no idea and let the interviewer know that. They tried to offer me clues but I was beyond any kind of help, in a completely foreign land. I knew absolutely nothing about binary!
If this is you, buckle up cause here we go!
The Code
Here's the code we'll be cycling on for the video:
<span class="hljs-keyword">const</span> add =<span class="hljs-keyword">function</span>(<span class="hljs-params">x,y</span>) {
<span class="hljs-comment">//iterate until there is no carry</span>
<span class="hljs-keyword">let</span> should_carry = <span class="hljs-literal">null</span>;
<span class="hljs-keyword">while</span>(should_carry !== <span class="hljs-number">0</span>){
should_carry = x & y;
x = x ^ y;
y = should_carry << <span class="hljs-number">1</span>;
}
<span class="hljs-keyword">return</span> x;
}
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">add</span>(<span class="hljs-number">27</span>,<span class="hljs-number">15</span>));
6+ hours across 72 lessons — Data Structures, Algorithms, Cryptography, Binary, Software Design, and Essential Unix Skills.