{"id":394,"date":"2015-12-21T08:32:41","date_gmt":"2015-12-21T08:32:41","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=394"},"modified":"2016-01-02T14:10:40","modified_gmt":"2016-01-02T14:10:40","slug":"shift-leftshift-and-rightshift-operator-in-java","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/shift-leftshift-and-rightshift-operator-in-java.html","title":{"rendered":"Shift: LeftShift And Rightshift Operator In JAVA With Example"},"content":{"rendered":"<p>Shift Operator is of two types Bitwise Left Shift And Right Shift operator. In the Bitwise shift operators, the result comes applying these operators on the bit values. Below we will discuss both the Shift operator.<\/p>\n<hr \/>\n<h4><strong><u>BITWISE LEFT SHIFT OPERATOR<\/u><\/strong><\/h4>\n<p>As you know very well, bit is a concept of binary number system. Here if you consider any number like 2 whose binary equivalent is 10 (i.e. one zero). And if on this particular number we apply left shift operator then it shift its value toward left as much number of bits as you defined and place zeros at the right most places. And this process goes on as much the size of that particular type is described.<\/p>\n<p>Example, if you consider the <strong>int <\/strong>type data this will move bits upto number 31 places. Because it is having total number of 32 bits in java. And if you consider <strong>long<\/strong> type data then it will move upto 63 places.<\/p>\n<p><strong>To move the number of bits is described by special notation \u2018&lt;&lt;\u2019. <\/strong><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>Bitwise Left Shift is assigned in the format of <strong>val&lt;&lt;num,<\/strong> where <strong>val<\/strong> represents the number to whom we have to find left shift and <strong>num <\/strong>specifies the number of bits as we want to shift.<\/p>\n<p><span style=\"color: #008000;\"><strong>Bitwise Left Shift Operator Example Program:<\/strong><\/span><\/p>\n<p>From program point of view you can understand this very easily. Here we are considering a simple int data type having 2 value to which we want just one left shit. As number 2\u2019s binary equivalent is 10 (i.e. one zero). And if its bits are simple move just one left shift then resultant value will be 100 (one zero zero) whose decimal is 4. And that\u2019s the truth that when input given in program code available and output result is 4 which is desired.<\/p>\n<p>In the code given here value to which we want to find the left shift operator is 2 and the number of places as we want to move is 1.<\/p>\n<pre>public class LeftShiftOperator {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t \r\n\t\t\t\tint a=2;\/\/10\r\n\t\t\t\tint i;\r\n\t\t\t\ti=a&lt;&lt;1;\/\/100\r\n\t\t\t\tSystem.out.println(\"the value of a before left shift is: \" +a);\r\n\t\t\t\tSystem.out.println(\"the value of a after applying left shift is: \" +i);\r\n\t\t\r\n\t}\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>the value of a before left shift is: 2\r\nthe value of a after applying left shift is: 4<\/pre>\n<hr \/>\n<h4><strong>BITWISE RIGHT SHIFT OPERATOR:<\/strong><\/h4>\n<p>Bitwise Right Shift Operator is applied on the number of bits. In this shift operator the Bit value for given number is simply move to right side and left most bits are replaced with the zeros.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> This is simply assigned as <strong>val&gt;&gt;num<\/strong>, where <strong>val<\/strong> is the particular value whose right shift is to be calculated and <strong>num<\/strong> indicate the particular number of bits shift movement.<\/p>\n<p><span style=\"color: #008000;\"><strong>Bitwise Right Shift Operator Example Program:<\/strong><\/span><\/p>\n<p>For example, if you want to apply right shift operator on integer value 2 for one shift only. Then result you will get will be 1. Understand how this comes.<\/p>\n<p>As you know simply binary number of 2 is 10 (i.e. one zero) and here we are applying a right shift operator for one position. So after the movement of one toward right side result will be 01\u00a0 (i.e. zero one) which is is equal to 1 only.<\/p>\n<p>Same result is calculated with the help of program. From code your intuition regarding this topic will be very clear. So just read out the program and then output for code is also given.<\/p>\n<pre>public class RightShiftOperator {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tint a=2;\r\n\t\tint i;\r\n\t\ti=a&gt;&gt;1;\r\n\t\tSystem.out.println(\"the value of a before right shfit is: \" +a);\r\n\t\tSystem.out.println(\"the value of a after applying right shfit is: \" +i);\r\n\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>the value of a before right shfit is: 2\r\nthe value of a after applying right shfit is: 1<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Shift Operator is of two types Bitwise Left Shift And Right Shift operator. In the Bitwise shift operators, the result comes applying these operators on the bit values. Below we will discuss both the Shift operator. BITWISE LEFT SHIFT OPERATOR As you know very well, bit is a concept of binary number system. Here if &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/shift-leftshift-and-rightshift-operator-in-java.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Shift: LeftShift And Rightshift 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-394","post","type-post","status-publish","format-standard","hentry","category-archieve","category-operator"],"psp_head":"<title>Shift: LeftShift And Rightshift Operator In JAVA With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on Shift Operator and its two two types Bitwise Left Shift And Right Shift operator. Understand the topic with easy example and code.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/shift-leftshift-and-rightshift-operator-in-java.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/394","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=394"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/394\/revisions"}],"predecessor-version":[{"id":1468,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/394\/revisions\/1468"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}