<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bit manipulation &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/bit-manipulation/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Fri, 17 Jun 2022 21:51:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Bit manipulation operators in Java</title>
		<link>https://codepills.com/bit-manipulation-operators-in-java/</link>
					<comments>https://codepills.com/bit-manipulation-operators-in-java/#comments</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Sat, 12 Dec 2020 09:11:51 +0000</pubDate>
				<category><![CDATA[Language basics]]></category>
		<category><![CDATA[bit manipulation]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java SE]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1099</guid>

					<description><![CDATA[Java offers a possibility for bit manipulation with bit operators in its code. This article will go through all the operators with real-life <a href="https://codepills.com/bit-manipulation-operators-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>As in many programming languages, Java offers a possibility for bit manipulation with bit operators. While the <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.htm" title="Bitwise and Bit Shift Operators" target="_blank" rel="nofollow noopener">official documentation</a> is kind of sparse, this article will give you more information about individual operators.</p>
<p><span id="more-1099"></span></p>
<p>We can divide bit manipulating operators into two categories categorized by the kind of operation operators do. We have <strong>bitwise operators</strong> such as AND, OR, XOR and bitwise complement; and <strong>shift operators</strong> such as Signed right shift, Unsigned right shift and Unsigned left shift operators. Let&#8217;s take a closer look at individual operators with real-life examples.</p>
<h2>Bitwise operators</h2>
<p>Bitwise operators are used to perform individual bits manipulation. However, we can also use them with integral types such as char, short, int, etc.</p>
<h3>Bitwise operator AND &#8211; &#038;</h3>
<p><b>AND</b> operator is binary operator, denoted by <b>&#8216;&#038;&#8217;</b> character in Java. It returns bit result of AND operation on input variables.</p>
<p>Let&#8217;s take a look on AND table and explain the OR bit operator on example below:</p>
<table>
<thead>
<tr>
<th scope="col">A</th>
<th scope="col">B</th>
<th scope="col">A &#038; B</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<pre><code class="language-bash">    A = 3 = 0011 [Binary]
    B = 5 = 0101 [Binary]

    Bitwise AND Operation of 3 and 5
    0011 &
    0101
    ________
    0001  = 1 [Decimal]</code></pre>
<h4>&#038; assignment trick</h4>
<p><b>&#038;</b> can be in Java also combined with assignment operator to provide shorthand assignment e.g.:</p>
<pre><code class="language-bash">    a = a&b
    a &= b;</code></pre>
<h3>Bitwise operator OR &#8211; |</h3>
<p><b>OR</b> operator is binary operator, denoted by <b>&#8216;|&#8217;</b> character in Java. It returns bit result of OR operation on input variables.</p>
<p>Let&#8217;s take a look on OR table and explain the OR bit operator on example below:</p>
<table>
<thead>
<tr>
<th scope="col">A</th>
<th scope="col">B</th>
<th scope="col">A | B</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<pre><code class="language-bash">    A = 3 = 0011 [Binary]
    B = 5 = 0101 [Binary]

    Bitwise OR Operation of 3 and 5
    0011 |
    0101
    ________
    0111  = 7 [Decimal]</code></pre>
<h3>Bitwise operator XOR &#8211; ^</h3>
<p><b>XOR</b> operator is binary operator, denoted by <b>&#8216;^&#8217;</b> character in Java. It returns bit result of XOR operation on input variables.</p>
<p>Let&#8217;s take a look on XOR table and explain the OR bit operator on example below:</p>
<table>
<thead>
<tr>
<th scope="col">A</th>
<th scope="col">B</th>
<th scope="col">A ^ B</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
<pre><code class="language-bash">    A = 3 = 0011 [Binary]
    B = 5 = 0101 [Binary]

    Bitwise XOR Operation of 3 and 5
    0011 ^
    0101
    ________
    0110  = 6 [Decimal]</code></pre>
<h3>Bitwise complement &#8211; ~</h3>
<p>This is unary operator, denoted by <b>&#8216;~&#8217;</b> character in Java. It means it returns one&#8217;s complement representation of the input value. It means flipping bits on the opposite value where 1 is 0 and 0 is 1.</p>
<p>Let&#8217;s take a look on XOR table and explain the OR bit operator on example below:</p>
<pre><code class="language-bash">    A = 3 = 0011 [Binary]

    Bitwise OR Operation of 3 and 5
    ~0011
    ________
    1100  = 12 [Decimal]</code></pre>
<h2>Shift operators</h2>
<p>Shift operators are used to shift the bits for a number of positions to the left or right. Due to bits movement, shifting bits multiply or divide the number by two, respectively.</p>
<p>Generally, shift operators can be used when we have to multiply or divide a number by two.</p>
<h3>Signed right shift operator &#8211; >></h3>
<p>Signed right operator is denoted by <b>&#8216;>>&#8217;</b> characters in Java. The signed right operator shifts the bits for the number of positions <b>to the right and fills the blank bit positions with 0</b> as a result.</p>
<p>The leftmost bit depends on the sign of the initial number. <b>With signed right shift we preserve the sign bit</b>.</p>
<p>Similarly, shifting bits right affects number as if it would be divided by the power of two.</p>
<pre><code class="language-bash">      5 => 0 0101>>2 =  1 = 0 0001
    -10 => 1 0110>>2 = -3 = 1111 1111 1111 1111 1111 1111 1111 1101</code></pre>
<h3>Unsigned right shift operator &#8211; >>></h3>
<p>Unsigned right operator is denoted by <b>&#8216;>>>&#8217;</b> characters in Java. The unsigned right operator shifts the bits for the number of positions to the right and fills the blank bit positions with 0s.</p>
<p>The leftmost bit is set to 0. Difference between signed and unsigned right shift operator is that unsigned-shift operator <b>(>>>)</b> will insert 0 while signed operator <b>(>>)</b> will extend the sign bit. <b><u>With unsigned right shift we do not preserve the sign bit</u></b>.</p>
<pre><code class="language-bash">      5 => 0 0101>>>2 = 1          = 0 0001
        // Similar to 5/(2^2)
      8 => 0 1000>>>2 = 2          = 0 0010
        // Similar to 8/(2^2)
    -10 => 1 0110>>>2 = 1073741821 = 11 1111 1111 1111 1111 1111 1111 1101 = 0011 1111 1111 1111 1111 1111 1111 1101
        // !!! Remember unsigned right shift operator for negative numbers - first 2 bits become 0 (and do not display in binary string)</code></pre>
<h3>Signed left shift operator &#8211; <<</h3>
<p>Signed left operator is denoted by <b>&#8216;<<<'</b> characters in Java. The signed left operator shifts the bits for the number of positions to the left and fills the blank bit positions with 0s.</p>
<p>The leftmost bit depends on the sign of the initial number. <b>With signed left shift we preserve the sign bit</b>.</p>
<p>Similarly, shifting bits left affects the number as if it would be <b>multiplied by the power of two</b>.</p>
<pre><code class="language-bash">      5 => 0 0101<<2 =  20 = 10100
            // Similar to 5*(2^2)
    -10 => 1 0110<<2 = -40 = 1111 1111 1111 1111 1111 1111 1101 1000
            // Similar to -10*(2^2)</code></pre>
<h3>Unsigned left shift operator - <<<</h3>
<p>Unlike unsigned light shift, <b>there is no "<<<" operator in Java</b>. The logical <b>(<<)</b> and arithmetic left-shift <b>(<<<)</b> operations are identical.</p>
<h2>Example code</h2>
<pre><code class="language-java">public class Test {
     public static void main(String []args) {

        int a = 3;
        int b = 5;
        int c = -7;
        int d = -10;

        // For testing of bitwise and assigning operators use
        // Integer.toBinaryString();


        // Bitwise AND
        // 0 0011 & 0 0101 = 0 0001 = 1
        System.out.println("A & B = " + (a & b));
        // 1 0111 & 1 1010 = 1 0000 = -16 = 1111 1111 1111 1111 1111 1111 1111 0000
        System.out.println("C & D = " + (c & d));


        // Bitwise OR
        // 0 0011 | 0 0101 = 0 0111 = 7
        System.out.println("A | B = " + (a | b));
        // 1 0111 | 1 1010 = 1 1111 = -1 = 1111 1111 1111 1111 1111 1111 1111 1111
        System.out.println("C | D = " + (c | d));


        // Bitwise XOR
        // 3 => 0 0011 ^ 0 0101 = 0 0110 = 6
        System.out.println("A ^ B = " + (a ^ b));
        // -7 => 1 0111 ^ 1 1010 = 0 1111 = 15
        System.out.println("C ^ D = " + (c ^ d));


        // Bitwise complement
        // ~0 0011 = -4 = 1111 1111 1111 1111 1111 1111 1111 1100
        System.out.println("~A = " + ~a);
        // 1 0111 = 6 = 110
        // will give 2's complement of 1010 = -6
        System.out.println("~C = " + ~c);


        // Signed right shift operator
        // 5 => 0 0101>>2 = 1 = 0 0001
        System.out.println("B>>2 = " + (b >> 2));
        // -10 => 1 0110>>2 = -3 = 1111 1111 1111 1111 1111 1111 1111 1101
        System.out.println("D>>2 = " + (d >> 2));


        // Unsigned right shift operator
        // 5 => 0 0101>>>2 = 1 = 0 0001
        System.out.println("B>>>2 = " + (b >>> 2));
        // -10 => 1 0110>>>2 = 1073741821 = 11 1111 1111 1111 1111 1111 1111 1101
        System.out.println("D>>>2 = " + d >>> 2);
        // !!! Important observation for negative numbers - first 2 bits become 0
        // (and do not display in binary string)


        // 5 => 0 0101<<2 = 20 = 10100
        // Similar to 5*(2^2)
        System.out.println("C<<2 = " + (b << 2));
        // -10 => 1 0110<<2 = -40 = 1111 1111 1111 1111 1111 1111 1101 1000
        // Similar to -10*(2^2)
        System.out.println("D<<2 = " + (d << 2));
     }
}</code></pre>
<p><b>Note:</b> Just to help with some bit transformation, here is a bit integer number transformation table to binary 4-bit and 8-bit representation.</p>
<table>
<thead>
<tr>
<th scope="col">Decadical</th>
<th scope="col">16</th>
<th scope="col">255</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0000</td>
<td>00000000</td>
</tr>
<tr>
<td>1</td>
<td>0001</td>
<td>00000001</td>
</tr>
<tr>
<td>2</td>
<td>0010</td>
<td>00000010</td>
</tr>
<tr>
<td>3</td>
<td>0011</td>
<td>00000011</td>
</tr>
<tr>
<td>4</td>
<td>0100</td>
<td>000000100</td>
</tr>
<tr>
<td>5</td>
<td>0101</td>
<td>00000101</td>
</tr>
<tr>
<td>6</td>
<td>0110</td>
<td>00000110</td>
</tr>
<tr>
<td>7</td>
<td>0111</td>
<td>00000111</td>
</tr>
<tr>
<td>8</td>
<td>1000</td>
<td>00001000</td>
</tr>
<tr>
<td>9</td>
<td>1001</td>
<td>00001001</td>
</tr>
<tr>
<td>10</td>
<td>1010</td>
<td>00001010</td>
</tr>
<tr>
<td>11</td>
<td>1011</td>
<td>00001011</td>
</tr>
<tr>
<td>12</td>
<td>1100</td>
<td>00001100</td>
</tr>
<tr>
<td>13</td>
<td>1101</td>
<td>00001101</td>
</tr>
<tr>
<td>14</td>
<td>1110</td>
<td>00001110</td>
</tr>
<tr>
<td>15</td>
<td>1111</td>
<td>00001111</td>
</tr>
<tr>
<td>16</td>
<td>----</td>
<td>00010000</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>254</td>
<td>----</td>
<td>11111110</td>
</tr>
<tr>
<td>255</td>
<td>----</td>
<td>11111111</td>
</tr>
</tbody>
</table>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/bit-manipulation-operators-in-java/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
