{"id":1537,"date":"2022-08-10T20:25:09","date_gmt":"2022-08-10T20:25:09","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=1537"},"modified":"2022-08-19T11:59:25","modified_gmt":"2022-08-19T11:59:25","slug":"bitwise-operators-in-java","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/bitwise-operators-in-java\/","title":{"rendered":"Bitwise Operators in Java"},"content":{"rendered":"<p style=\"text-align: center;\"><strong>Bitwise Operators in Java<\/strong><\/p>\n\n\n<p>This type of operator is used to perform operations on bits. Here are some operations that we can, and are going to perform on bits &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&amp; \u2192 this is the bitwise AND operator.<\/li><li>| \u2192 this is the bitwise OR operator.<\/li><li>^ \u2192 this is a bitwise XOR operator.<\/li><li>~ \u2192 this is a bitwise complement operator.<\/li><li>&lt;&lt; \u2192 this is a bitwise left shift.<\/li><li>&gt;&gt; \u2192 This is a bitwise right shift.<\/li><\/ul>\n\n\n\n<p>So, let&#8217;s understand all these bitwise operators.<br>First, let&#8217;s understand the Bitwise &amp; (AND) operator. This somewhat resembles the logical AND operator, but not completely. Here, we are working with bits. Here is the truth table for &amp; the operator &#8211;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>A<\/td><td>B<\/td><td>A&amp;B<\/td><\/tr><tr><td><strong>0<\/strong><\/td><td>0<\/td><td>0<\/td><\/tr><tr><td><strong>0<\/strong><\/td><td>1<\/td><td>0<\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>0<\/td><td>0<\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>1<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So, from the above table, we understand that for the output to be 1, all the inputs should be 1. Any input that is 0, will result in a 0 output as we can see in the above table. We can consider that 0 is false and 1 is true.<\/p>\n\n\n\n<p>So, if we do 9&amp;7 for example, then the 9 will be converted to binary and the 7 will be converted to binary, and then the AND operation according to the above table and the output comes out to be 1. Here is the answer to the above question &#8211;<\/p>\n\n\n\n<p class=\"has-text-align-center\">1001<br>0111<br>&#8212;&#8212;&#8212;&#8212;-<br>0001<\/p>\n\n\n\n<p>So, if we convert 0001 to decimal, it comes out to be 1, which is the answer. So this now becomes self-explanatory for why is 9&amp;7 are 1.<\/p>\n\n\n\n<p>If you are still confused about the output, you need to have a look at the general AND operation related to the bits. You can understand the general concept first, which is pretty simple, and then you will get familiar with what we are doing.<\/p>\n\n\n\n<p>Now let&#8217;s understand the OR operator. Here, we have to have at least one true input, or let&#8217;s say at least one 1 out of all the inputs. Have a look at the below table, to understand the bitwise OR operator.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>A<\/td><td>B<\/td><td>A|B<\/td><\/tr><tr><td><strong>0<\/strong><\/td><td>0<\/td><td>0<\/td><\/tr><tr><td><strong>0<\/strong><\/td><td>1<\/td><td>1<\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>0<\/td><td>1<\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>1<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In this situation, you can see that we at least need one 1 for the output to be 1. This may seem confusing, but can resemble this quite to the logical OR operator, but not completely, since we are working with bits here.<\/p>\n\n\n\n<p>So, let&#8217;s have an example so that we can better understand what is going on in the bitwise OR operator. Let&#8217;s say that we have two numbers again, 9 and 7. now, if we try performing the bitwise OR operation on them, it would be something like this \u2013 first, both the numbers would be converted to binary, and the OR operation would be done.<\/p>\n\n\n\n<p class=\"has-text-align-center\">1001<br>0111<br>&#8212;&#8212;&#8212;&#8212;-<br>1111<\/p>\n\n\n\n<p>As you can see, we got the answer as 1111, and if you try to attempt the same thing in the program, we get the output as 15, which is 1111 in binary.<\/p>\n\n\n\n<p>The next symbol, which is defining the XOR operation over here, is the \u201c^\u201d. Well, if you are already familiar with the XOR operation, it is well and good, otherwise, we are going to get to the point right now.<\/p>\n\n\n\n<p>When it comes to XOR operation, we get the output bit as 1, if there is an odd number of true (1). Have a look at the below table, to understand the XOR operation.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>A<\/td><td>B<\/td><td>A^B<\/td><\/tr><tr><td><strong>0<\/strong><\/td><td>0<\/td><td>0<\/td><\/tr><tr><td><strong>0<\/strong><\/td><td>1<\/td><td>1<\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>0<\/td><td>1<\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>1<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So, if we try to perform the XOR operation between two inputs, let&#8217;s say 9 and 12, the output comes out to be 5. Here is the demonstration &#8211;<\/p>\n\n\n\n<p class=\"has-text-align-center\">1001<br>1100<\/p>\n\n\n\n<p class=\"has-text-align-center\">0101<\/p>\n\n\n\n<p class=\"has-text-align-left\">This 0101 is 5.<\/p>\n\n\n\n<p class=\"has-text-align-left\">If we come to the left shift or right shift things, we are just shifting the bit to left, or right, by the specified number of bits. For example, we can say that shift the bits to left by 2 bits. Let&#8217;s see what happens if we shift the bits by a specified number of bits to the left.<\/p>\n\n\n\n<p class=\"has-text-align-left\">This is how the left-shift operator is symbolized \u2192 &lt;&lt;<br>Again, we can do something like this \u2192 10 &lt;&lt; 2.<\/p>\n\n\n\n<p class=\"has-text-align-left\">This simply means that we are saying that bits of the value 10(which is a decimal value), should be left-shifted by 2 bits. Let&#8217;s see how this gets going &#8211;<\/p>\n\n\n\n<p class=\"has-text-align-left\">Firstly, let&#8217;s consider the binary for the decimal number 10, which comes out to be 00001010.<\/p>\n\n\n\n<p class=\"has-text-align-left\">Now, if we are going to shift the bits to the left, by 2 bits, it becomes something like this \u2192 00101000.<\/p>\n\n\n\n<p class=\"has-text-align-left\">So, this becomes 40 in the decimal system. So, when we are shifting 2 bits left for the data 10, we get 40 as output.<\/p>\n\n\n\n<p class=\"has-text-align-left\">On the other hand, if we are talking about the right shift, it simply means shifting the bits to the right by the specified number of bits. Let&#8217;s have a look at this too \u2192<\/p>\n\n\n\n<p class=\"has-text-align-left\">Firstly, the right shift operator is symbolized as \u2192 &gt;&gt;<br>Now, if we say, for example, 10&gt;&gt;1, this simply means that the bits for the decimal data 10, should be shifted to the right by 1 bit.<\/p>\n\n\n\n<p class=\"has-text-align-left\">So, first, let&#8217;s consider the binary for the decimal number 10, which comes out to be 00001010 again.<\/p>\n\n\n\n<p class=\"has-text-align-left\">Now, if we shift the bits by 1 to the right, it becomes something like this \u2192 00000101.<\/p>\n\n\n\n<p class=\"has-text-align-left\">So, this becomes 5 in the decimal system. So, when we are shifting 1 bit right for the data 10, the output comes out to be 5.<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">public class Main{<br>public static void main(String[] args) {<br>System.out.println(9&amp;7);<br>System.out.println(9|7);<br>System.out.println(9^12);<br>System.out.println(~9);<br>System.out.println(10&lt;&lt;2);<br>System.out.println(10&gt;&gt;1);<br>}<br>}<\/p>\n\n\n\n<p>If we try to execute the above java program, we get the relevant outputs accordingly. As the name of the operators says, we use these operators, when we are working with some kind of bit-by-bit operations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bitwise Operators in Java This type of operator is used to perform operations on bits. Here are some operations that we can, and are going to perform on bits &#8211; &amp; \u2192 this is the bitwise AND operator. | \u2192 this is the bitwise OR operator. ^ \u2192 this is a bitwise XOR operator. ~ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[12,11],"class_list":{"0":"post-1537","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-java-tutorial","7":"tag-java","8":"tag-java-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=1537"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1537\/revisions"}],"predecessor-version":[{"id":2703,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1537\/revisions\/2703"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=1537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=1537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=1537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}