<?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: dvloznov</title>
    <description>The latest articles on DEV Community by dvloznov (@dvloznov).</description>
    <link>https://dev.to/dvloznov</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%2F1038468%2F6d63f927-555f-4c95-b3f4-800500facab1.png</url>
      <title>DEV Community: dvloznov</title>
      <link>https://dev.to/dvloznov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dvloznov"/>
    <language>en</language>
    <item>
      <title>Simple arithmetics with bitwise operators in Java</title>
      <dc:creator>dvloznov</dc:creator>
      <pubDate>Sun, 05 Mar 2023 15:17:55 +0000</pubDate>
      <link>https://dev.to/dvloznov/simple-arithmetics-with-bitwise-operators-in-java-4nni</link>
      <guid>https://dev.to/dvloznov/simple-arithmetics-with-bitwise-operators-in-java-4nni</guid>
      <description>&lt;p&gt;Bitwise operations is one of the fundamental concepts of computer science. While they can be utilised for various scenarios, they are not particularly common in real-world applications. However, understanding bitwise operators can be valuable for interviews and general knowledge purposes.&lt;/p&gt;

&lt;p&gt;Like most other languages, Java contains a subset of bitwise operators. The easiest way to get some experience with them is to try and implement basic arithmetic functions on integers using only bitwise operators. In Java, integers are stored in memory as binary values in a fixed number of bits, depending on the integer type.  For example, suppose we declare an integer variable &lt;code&gt;num&lt;/code&gt; of type &lt;code&gt;byte&lt;/code&gt; and assign it the value &lt;code&gt;5&lt;/code&gt;. Java converts the decimal value &lt;code&gt;5&lt;/code&gt; to its binary representation &lt;code&gt;101&lt;/code&gt; and stores it in the memory location of size 8 bits reserved for &lt;code&gt;num&lt;/code&gt; as &lt;code&gt;00000101&lt;/code&gt;. The actual storage of integers in memory is handled by the computer's processor, which has registers and memory units designed to store and manipulate binary values.&lt;/p&gt;

&lt;p&gt;Let’s start by looking into the addition method and how it may be performed on two integers on a bit level. For simplicity, we’ll assume that all of the values are positive, and later in the article, I’ll show how Java deals with negative numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Addition
&lt;/h2&gt;

&lt;p&gt;Before jumping into implementation, let’s discuss how it works on paper. Assume we have two numbers: &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;7&lt;/code&gt;. Their binary representation would be &lt;code&gt;101&lt;/code&gt; and &lt;code&gt;111&lt;/code&gt;, respectively.&lt;br&gt;
If we apply school math, adding those two numbers would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ------------------------
| carry  | 1 | 1 | 1 | 0 |
 ------------------------
| a      | 0 | 1 | 0 | 1 |
| b      | 0 | 1 | 1 | 1 |
 ------------------------
| result | 1 | 1 | 0 | 0 |
 ------------------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we perform this by hand, we are calculating each digit by summing up two operands and &lt;code&gt;carry&lt;/code&gt; value. To put it into code, we must split these operations into different steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Calculate intermediate result ignoring &lt;code&gt;carry&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;Calculate &lt;code&gt;carry&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;carry&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt;, the &lt;code&gt;intermediate result&lt;/code&gt; is the final value, and we should return it&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;carry&lt;/code&gt; is not &lt;code&gt;0&lt;/code&gt;, calculate the result of &lt;code&gt;intermediate result&lt;/code&gt; and &lt;code&gt;carry&lt;/code&gt; (go to step 1)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If we use this algorithm to calculate &lt;code&gt;5 + 7&lt;/code&gt;, we would get the following results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;carry:  0101 -&amp;gt; 1010 -&amp;gt; 0100 -&amp;gt; 0000
result: 0111 -&amp;gt; 0010 -&amp;gt; 1000 -&amp;gt; 1100

result = 1100 (12)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let discuss, how do we calculate &lt;code&gt;intermediate result&lt;/code&gt; with bitwise operations. To do so, let's create a logical table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ----------------
| a | b | result |
 ----------------
| 0 | 0 |   0    |
| 0 | 1 |   1    |
| 1 | 0 |   1    |
| 1 | 1 |   0    |
 ----------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is precisely, what bitwise XOR &lt;code&gt;^&lt;/code&gt;  is doing. Thus, the &lt;code&gt;intermediate result&lt;/code&gt; of integers a and b would be &lt;code&gt;a ^ b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now check how we calculate the &lt;code&gt;carry&lt;/code&gt; value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ----------------
| a | b | carry  |
 ----------------
| 0 | 0 |   0    |
| 0 | 1 |   0    |
| 1 | 0 |   0    |
| 1 | 1 |   1    |
 ----------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a logical &lt;code&gt;AND&lt;/code&gt;. One small caveat here is when we are calculating &lt;code&gt;carry&lt;/code&gt; value, we move the result one position to the left. To do this in code, we could use &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; operator, which does precisely that. The &lt;code&gt;carry&lt;/code&gt; result would be &lt;code&gt;(a &amp;amp; b) &amp;lt;&amp;lt; 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s put all of this into the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static int add(int a, int b) {
    int carry = (a &amp;amp; b) &amp;lt;&amp;lt; 1;
    int sum = a ^ b;

    while (carry != 0) {
        a = sum;
        b = carry;
        carry = (a &amp;amp; b) &amp;lt;&amp;lt; 1;
        sum = a ^ b;
    }
    return sum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A slightly clear rewrite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static int add(int a, int b) {
    while (b != 0) {
        int carry = (a &amp;amp; b) &amp;lt;&amp;lt; 1;
        a = a ^ b;
        b = carry;
    }
    return a;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Substruction
&lt;/h2&gt;

&lt;p&gt;Let’s repeat the process we did for addition method and check if we can come up with similar algorithm for substruction. When substructing two numbers &lt;code&gt;a - b&lt;/code&gt;, we’ll assume both are non-negative and &lt;code&gt;a &amp;gt; b&lt;/code&gt;. Later we will check if we can drop this condition.&lt;/p&gt;

&lt;p&gt;Let’s first review how to do the substruction of numbers &lt;code&gt;12&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ------------------------
| borrow | 1 | 1 | 1 | 0 |
 ------------------------
| a      | 1 | 1 | 0 | 0 |
| b      | 0 | 1 | 0 | 1 |
 ------------------------
| result | 0 | 1 | 1 | 1 |
 ------------------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The algorithm that we can implement here would look similar to addition:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Calculate intermediate result ignoring &lt;code&gt;borrow&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;Calculate &lt;code&gt;borrow&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;borrow&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt;, return the result&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;borrow&lt;/code&gt; is not &lt;code&gt;0&lt;/code&gt;, calculate the substruction between the &lt;code&gt;borrow&lt;/code&gt; and &lt;code&gt;intermediate result&lt;/code&gt; (go to step 1)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If we use this algorithm to calculate &lt;code&gt;12 - 5&lt;/code&gt;, we would get the following results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;borrow: 0101 -&amp;gt; 0010 -&amp;gt; 0100 -&amp;gt; 1000 -&amp;gt; 0000
result: 1100 -&amp;gt; 1001 -&amp;gt; 1011 -&amp;gt; 1111 -&amp;gt; 0111

result = 0111 (7)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s check, what bitwise operators we can use, to implement this logic. For intermediate result, it should correspond to this logical table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ----------------
| a | b | result |
 ----------------
| 0 | 0 |   0    |
| 0 | 1 |   1    |
| 1 | 0 |   1    |
| 1 | 1 |   0    |
 ----------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a &lt;code&gt;XOR&lt;/code&gt; (^) operator, similar to what we did in addition. For calculating &lt;code&gt;borrow&lt;/code&gt; value, it becomes more tricky. The logical table looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ----------------
| a | b | borrow |
 ----------------
| 0 | 0 |   0    |
| 0 | 1 |   1    |
| 1 | 0 |   0    |
| 1 | 1 |   0    |
 ----------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no binary operator that would do it out of the box. At the same time, if we invert the first column, we would get the table&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ----------------
| a | b | borrow |
 ----------------
| 1 | 0 |   0    |
| 1 | 1 |   1    |
| 0 | 0 |   0    |
| 0 | 1 |   0    |
 ----------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the logical &lt;code&gt;AND&lt;/code&gt; (&amp;amp;) operator. That means that for calculating the &lt;code&gt;borrow&lt;/code&gt; value, we need to invert the first operand and then apply logical &lt;code&gt;AND&lt;/code&gt;: &lt;code&gt;(~a) &amp;amp; b.&lt;/code&gt; Similar to addition, we need to shift the bits to the left: &lt;code&gt;borrow = (~a) &amp;amp; b &amp;lt;&amp;lt; 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Putting it into the code, we would get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static int substruct(int a, int b) {
    while (b != 0) {
        int borrow = ((~a) &amp;amp; b) &amp;lt;&amp;lt; 1;
        a = a ^ b;
        b = borrow;
    }
    return a;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What about negative numbers
&lt;/h2&gt;

&lt;p&gt;Java uses two's complement representation to store signed integers. In the two's complement representation, the leftmost bit (most significant bit) is reserved to represent the sign of the number. If the leftmost bit is 0, the number is positive; if it is 1, the number is negative.&lt;/p&gt;

&lt;p&gt;To obtain the two's complement representation of a negative number, we need to follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Represent the absolute value of the number in binary format (including leading zeros if necessary)&lt;/li&gt;
&lt;li&gt;Invert all the bits (change 0s to 1s and vice versa)&lt;/li&gt;
&lt;li&gt;Add 1 to the result from step 2&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, to obtain the two's complement representation of &lt;code&gt;-3&lt;/code&gt;, we can follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Represent the absolute value of the number &lt;code&gt;3&lt;/code&gt; in binary format: &lt;code&gt;00000011&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Invert all the bits: &lt;code&gt;11111100&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add 1: &lt;code&gt;11111101&lt;/code&gt;
Therefore, the two's complement representation of &lt;code&gt;-3&lt;/code&gt; is &lt;code&gt;11111101&lt;/code&gt; in binary format.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The advantage of using two's complement representation is that it allows addition and subtraction of signed integers to be performed using the same binary operations as for unsigned integers. That means, that the code that we wrote above, would work just fine with negative numbers as well&lt;/p&gt;

&lt;p&gt;Let’s check, how negative numbers would work for our addition algorithm by summing 5 and -3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;result: 00000101 -&amp;gt; 11111000 -&amp;gt; 11110010 -&amp;gt; 11100010 -&amp;gt; 
carry:  11111101 -&amp;gt; 00001010 -&amp;gt; 00010000 -&amp;gt; 00100000 -&amp;gt; 

-&amp;gt; 11000010 -&amp;gt; 10000010 -&amp;gt; 00000010
-&amp;gt; 01000000 -&amp;gt; 10000000 -&amp;gt; 00000000

result -&amp;gt; 10 (2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s check the same for substruction, for example &lt;code&gt;5 - (-3)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;result: 00000101 -&amp;gt; 11111000 -&amp;gt; 00001000
borrow: 11111101 -&amp;gt; 11110000 -&amp;gt; 00000000

result -&amp;gt; 1000 (8)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, despite a non-obvious approach for representing negative numbers, it significantly simplifies arithmetics.&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
