{"id":385,"date":"2015-12-21T07:10:23","date_gmt":"2015-12-21T07:10:23","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=385"},"modified":"2016-02-24T14:34:19","modified_gmt":"2016-02-24T14:34:19","slug":"bitwise-operator-java-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/bitwise-operator-java-example.html","title":{"rendered":"Bitwise Operator In JAVA With Example"},"content":{"rendered":"<p>As from the name Bitwise sound these operator performs the operation on bit value. And also you must have heard bit is smallest unit of memory. <em>This is represented by either 0 or 1 which means you have only one option to mark your answer<\/em>. Also you can understand this concept by an electrical switch which is connected with a bulb. There can only be two conditions, one is bulb can be ON and second is it can be OFF. This is exact representation of a single bit. The combination of 4 bits is called as nibble. <strong>Bitwise common Operators are: Bitwise AND (&amp;), Bitwise OR(|) and Bitwise exclusive OR(^):<\/strong><\/p>\n<hr \/>\n<h4><strong>Binary Number System:<\/strong><\/h4>\n<p>Before you understand Bitwise operator it is very important to understand Binary Number System. As binary number indicates that there can be only two values can be represented here either 0 or 1 which makes it a complete binary system. Below table describe the addition and multiplication of two Bit Value.<\/p>\n<table Border=\"1\">\n<tbody>\n<tr>\n<td width=\"159\"><strong>A<\/strong><\/td>\n<td width=\"159\"><strong>B<\/strong><\/td>\n<td width=\"159\"><strong>Addition<\/strong><\/td>\n<td width=\"160\"><strong>Multiplication<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"159\">0<\/td>\n<td width=\"159\">0<\/td>\n<td width=\"159\">0<\/td>\n<td width=\"160\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">0<\/td>\n<td width=\"159\">1<\/td>\n<td width=\"159\">1<\/td>\n<td width=\"160\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">1<\/td>\n<td width=\"159\">0<\/td>\n<td width=\"159\">1<\/td>\n<td width=\"160\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">1<\/td>\n<td width=\"159\">1<\/td>\n<td width=\"159\">10<\/td>\n<td width=\"160\">1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> There is also a difference between AND (&amp;) and logical AND (&amp;&amp;). Because logical AND (&amp;&amp;) can only result in the Boolean value i.e. it can be either 0 or 1. And in other words we can say it can be true or false, it can be recognized as on or off.<\/p>\n<hr \/>\n<h4><strong>Bitwise Different Operator:<\/strong><\/h4>\n<p>Now we discuss about different Bitwise operator: Bitwise AND (&amp;), Bitwise OR(|) and Bitwise exclusive OR(^).<\/p>\n<p><span style=\"text-decoration: underline; color: #008000;\"><strong>Bitwise And (&amp;):<\/strong><\/span><\/p>\n<p>In case of AND(&amp;), result depend on the value of bit value of comparative operands. In other words, only if both operands Bit value is 1 then only the result is 1. Below is the Bitwise And (&amp;) table:<\/p>\n<table Border=\"1\">\n<tbody>\n<tr>\n<td width=\"213\"><strong>A<\/strong><\/td>\n<td width=\"213\"><strong>B<\/strong><\/td>\n<td width=\"213\"><strong>AND(&amp;)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"213\">0<\/td>\n<td width=\"213\">0<\/td>\n<td width=\"213\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">0<\/td>\n<td width=\"213\">1<\/td>\n<td width=\"213\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">1<\/td>\n<td width=\"213\">0<\/td>\n<td width=\"213\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">1<\/td>\n<td width=\"213\">1<\/td>\n<td width=\"213\">1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Example, consider in the given program there are two variables considered a and b having two different values.<\/p>\n<p>a=3; b=4;<\/p>\n<p>if we calculate respective binary value of both the numbers we get:<\/p>\n<p>a=011;<\/p>\n<p>b=100;<\/p>\n<p>As AND work on bit values and if we calculate the result for this (please refer &amp; table):<\/p>\n<p>a&amp;b=000;<\/p>\n<p>Now if we calculate the number value of result bit values(000) is equal to 0. <strong>Because in this case there bit value respective to other operand bit values are different and hence the result generated is 000<\/strong>. And the same result <strong>(0) <\/strong>is produced by the program.<\/p>\n<p><span style=\"text-decoration: underline; color: #008000;\"><strong>Bitwise Inclusive OR (|)<\/strong><\/span><\/p>\n<p>Now for consideration of Bitwise OR (|) firstly understand the basic behind it. This also works on bit value. In this the logic result 1 if any one of bit value is 1. The result will be zero only if both the values are zero.<\/p>\n<p>It can be more cleared from the table shown below:<\/p>\n<table Border=\"1\">\n<tbody>\n<tr>\n<td width=\"213\"><strong>A<\/strong><\/td>\n<td width=\"213\"><strong>B<\/strong><\/td>\n<td width=\"213\"><strong>Inclusive OR (|)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"213\">0<\/td>\n<td width=\"213\">0<\/td>\n<td width=\"213\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">0<\/td>\n<td width=\"213\">1<\/td>\n<td width=\"213\">1<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">1<\/td>\n<td width=\"213\">0<\/td>\n<td width=\"213\">1<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">1<\/td>\n<td width=\"213\">1<\/td>\n<td width=\"213\">1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>From the code view of point there are two variable considered<br \/>\nA=3; \/\/ 011<br \/>\nB=4 \/\/100<br \/>\nA|B=7 \/\/111<\/p>\n<p>Hence the result obtained from these variables is given as 7. Because here we apply inclusive OR (|) operator in it.<\/p>\n<p><span style=\"text-decoration: underline; color: #008000;\"><strong>Bitwise Exclusive OR (^)<\/strong><\/span><\/p>\n<p>So in case of <strong>exclusive OR (^) <\/strong>the result value is only one when both the bit values are different means when one is 0 and other value is 1 and also if first one is 1 and second one 0, in both the cases the result value will be 1. And if both are having same value like 0 and 1 then result will be zero (0).<\/p>\n<table Border=\"1\">\n<tbody>\n<tr>\n<td width=\"213\"><strong>A<\/strong><\/td>\n<td width=\"213\"><strong>B<\/strong><\/td>\n<td width=\"213\"><strong>Exclusive OR<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"213\">0<\/td>\n<td width=\"213\">0<\/td>\n<td width=\"213\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">0<\/td>\n<td width=\"213\">1<\/td>\n<td width=\"213\">1<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">1<\/td>\n<td width=\"213\">0<\/td>\n<td width=\"213\">1<\/td>\n<\/tr>\n<tr>\n<td width=\"213\">1<\/td>\n<td width=\"213\">1<\/td>\n<td width=\"213\">0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As from program there are two variables<\/p>\n<p>a=3;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/011<\/p>\n<p>b=4;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/100<\/p>\n<p>a^b=7;\u00a0 \/\/111<\/p>\n<p>Here a is having bit values as 011 and b having 100 as bit values in binary number system. And in the resultant if take it its <strong>exclusive OR(^)<\/strong> all bit values are different in respect to each other so the resultant comes as 111 which is count to be as 7 in decimal number system. And hence the output from the respective code of program.<\/p>\n<hr \/>\n<h4><strong>Bitwise Operator Example in JAVA<\/strong><\/h4>\n<p>Now lets create a program in JAVA using all three Bitwise AND (&amp;), Bitwise OR(|), and Bitwise exclusive OR(^) operator.<\/p>\n<pre>public class BitwiseExample {\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String[] args) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int a=3; \/\/binary value is 011\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int b=4; \/\/binary value is 100\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int e=a&amp;b; \/\/ result value is 000 which is 0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int f=a|b;\/\/ result value is 111 which is 7\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int g=a^b;\/\/ result value is 111 which is 7\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"The value of a &amp; b(AND) is: \" + e);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"The value of a | b(INCLUSIVE OR) is: \" + f);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"The value of a ^ b(EXCLUSIVE OR) is: \"+ g);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>The value of a &amp; b(AND) is: 0\r\nThe value of a | b(INCLUSIVE OR) is: 7\r\nThe value of a ^ b(EXCLUSIVE OR) is: 7<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As from the name Bitwise sound these operator performs the operation on bit value. And also you must have heard bit is smallest unit of memory. This is represented by either 0 or 1 which means you have only one option to mark your answer. Also you can understand this concept by an electrical switch &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/bitwise-operator-java-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Bitwise Operator In JAVA With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,66],"tags":[],"class_list":["post-385","post","type-post","status-publish","format-standard","hentry","category-archieve","category-operator"],"psp_head":"<title>Bitwise Operator In JAVA With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on Bitwise Operator and its types: Bitwise AND (&amp;), Bitwise OR(|) and Bitwise exclusive OR(^) with example in JAVA. Also learn about Binary Number system.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/bitwise-operator-java-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=385"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/385\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}