<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5718802842428101241</id><updated>2024-03-13T05:09:14.009-07:00</updated><category term="data structure"/><category term="asymptotic notation"/><category term="asymptotic analysis"/><category term="big-Oh"/><category term="core java"/><category term="Basic Concept"/><category term="Big-Omega notation"/><category term="Big-Theta notation"/><category term="Core Java Interview Q&amp;A"/><category term="Ideal assumption"/><category term="analysis"/><category term="asymptotic"/><category term="big o notation"/><category term="binary addition"/><category term="bit manipulation"/><category term="guest post"/><category term="java"/><category term="numeric promotion"/><category term="ruby"/><category term="shift operators"/><title type='text'>CS.Techruto</title><subtitle type='html'>Free college notes on Computer Science Engineering and Coding. Its a last minute study notes for everybody. </subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://cs.techruto.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5718802842428101241/posts/default?max-results=3'/><link rel='alternate' type='text/html' href='https://cs.techruto.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='https://www.blogger.com/feeds/5718802842428101241/posts/default?start-index=4&amp;max-results=3'/><author><name>Sasuke Naruto</name><uri>http://www.blogger.com/profile/12066467749580493805</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiodJZB1LFPXE0moMcmctZiB2zeNCtF1R5W7d-frodSshVl5P5D8OpxFt6iPmy52-hl1INiTYq7zlckUiSBtk4ajUftMnUdCNNTlWF6-oZmPIjD0yJw5odCfy-012_MA/s220/pp.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>3</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5718802842428101241.post-4956906352979679181</id><published>2017-10-07T23:09:00.001-07:00</published><updated>2017-10-07T23:09:38.234-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="guest post"/><category scheme="http://www.blogger.com/atom/ns#" term="ruby"/><title type='text'>Creating a Ruby DSL: A Metaprogramming Tutorial</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This article is written by an author from &lt;a href=&quot;https://www.toptal.com/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;toptal.com&lt;/a&gt; ( &lt;a href=&quot;https://www.toptal.com/ruby/ruby-dsl-metaprogramming-guide&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Origin&lt;/a&gt; )&lt;br /&gt;
&lt;br /&gt;
Domain specific languages (DSL) are an incredibly powerful tool for  making it easier to program or configure complex systems. They are also  everywhere—as a software engineer you are most likely using several  different DSLs on a daily basis.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://4.bp.blogspot.com/-MciXr8V7W1U/Wdb12HIdyhI/AAAAAAAAAMU/XR9i8K43WdYiGRsWdjTHZR5TZX7ozF5dwCLcBGAs/s1600/TopTal.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;561&quot; data-original-width=&quot;578&quot; height=&quot;386&quot; src=&quot;https://4.bp.blogspot.com/-MciXr8V7W1U/Wdb12HIdyhI/AAAAAAAAAMU/XR9i8K43WdYiGRsWdjTHZR5TZX7ozF5dwCLcBGAs/s400/TopTal.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
In this article, you will learn what domain specific languages are,  when they should be used, and finally how you can make your very own DSL  in Ruby using advanced metaprogramming techniques.&lt;br /&gt;
&lt;br /&gt;
This article builds upon Nikola Todorovic’s &lt;a href=&quot;https://www.toptal.com/ruby/ruby-metaprogramming-cooler-than-it-sounds&quot;&gt;introduction&lt;/a&gt;  to Ruby metaprogramming, also published on the Toptal Blog. So if you  are new to metaprogramming, make sure you read that first.&lt;br /&gt;
&lt;h3 id=&quot;what-is-a-domain-specific-language&quot; style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;What Is a Domain Specific Language?&lt;/span&gt;&lt;/u&gt;&lt;/h3&gt;
The general definition of DSLs is that they are languages specialized  to a particular application domain or use case. This means that you can  only use them for specific things—they are not suitable for  general-purpose software development. If that sounds broad, that’s  because it is—DSLs come in many different shapes and sizes. Here are a  few important categories:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Markup languages such as HTML and CSS are designed for describing  specific things like the structure, content, and styles of web pages. It  is not possible to write arbitrary algorithms with them, so they fit  the description of a DSL.&lt;/li&gt;
&lt;li&gt;Macro and query languages (e.g., SQL) sit on top of a particular  system or another programming language and are usually limited in what  they can do. Therefore they obviously qualify as domain specific  languages.&lt;/li&gt;
&lt;li&gt;Many DSLs do not have their own syntax—instead, they use the  syntax of an established programming language in a clever way that feels  like using a separate mini-language.&lt;/li&gt;
&lt;/ul&gt;
This last category is called an &lt;i&gt;internal DSL&lt;/i&gt;, and it is one  of these that we are going to create as an example very soon. But before  we get into that, let’s take a look at a few well-known examples of  internal DSLs. The route definition syntax in Rails is one of them:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span style=&quot;color: cyan;&quot;&gt;&lt;b&gt;&lt;span class=&quot;hljs-constant&quot;&gt;Rails&lt;/span&gt;.application.routes.draw&lt;/b&gt;&lt;/span&gt; &lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;&lt;/b&gt;
  root &lt;span class=&quot;hljs-symbol&quot;&gt;to:&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;pages#main&quot;&lt;/span&gt;

  resources &lt;span class=&quot;hljs-symbol&quot;&gt;:posts&lt;/span&gt; &lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;&lt;/b&gt;
    get &lt;span class=&quot;hljs-symbol&quot;&gt;:preview&lt;/span&gt;

    resources &lt;span class=&quot;hljs-symbol&quot;&gt;:comments&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;only:&lt;/span&gt; [&lt;span class=&quot;hljs-symbol&quot;&gt;:new&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:create&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:destroy&lt;/span&gt;]
 &lt;b&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/b&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
This is Ruby code, yet it feels more like a custom route definition  language, thanks to the various metaprogramming techniques that make  such a clean, easy-to-use interface possible. Notice that the structure  of the DSL is implemented using Ruby blocks, and method calls such as &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;resources&lt;/code&gt; are used for defining the keywords of this mini-language.&lt;br /&gt;
&lt;br /&gt;
Metaprogramming is used even more heavily in the RSpec testing library:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;describe &lt;span class=&quot;hljs-constant&quot;&gt;UsersController&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;type:&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;:controller&lt;/span&gt; &lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;&lt;/b&gt;
  before &lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;&lt;/b&gt;
    allow(controller).to receive(&lt;span class=&quot;hljs-symbol&quot;&gt;:current_user&lt;/span&gt;).and_return(&lt;span class=&quot;hljs-keyword&quot;&gt;nil&lt;/span&gt;)
  &lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/b&gt;

  describe &lt;span class=&quot;hljs-string&quot;&gt;&quot;GET #new&quot;&lt;/span&gt; &lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;&lt;/b&gt;
    subject { get &lt;span class=&quot;hljs-symbol&quot;&gt;:new&lt;/span&gt; }

    it &lt;span class=&quot;hljs-string&quot;&gt;&quot;returns success&quot;&lt;/span&gt; &lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;&lt;/b&gt;
      expect(subject).to be_success
   &lt;b&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/b&gt;
 &lt;b&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/b&gt;
&lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/b&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
This piece of code also contains examples for &lt;i&gt;fluent interfaces&lt;/i&gt;,  which allow declarations to be read out loud as plain English  sentences, making it a lot easier to understand what the code is doing:&lt;code class=&quot;language-ruby hljs&quot;&gt; &lt;/code&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;allow(controller).to receive(&lt;span class=&quot;hljs-symbol&quot;&gt;:current_user&lt;/span&gt;).and_return(&lt;span class=&quot;hljs-keyword&quot;&gt;nil&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;expect(subject).to be_success&lt;/code&gt;&lt;/pre&gt;
Another example of a fluent interface is the query interface of ActiveRecord and Arel, which uses an &lt;a href=&quot;https://en.wikipedia.org/wiki/Abstract_syntax_tree&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;abstract syntax tree&lt;/a&gt; internally for building complex SQL queries:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;Post&lt;/span&gt;.                               
  select([                          
    &lt;span class=&quot;hljs-constant&quot;&gt;Post&lt;/span&gt;[&lt;span class=&quot;hljs-constant&quot;&gt;Arel&lt;/span&gt;.star],                
    &lt;span class=&quot;hljs-constant&quot;&gt;Comment&lt;/span&gt;[&lt;span class=&quot;hljs-symbol&quot;&gt;:id&lt;/span&gt;].count.             
      as(&lt;span class=&quot;hljs-string&quot;&gt;&quot;num_comments&quot;&lt;/span&gt;),           
  ]).                               
  joins(&lt;span class=&quot;hljs-symbol&quot;&gt;:comments&lt;/span&gt;).                 
                                    
  where.&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;(&lt;span class=&quot;hljs-symbol&quot;&gt;status:&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;:draft&lt;/span&gt;).        
  where(                            
    &lt;span class=&quot;hljs-constant&quot;&gt;Post&lt;/span&gt;[&lt;span class=&quot;hljs-symbol&quot;&gt;:created_at&lt;/span&gt;].lte(&lt;span class=&quot;hljs-constant&quot;&gt;Time&lt;/span&gt;.now) 
  ).                                
  group(&lt;span class=&quot;hljs-constant&quot;&gt;Post&lt;/span&gt;[&lt;span class=&quot;hljs-symbol&quot;&gt;:id&lt;/span&gt;])                  
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Although the clean and expressive syntax of Ruby along with its  metaprogramming capabilities makes it uniquely suited for building  domain specific languages, DSLs exist in other languages as well. Here  is an example of a JavaScript test using the Jasmine framework:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;describe(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Helper functions&quot;&lt;/span&gt;, function() {
  beforeEach(function() {
    this.helpers = window.helpers;
  });

  describe(&lt;span class=&quot;hljs-string&quot;&gt;&quot;log error&quot;&lt;/span&gt;, function() {
    it(&lt;span class=&quot;hljs-string&quot;&gt;&quot;logs error message to console&quot;&lt;/span&gt;, function() {
      spyOn(console, &lt;span class=&quot;hljs-string&quot;&gt;&quot;log&quot;&lt;/span&gt;).&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt;.returnValue(&lt;span class=&quot;hljs-keyword&quot;&gt;true&lt;/span&gt;);
      this.helpers.log_error(&lt;span class=&quot;hljs-string&quot;&gt;&quot;oops!&quot;&lt;/span&gt;);
      expect(console.log).toHaveBeenCalledWith(&lt;span class=&quot;hljs-string&quot;&gt;&quot;ERROR: oops!&quot;&lt;/span&gt;);
    });
  });
});&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
This syntax is perhaps not as clean as that of the Ruby examples, but  it shows that with clever naming and creative use of the syntax,  internal DSLs can be created using almost any language.&lt;br /&gt;
&lt;br /&gt;
The benefit of &lt;a href=&quot;http://javieracero.com/blog/internal-vs-external-dsl&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;internal DSLs&lt;/a&gt;  is that they don’t require a separate parser, which can be notoriously  difficult to implement properly. And because they use the syntax of the  language they are implemented in, they also integrate seamlessly with  the rest of the codebase.&lt;br /&gt;
&lt;br /&gt;
What we have to give up in return is syntactic freedom—internal DSLs  have to be syntactically valid in their implementation language. How  much you have to compromise in this regard depends largely on the  selected language, with verbose, statically typed languages such as Java  and VB.NET being on one end of the spectrum, and dynamic languages with  extensive metaprogramming capabilities such as Ruby on the other end.&lt;br /&gt;
&lt;h4 id=&quot;building-our-own---a-ruby-dsl-for-class-configuration&quot; style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Building Our Own—A Ruby DSL for Class Configuration&lt;/span&gt;&lt;/u&gt;&lt;/h4&gt;
The example DSL we are going to build in Ruby is a reusable  configuration engine for specifying the configuration attributes of a  Ruby class using a very simple syntax. Adding configuration capabilities  to a class is a very common requirement in the Ruby world, especially  when it comes to configuring external gems and API clients. The usual  solution is an interface like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |config|
  config.app_id = &lt;span class=&quot;hljs-string&quot;&gt;&quot;my_app&quot;&lt;/span&gt;
  config.title = &lt;span class=&quot;hljs-string&quot;&gt;&quot;My App&quot;&lt;/span&gt;
  config.cookie_name = &lt;span class=&quot;hljs-string&quot;&gt;&quot;my_app_session&quot;&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;&amp;nbsp;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Let’s implement this interface first—and then, using it as the  starting point, we can improve it step by step by adding more features,  cleaning up the syntax, and making our work reusable.&lt;br /&gt;
&lt;br /&gt;
What do we need to make this interface work? The &lt;code&gt;MyApp&lt;/code&gt; class should have a &lt;code&gt;configure&lt;/code&gt;  class method that takes a block and then executes that block by  yielding to it, passing in a configuration object that has accessor  methods for reading and writing the configuration values:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;MyApp&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-inheritance&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-inheritance&quot;&gt;&amp;lt; &lt;span class=&quot;hljs-parent&quot;&gt;self&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;config
      &lt;span class=&quot;hljs-variable&quot;&gt;@config&lt;/span&gt; ||= &lt;span class=&quot;hljs-constant&quot;&gt;Configuration&lt;/span&gt;.new
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;configure
      &lt;span class=&quot;hljs-keyword&quot;&gt;yield&lt;/span&gt; config
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Configuration&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;:app_id&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:title&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:cookie_name&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Once the configuration block has run, we can easily access and modify the values:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.config
=&amp;gt; 

&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.config.title
=&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;My App&quot;&lt;/span&gt;

&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.config.app_id = &lt;span class=&quot;hljs-string&quot;&gt;&quot;not_my_app&quot;&lt;/span&gt;
=&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;not_my_app&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
So far, this implementation does not feel like a custom language  enough to be considered a DSL. But let’s take things one step at a time.  Next, we will decouple the configuration functionality from the &lt;code&gt;MyApp&lt;/code&gt; class and make it generic enough to be usable in many different use cases.&lt;br /&gt;
&lt;h3&gt;
&lt;/h3&gt;
&lt;h4 id=&quot;making-it-reusable&quot; style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;Making It Reusable&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;
Right now, if we wanted to add similar configuration capabilities to a different class, we would have to copy both the &lt;code&gt;Configuration&lt;/code&gt; class and its related setup methods into that other class, as well as edit the &lt;code&gt;attr_accessor&lt;/code&gt;  list to change the accepted configuration attributes. To avoid having  to do this, let’s move the configuration features into a separate module  called &lt;code&gt;Configurable&lt;/code&gt;. With that, our &lt;code&gt;MyApp&lt;/code&gt; class will look like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;MyApp&lt;/span&gt;&lt;/span&gt;
&lt;b&gt;  &lt;span class=&quot;hljs-keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;hljs-constant&quot;&gt;Configurable&lt;/span&gt;&lt;/b&gt;
  
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Everything related to configuration has been moved to the &lt;code&gt;Configurable&lt;/code&gt; module:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;b&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Configurable&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;.included(host_class)
    host_class.extend &lt;span class=&quot;hljs-constant&quot;&gt;ClassMethods&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;ClassMethods&lt;/span&gt;&lt;/span&gt;
&lt;/b&gt;    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;config
      &lt;span class=&quot;hljs-variable&quot;&gt;@config&lt;/span&gt; ||= &lt;span class=&quot;hljs-constant&quot;&gt;Configuration&lt;/span&gt;.new
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;configure
      &lt;span class=&quot;hljs-keyword&quot;&gt;yield&lt;/span&gt; config
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;b&gt;  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/b&gt;
  &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Configuration&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;:app_id&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:title&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:cookie_name&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Not much has changed here, except for the new &lt;code&gt;self.included&lt;/code&gt; method. We need this method because including a module only mixes in its instance methods, so our &lt;code&gt;config&lt;/code&gt; and &lt;code&gt;configure&lt;/code&gt; class methods will not be added to the host class by default. However, if we define a special method called &lt;code&gt;included&lt;/code&gt;  on a module, Ruby will call it whenever that module is included in a  class. There we can manually extend the host class with the methods in &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;code&gt;ClassMethods&lt;/code&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;.included(host_class)     
  host_class.extend &lt;span class=&quot;hljs-constant&quot;&gt;ClassMethods&lt;/span&gt;  
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
We are not done yet—our next step is to make it possible to specify  the supported attributes in the host class that includes the &lt;code&gt;Configurable&lt;/code&gt; module. A solution like this would look nice:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;MyApp&lt;/span&gt;&lt;/span&gt;
&lt;b&gt;  &lt;span class=&quot;hljs-keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;hljs-constant&quot;&gt;Configurable&lt;/span&gt;.with(&lt;span class=&quot;hljs-symbol&quot;&gt;:app_id&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:title&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:cookie_name&lt;/span&gt;)
&lt;/b&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Perhaps somewhat surprisingly, the code above is syntactically correct—&lt;code&gt;include&lt;/code&gt; is not a keyword but simply a regular method that expects a &lt;code&gt;Module&lt;/code&gt; object as its parameter. As long as we pass it an expression that returns a &lt;code&gt;Module&lt;/code&gt;, it will happily include it. So, instead of including &lt;code&gt;Configurable&lt;/code&gt; directly, we need a method with the name &lt;code&gt;with&lt;/code&gt; on it that generates a new module that is customized with the specified attributes:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Configurable&lt;/span&gt;&lt;/span&gt;
&lt;b&gt;  &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;.with(*attrs)
&lt;/b&gt;    
&lt;b&gt;    config_class = &lt;span class=&quot;hljs-constant&quot;&gt;Class&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;hljs-keyword&quot;&gt;attr_accessor&lt;/span&gt; *attrs
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/b&gt;
&lt;b&gt;    class_methods = &lt;span class=&quot;hljs-constant&quot;&gt;Module&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
      define_method &lt;span class=&quot;hljs-symbol&quot;&gt;:config&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;hljs-variable&quot;&gt;@config&lt;/span&gt; ||= config_class.new
      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/b&gt;
      &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;configure
        &lt;span class=&quot;hljs-keyword&quot;&gt;yield&lt;/span&gt; config
      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;b&gt;    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/b&gt;
&lt;b&gt;    &lt;span class=&quot;hljs-constant&quot;&gt;Module&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
      singleton_class.send &lt;span class=&quot;hljs-symbol&quot;&gt;:define_method&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:included&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |host_class|
        host_class.extend class_methods
      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/b&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
There is a lot to unpack here. The entire &lt;code&gt;Configurable&lt;/code&gt; module now consists of just a single &lt;code&gt;with&lt;/code&gt; method, with everything happening within that method. First, we create a new anonymous class with &lt;code&gt;Class.new&lt;/code&gt; to hold our attribute accessor methods. Because &lt;code&gt;Class.new&lt;/code&gt; takes the class definition as a block and blocks have access to outside variables, we are able to pass the &lt;code&gt;attrs&lt;/code&gt; variable to &lt;code&gt;attr_accessor&lt;/code&gt; without problems.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;.with(*attrs)            
   
  config_class = &lt;span class=&quot;hljs-constant&quot;&gt;Class&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;    
    &lt;span class=&quot;hljs-keyword&quot;&gt;attr_accessor&lt;/span&gt; *attrs         
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
The fact that blocks in Ruby have access to outside variables is also the reason why they are sometimes called &lt;i&gt;closures&lt;/i&gt;,  as they include, or “close over” the outside environment that they were  defined in. Note that I used the phrase “defined in” and not “executed  in”. That’s correct – regardless of when and where our &lt;code&gt;define_method&lt;/code&gt; blocks will eventually be executed, they will always be able to access the variables &lt;code&gt;config_class&lt;/code&gt; and &lt;code&gt;class_methods&lt;/code&gt;, even &lt;i&gt;after&lt;/i&gt; the &lt;code&gt;with&lt;/code&gt; method has finished running and returned. The following example demonstrates this behavior:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;create_block 
  foo = &lt;span class=&quot;hljs-string&quot;&gt;&quot;hello&quot;&lt;/span&gt;            
   &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-constant&quot;&gt;Proc&lt;/span&gt;.new { foo }   
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

  block = create_block       

  block.call                 
 =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;hello&quot;&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Now that we know about this neat behavior of blocks, we can go ahead and define an anonymous module in &lt;code&gt;class_methods&lt;/code&gt; for the class methods that will be added to the host class when our generated module is included. Here we have to use &lt;code&gt;define_method&lt;/code&gt; to define the &lt;code&gt;config&lt;/code&gt; method, because we need access to the outside &lt;code&gt;config_class&lt;/code&gt; variable from within the method. Defining the method using the &lt;code&gt;def&lt;/code&gt; keyword would not give us that access because regular method definitions with &lt;code&gt;def&lt;/code&gt; are not closures – however, &lt;code&gt;define_method&lt;/code&gt; takes a block, so this will work:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;config_class =  

 class_methods = &lt;span class=&quot;hljs-constant&quot;&gt;Module&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;      
   define_method &lt;span class=&quot;hljs-symbol&quot;&gt;:config&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;          
    &lt;span class=&quot;hljs-variable&quot;&gt;@config&lt;/span&gt; ||= config_class.new   
   &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Finally, we call &lt;code&gt;Module.new&lt;/code&gt; to create the module that we are going to return. Here we need to define our &lt;code&gt;self.included&lt;/code&gt; method, but unfortunately we cannot do that with the &lt;code&gt;def&lt;/code&gt; keyword, as the method needs access to the outside &lt;code&gt;class_methods&lt;/code&gt; variable. Therefore, we have to use &lt;code&gt;define_method&lt;/code&gt;  with a block again, but this time on the singleton class of the module,  as we are defining a method on the module instance itself. Oh, and  since &lt;code&gt;define_method&lt;/code&gt; is a private method of the singleton class, we have to use &lt;code&gt;send&lt;/code&gt; to invoke it instead of calling it directly:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;class_methods =  
 
&lt;span class=&quot;hljs-constant&quot;&gt;Module&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; 
  singleton_class.send &lt;span class=&quot;hljs-symbol&quot;&gt;:define_method&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:included&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |host_class| 
    host_class.extend class_methods   
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt; 
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Phew, that was some pretty hardcore metaprogramming already. But was  the added complexity worth it? Take a look at how easy it is to use and  decide for yourself:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;SomeClass&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;hljs-constant&quot;&gt;Configurable&lt;/span&gt;.with(&lt;span class=&quot;hljs-symbol&quot;&gt;:foo&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:bar&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hljs-constant&quot;&gt;SomeClass&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |config|
  config.foo = &lt;span class=&quot;hljs-string&quot;&gt;&quot;wat&quot;&lt;/span&gt;
  config.bar = &lt;span class=&quot;hljs-string&quot;&gt;&quot;huh&quot;&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hljs-constant&quot;&gt;SomeClass&lt;/span&gt;.config.foo
=&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;wat&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-string&quot;&gt;&amp;nbsp;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
But we can do even better. In the next step we will clean up the syntax of the &lt;code&gt;configure&lt;/code&gt; block a little bit to make our module even more convenient to use.&lt;br /&gt;
&lt;h4 id=&quot;cleaning-up-the-syntax&quot; style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;Cleaning Up the Syntax&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;
There is one last thing that is still bothering me with our current implementation—we have to repeat &lt;code&gt;config&lt;/code&gt; on every single line in the configuration block. A proper DSL would know that everything within the &lt;code&gt;configure&lt;/code&gt; block should be executed in the context of our configuration object and enable us to achieve the same thing with just this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  app_id &lt;span class=&quot;hljs-string&quot;&gt;&quot;my_app&quot;&lt;/span&gt;
  title &lt;span class=&quot;hljs-string&quot;&gt;&quot;My App&quot;&lt;/span&gt;
  cookie_name &lt;span class=&quot;hljs-string&quot;&gt;&quot;my_app_session&quot;&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Let’s implement it, shall we? From the looks of it, we will need two  things. First, we need a way to execute the block passed to &lt;code&gt;configure&lt;/code&gt;  in the context of the configuration object so that method calls within  the block go to that object. Second, we have to change the accessor  methods so that they write the value if an argument is provided to them  and read it back when called without an argument. A possible  implementation looks like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Configurable&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;.with(*attrs)
&lt;b&gt;    not_provided = &lt;span class=&quot;hljs-constant&quot;&gt;Object&lt;/span&gt;.new
&lt;/b&gt;  
    config_class = &lt;span class=&quot;hljs-constant&quot;&gt;Class&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
&lt;b&gt;      attrs.each &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |attr|
        define_method attr &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |value = not_provided|
          &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; value === not_provided
            instance_variable_get(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@&lt;span class=&quot;hljs-subst&quot;&gt;#{attr}&lt;/span&gt;&quot;&lt;/span&gt;)
          &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;
            instance_variable_set(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@&lt;span class=&quot;hljs-subst&quot;&gt;#{attr}&lt;/span&gt;&quot;&lt;/span&gt;, value)
          &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
        &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

      &lt;span class=&quot;hljs-keyword&quot;&gt;attr_writer&lt;/span&gt; *attrs
&lt;/b&gt;    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

    class_methods = &lt;span class=&quot;hljs-constant&quot;&gt;Module&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
      

      &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;configure(&amp;amp;block)
&lt;b&gt;        config.instance_eval(&amp;amp;block)
&lt;/b&gt;      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;   
    
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
The simpler change here is running the &lt;code&gt;configure&lt;/code&gt; block in the context of the configuration object. Calling Ruby’s &lt;code&gt;instance_eval&lt;/code&gt;  method on an object lets you execute an arbitrary block of code as if  it was running within that object, which means that when the  configuration block calls the &lt;code&gt;app_id&lt;/code&gt; method on the first line, that call will go to our configuration class instance.&lt;br /&gt;
The change to the attribute accessor methods in &lt;code&gt;config_class&lt;/code&gt; is a bit more complicated. To understand it, we need to first understand what exactly &lt;code&gt;attr_accessor&lt;/code&gt; was doing behind the scenes. Take the following &lt;code&gt;attr_accessor&lt;/code&gt; call for example:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;SomeClass&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;:foo&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:bar&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
This is equivalent to defining a reader and writer method for each specified attribute:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;SomeClass&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;foo
    &lt;span class=&quot;hljs-variable&quot;&gt;@foo&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;foo=(value)
    &lt;span class=&quot;hljs-variable&quot;&gt;@foo&lt;/span&gt; = value
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
  
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
So when we wrote &lt;code&gt;attr_accessor *attrs&lt;/code&gt; in the original code, Ruby defined the attribute reader and writer methods for us for every attribute in &lt;code&gt;attrs&lt;/code&gt;—that is, we got the following standard accessor methods: &lt;code&gt;app_id&lt;/code&gt;, &lt;code&gt;app_id=&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;title=&lt;/code&gt; and so on. In our new version, we want to keep the standard writer methods so that assignments like this still work properly:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.config.app_id = &lt;span class=&quot;hljs-string&quot;&gt;&quot;not_my_app&quot;&lt;/span&gt;
=&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;not_my_app&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
We can keep auto-generating the writer methods by calling &lt;code&gt;attr_writer *attrs&lt;/code&gt;.  However, we can no longer use the standard reader methods, as they also  have to be capable of writing the attribute to support this new syntax:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  app_id &lt;span class=&quot;hljs-string&quot;&gt;&quot;my_app&quot;&lt;/span&gt; 
  app_id          
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
To generate the reader methods ourselves, we loop over the &lt;code&gt;attrs&lt;/code&gt;  array and define a method for each attribute that returns the current  value of the matching instance variable if no new value is provided and  writes the new value if it is specified:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;not_provided = &lt;span class=&quot;hljs-constant&quot;&gt;Object&lt;/span&gt;.new

attrs.each &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |attr|
  define_method attr &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |value = not_provided|
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; value === not_provided
      instance_variable_get(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@&lt;span class=&quot;hljs-subst&quot;&gt;#{attr}&lt;/span&gt;&quot;&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;
      instance_variable_set(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@&lt;span class=&quot;hljs-subst&quot;&gt;#{attr}&lt;/span&gt;&quot;&lt;/span&gt;, value)
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Here we use Ruby’s &lt;code&gt;instance_variable_get&lt;/code&gt; method to read an instance variable with an arbitrary name, and &lt;code&gt;instance_variable_set&lt;/code&gt;  to assign a new value to it. Unfortunately the variable name must be  prefixed with an “@” sign in both cases—hence the string interpolation.&lt;br /&gt;
You might be wondering why we have to use a blank object as the default value for “not provided” and why we can’t simply use &lt;code&gt;nil&lt;/code&gt; for that purpose. The reason is simple—&lt;code&gt;nil&lt;/code&gt; is a valid value that someone might want to set for a configuration attribute. If we tested for &lt;code&gt;nil&lt;/code&gt;, we would not be able to tell these two scenarios apart:&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  app_id &lt;span class=&quot;hljs-keyword&quot;&gt;nil&lt;/span&gt; 
  app_id     
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
That blank object stored in &lt;code&gt;not_provided&lt;/code&gt; is only ever  going to be equal to itself, so this way we can be certain that nobody  is going to pass it into our method and cause an unintended read instead  of a write.&lt;br /&gt;
&lt;h4 id=&quot;adding-support-for-references&quot; style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;Adding Support for References&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;
There is one more feature that we could add to make our module even  more versatile—the ability to reference a configuration attribute from  another one:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  app_id &lt;span class=&quot;hljs-string&quot;&gt;&quot;my_app&quot;&lt;/span&gt;
  title &lt;span class=&quot;hljs-string&quot;&gt;&quot;My App&quot;&lt;/span&gt;
  cookie_name { &lt;span class=&quot;hljs-string&quot;&gt;&quot;&lt;span class=&quot;hljs-subst&quot;&gt;#{app_id}&lt;/span&gt;_session&quot;&lt;/span&gt; }
&lt;span class=&quot;hljs-constant&quot;&gt;End&lt;/span&gt;

&lt;span class=&quot;hljs-constant&quot;&gt;MyApp&lt;/span&gt;.config.cookie_name
=&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;my_app_session&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Here we added a reference from &lt;code&gt;cookie_name&lt;/code&gt; to the &lt;code&gt;app_id&lt;/code&gt;  attribute. Note that the expression containing the reference is passed  in as a block—this is necessary in order to support the delayed  evaluation of the attribute value. The idea is to only evaluate the  block later when the attribute is read and not when it is  defined—otherwise funny things would happen if we defined the attributes  in the “wrong” order:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;SomeClass&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  foo &lt;span class=&quot;hljs-string&quot;&gt;&quot;&lt;span class=&quot;hljs-subst&quot;&gt;#{bar}&lt;/span&gt;_baz&quot;&lt;/span&gt;     
  bar &lt;span class=&quot;hljs-string&quot;&gt;&quot;hello&quot;&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hljs-constant&quot;&gt;SomeClass&lt;/span&gt;.config.foo
=&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;_baz&quot;&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
If the expression is wrapped in a block, that will prevent it from  being evaluated right away. Instead, we can save the block to be  executed later when the attribute value is retrieved:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;SomeClass&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  foo { &lt;span class=&quot;hljs-string&quot;&gt;&quot;&lt;span class=&quot;hljs-subst&quot;&gt;#{bar}&lt;/span&gt;_baz&quot;&lt;/span&gt; }  
  bar &lt;span class=&quot;hljs-string&quot;&gt;&quot;hello&quot;&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hljs-constant&quot;&gt;SomeClass&lt;/span&gt;.config.foo    
=&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;hello_baz&quot;&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
We do not have to make big changes to the &lt;code&gt;Configurable&lt;/code&gt; module to add support for delayed evaluation using blocks. In fact, we only have to change the attribute method definition:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;define_method attr &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |value = not_provided, &amp;amp;block|
  &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; value === not_provided &amp;amp;&amp;amp; block.&lt;span class=&quot;hljs-keyword&quot;&gt;nil&lt;/span&gt;?
    result = instance_variable_get(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@&lt;span class=&quot;hljs-subst&quot;&gt;#{attr}&lt;/span&gt;&quot;&lt;/span&gt;)
    result.is_a?(&lt;span class=&quot;hljs-constant&quot;&gt;Proc&lt;/span&gt;) ? instance_eval(&amp;amp;result) &lt;span class=&quot;hljs-symbol&quot;&gt;:&lt;/span&gt; result
  &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;
    instance_variable_set(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@&lt;span class=&quot;hljs-subst&quot;&gt;#{attr}&lt;/span&gt;&quot;&lt;/span&gt;, block || value)
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
When setting an attribute, the &lt;code&gt;block || value&lt;/code&gt; expression  saves the block if one was passed in, or otherwise it saves the value.  Then, when the attribute is later read, we check if it is a block and  evaluate it using &lt;code&gt;instance_eval&lt;/code&gt; if it is, or if it is not a block, we return it like we did before.&lt;br /&gt;
Supporting references comes with its own caveats and edge cases, of  course. For example, you can probably figure out what happens if you  read any of the attributes in this configuration:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-constant&quot;&gt;SomeClass&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  foo { bar }
  bar { foo }
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 id=&quot;the-finished-module&quot; style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;The Finished Module&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;
In the end, we have got ourselves a pretty neat module for making an  arbitrary class configurable and then specifying those configuration  values using a clean and simple DSL that also lets us reference one  configuration attribute from another:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;MyApp&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;hljs-constant&quot;&gt;Configurable&lt;/span&gt;.with(&lt;span class=&quot;hljs-symbol&quot;&gt;:app_id&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:title&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:cookie_name&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hljs-constant&quot;&gt;SomeClass&lt;/span&gt;.configure &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  app_id &lt;span class=&quot;hljs-string&quot;&gt;&quot;my_app&quot;&lt;/span&gt;
  title &lt;span class=&quot;hljs-string&quot;&gt;&quot;My App&quot;&lt;/span&gt;
  cookie_name { &lt;span class=&quot;hljs-string&quot;&gt;&quot;&lt;span class=&quot;hljs-subst&quot;&gt;#{app_id}&lt;/span&gt;_session&quot;&lt;/span&gt; }
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Here is the final version of the module that implements our DSL—a total of 36 lines of code:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;code1&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby hljs&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Configurable&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;.with(*attrs)
    not_provided = &lt;span class=&quot;hljs-constant&quot;&gt;Object&lt;/span&gt;.new

    config_class = &lt;span class=&quot;hljs-constant&quot;&gt;Class&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
      attrs.each &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |attr|
        define_method attr &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |value = not_provided, &amp;amp;block|
          &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; value === not_provided &amp;amp;&amp;amp; block.&lt;span class=&quot;hljs-keyword&quot;&gt;nil&lt;/span&gt;?
            result = instance_variable_get(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@&lt;span class=&quot;hljs-subst&quot;&gt;#{attr}&lt;/span&gt;&quot;&lt;/span&gt;)
            result.is_a?(&lt;span class=&quot;hljs-constant&quot;&gt;Proc&lt;/span&gt;) ? instance_eval(&amp;amp;result) &lt;span class=&quot;hljs-symbol&quot;&gt;:&lt;/span&gt; result
          &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;
            instance_variable_set(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@&lt;span class=&quot;hljs-subst&quot;&gt;#{attr}&lt;/span&gt;&quot;&lt;/span&gt;, block || value)
          &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
        &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

      &lt;span class=&quot;hljs-keyword&quot;&gt;attr_writer&lt;/span&gt; *attrs
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

    class_methods = &lt;span class=&quot;hljs-constant&quot;&gt;Module&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
      define_method &lt;span class=&quot;hljs-symbol&quot;&gt;:config&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;hljs-variable&quot;&gt;@config&lt;/span&gt; ||= config_class.new
      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

      &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt; &lt;/span&gt;configure(&amp;amp;block)
        config.instance_eval(&amp;amp;block)
      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;hljs-constant&quot;&gt;Module&lt;/span&gt;.new &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
      singleton_class.send &lt;span class=&quot;hljs-symbol&quot;&gt;:define_method&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;:included&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt; |host_class|
        host_class.extend class_methods
      &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Looking at all this &lt;a href=&quot;https://www.toptal.com/ruby&quot;&gt;Ruby&lt;/a&gt;  magic in a piece of code that is nearly unreadable and therefore very  hard to maintain, you might wonder if all this effort was worth it just  to make our domain specific language a little bit nicer. The short  answer is that it depends—which brings us to the final topic of this  article.&lt;br /&gt;
&lt;h4 id=&quot;ruby-dsls---when-to-use-and-when-not-to-use-them&quot; style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;Ruby DSLs—When to Use and When Not to Use Them&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;
You have probably noticed while reading the implementation steps of  our DSL that, as we made the external facing syntax of the language  cleaner and easier to use, we had to use an ever increasing number of &lt;a href=&quot;http://nithinbekal.com/slides/metaprogramming-tricks/#/&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;meta-programming tricks&lt;/a&gt;  under the hood to make it happen. This resulted in an implementation  that will be incredibly hard to understand and modify in the future.  Like so many other things in software development, this is also a  tradeoff that must be carefully examined.&lt;br /&gt;
&lt;br /&gt;
For a domain specific language to be worth its implementation and  maintenance cost, it must bring an even greater sum of benefits to the  table. This is usually achieved by making the language reusable in as  many different scenarios as possible, thereby amortizing the total cost  between many different use cases. Frameworks and libraries are more  likely to contain their own DSLs exactly because they are used by lots  of developers, each of whom can enjoy the productivity benefits of those  embedded languages.&lt;br /&gt;
&lt;br /&gt;
So, as a general principle, only build DSLs if you, other developers,  or the end users of your application will be getting a lot of use out  of them. If you do create a DSL, make sure to include a comprehensive  test suite with it, as well as properly document its syntax as it can be  very hard to figure out from the implementation alone. Future you and  your fellow developers will thank you for it. &lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
Visit &amp;amp; subscribe &lt;a href=&quot;https://www.toptal.com/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;toptal.com&lt;/a&gt; for such insightful articles.&amp;nbsp; It&#39;s a #1 blog for Engineers.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: #2d89f1; border-radius: 30px; border: 1px solid black; padding: 13px;&quot;&gt;
&lt;b&gt;About the Author of this post&lt;span style=&quot;color: white;&quot;&gt; &lt;/span&gt;:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;blog_author_banner-about&quot; data-role=&quot;author_about&quot;&gt;
&lt;b&gt;Máté&lt;/b&gt; is a  full-stack software engineer with 7+ years of experience working on web  applications of all sizes, from small greenfield projects to complex  legacy systems. He is extremely organized, communicates very well, and  prides himself in writing clean, future-proof code with a low tolerance  for technical debt. Máté&#39;s formal education is in accounting, banking,  and finance; therefore, he has extensive domain knowledge in these  fields as well.&lt;/div&gt;
&amp;nbsp; &lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://cs.techruto.com/feeds/4956906352979679181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=5718802842428101241&amp;postID=4956906352979679181&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5718802842428101241/posts/default/4956906352979679181'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5718802842428101241/posts/default/4956906352979679181'/><link rel='alternate' type='text/html' href='https://cs.techruto.com/2017/10/creating-ruby-dsl-metaprogramming.html' title='Creating a Ruby DSL: A Metaprogramming Tutorial'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/14791872663816121285</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://4.bp.blogspot.com/-MciXr8V7W1U/Wdb12HIdyhI/AAAAAAAAAMU/XR9i8K43WdYiGRsWdjTHZR5TZX7ozF5dwCLcBGAs/s72-c/TopTal.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5718802842428101241.post-1057856940855577084</id><published>2017-06-30T06:44:00.000-07:00</published><updated>2017-07-01T06:19:41.675-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="core java"/><category scheme="http://www.blogger.com/atom/ns#" term="Core Java Interview Q&amp;A"/><title type='text'>Logical operator Vs Bitwise operator in Java : &amp;&amp; vs &amp;, || vs | </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;&lt;u&gt;&amp;amp;&amp;amp;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt; operator is a Logical operator in Java. In Java it&#39;s alias names are short-circuit &lt;b&gt;AND &lt;/b&gt;operator, and conditional AND operator. &lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://2.bp.blogspot.com/-KfKZTeXFJjQ/WUZUlq5cHcI/AAAAAAAAAl8/NE9AUorZZkEtsFOh0jRDn4N_OSstm02_gCLcBGAs/s1600/AND.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;AND Logic Gate&quot; border=&quot;0&quot; data-original-height=&quot;333&quot; data-original-width=&quot;565&quot; height=&quot;235&quot; src=&quot;https://2.bp.blogspot.com/-KfKZTeXFJjQ/WUZUlq5cHcI/AAAAAAAAAl8/NE9AUorZZkEtsFOh0jRDn4N_OSstm02_gCLcBGAs/s400/AND.png&quot; title=&quot;AND Logic Gate&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;AND Logic Gate&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; It operates on Boolean variables only (true, false).&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; In an AND operation involving multiple conditions, if one of the condition is false the rest is not checked, as the result will always be false - This short circuit behavior of the Logical operator is the reason why it is also known as short-circuit operator. &lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;u&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&amp;nbsp;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; It&#39;s called Bitwise operator in Java, as it performs Logical AND operation on bit level.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; it is a.k.a Bitwise &lt;b&gt;AND&lt;/b&gt;, Unconditional Operator in Java. &lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; It performs AND logical operation on each bit.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Let&#39;s observe some important points about &amp;amp;&amp;amp; and &amp;amp;.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://4.bp.blogspot.com/-wSUauhqRonA/WUZiZYO7gGI/AAAAAAAAAmg/nYdUlgkdAX0-cDc6T_wzsTr1xzfZr5pQQCLcBGAs/s1600/blog1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Compilation error because Operator &amp;amp;&amp;amp; is defined only for Boolean values&quot; border=&quot;0&quot; data-original-height=&quot;219&quot; data-original-width=&quot;745&quot; height=&quot;188&quot; src=&quot;https://4.bp.blogspot.com/-wSUauhqRonA/WUZiZYO7gGI/AAAAAAAAAmg/nYdUlgkdAX0-cDc6T_wzsTr1xzfZr5pQQCLcBGAs/s640/blog1.png&quot; title=&quot;Compilation error because Operator &amp;amp;&amp;amp; is defined only for Boolean values&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Compilation error because Operator &lt;b&gt;&amp;amp;&amp;amp;&lt;/b&gt; is defined only for Boolean values&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: lime;&quot;&gt;Note #1&lt;/span&gt;: &lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; is defined only for &lt;span style=&quot;color: #741b47;&quot;&gt;&lt;i&gt;&lt;b&gt;boolean&lt;/b&gt;&lt;/i&gt;&lt;/span&gt; values. It&#39;s operands can only be true or false. Else, it produce compilation error.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: left;&quot; /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://3.bp.blogspot.com/-dljW37VN3uw/WUZiZRRQ0AI/AAAAAAAAAmc/JuhcicnLF4YAVjKPCrMX2Xd6IN9UoW0CQCLcBGAs/s1600/blog2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot; Operator &amp;amp; is defined only for Integer and Boolean values&quot; border=&quot;0&quot; data-original-height=&quot;245&quot; data-original-width=&quot;709&quot; height=&quot;219&quot; src=&quot;https://3.bp.blogspot.com/-dljW37VN3uw/WUZiZRRQ0AI/AAAAAAAAAmc/JuhcicnLF4YAVjKPCrMX2Xd6IN9UoW0CQCLcBGAs/s640/blog2.png&quot; title=&quot; Operator &amp;amp; is defined only for Integer and Boolean values&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Compilation error because Operator &lt;b&gt;&amp;amp;&lt;/b&gt; is defined only for Integer and Boolean values&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: lime;&quot;&gt;Note #2&lt;/span&gt;: &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/span&gt; is defined only for &lt;i&gt;&lt;b&gt;&lt;span style=&quot;color: #741b47;&quot;&gt;boolean&lt;/span&gt;&lt;/b&gt;&lt;/i&gt; and &lt;span style=&quot;color: #741b47;&quot;&gt;&lt;i&gt;&lt;b&gt;Integer&lt;/b&gt;&lt;/i&gt; &lt;/span&gt;(i.e., byte, short, char, int and long) values. Else it produce compilation error.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: left;&quot; /&gt;
&lt;div style=&quot;background-color: black; color: white; padding: 17px; text-align: left;&quot;&gt;
&lt;code&gt;  &lt;br /&gt;
&lt;span style=&quot;color: #6fa8dc;&quot;&gt;package&lt;/span&gt; LogicalVsBitwise;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #9fc5e8;&quot;&gt;public class&lt;/span&gt; LogicalVsBitwise {&lt;br /&gt;
&lt;span style=&quot;color: #9fc5e8;&quot;&gt;public static void&lt;/span&gt; main(String[] args) {&lt;br /&gt;
&lt;span style=&quot;color: #93c47d;&quot;&gt;// Logical &amp;amp;&amp;amp; Vs Bitwise &amp;amp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #9fc5e8;&quot;&gt;int&lt;/span&gt; a = 2;&lt;br /&gt;
&lt;span style=&quot;color: #9fc5e8;&quot;&gt;int&lt;/span&gt; b = 9;&lt;br /&gt;
System.&lt;span style=&quot;color: lime;&quot;&gt;out&lt;/span&gt;.println(&lt;b&gt;(a &amp;gt; b) &amp;amp;&amp;amp; (++a &amp;lt; b)&lt;/b&gt;); &lt;span style=&quot;color: #93c47d;&quot;&gt;//false &amp;amp;&amp;amp; -skipped- = false&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;System.&lt;span style=&quot;color: lime;&quot;&gt;out&lt;/span&gt;.println(&lt;b&gt;a&lt;/b&gt;); &lt;span style=&quot;color: #93c47d;&quot;&gt;//-skipped- so a did&#39;t get incremented. So, a=2&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;System.&lt;span style=&quot;color: lime;&quot;&gt;out&lt;/span&gt;.println(&lt;b&gt;(a &amp;gt; b) &amp;amp; (++a &amp;lt; b)&lt;/b&gt;);&lt;span style=&quot;color: #cccccc;&quot;&gt; &lt;span style=&quot;color: #93c47d;&quot;&gt;//false &amp;amp; true = false&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;System.&lt;span style=&quot;color: lime;&quot;&gt;out&lt;/span&gt;.println(&lt;b&gt;a&lt;/b&gt;); &lt;span style=&quot;color: #93c47d;&quot;&gt;// didn&#39;t skip so ++a = 3&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;}&amp;nbsp;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;} &lt;/code&gt; &lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Output&lt;/b&gt;:-&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
false&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
2&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
false&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
3 &lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: lime;&quot;&gt;Note #3&lt;/span&gt;: &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;&amp;amp;&amp;amp;&lt;/b&gt; &lt;/span&gt;operator skips rest of the conditions if it finds any false condition. Because, the result will be false.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Meanwhile, &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/span&gt; operator doesn&#39;t skip any condition even if it finds one false condition during the calculation.&lt;/div&gt;
&lt;hr style=&quot;text-align: left;&quot; /&gt;
&lt;div style=&quot;background-color: black; color: white; padding: 17px; text-align: left;&quot;&gt;
&lt;code&gt;&lt;br /&gt;
&lt;span style=&quot;color: #9fc5e8;&quot;&gt;package&lt;/span&gt; ObesrveIt &lt;br /&gt;
&lt;span style=&quot;color: #9fc5e8;&quot;&gt;public&lt;/span&gt; class ObesrveIt  {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #9fc5e8;&quot;&gt; public static void&lt;/span&gt; main(String[] args) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #93c47d;&quot;&gt; // &lt;b&gt;&amp;amp; operator&lt;/b&gt; works only on boolean or int operands&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #93c47d;&quot;&gt;// Default size of &lt;b&gt;int&lt;/b&gt; is 32 bit&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #9fc5e8;&quot;&gt;int &lt;/span&gt;&lt;b&gt;a = 2&lt;/b&gt;;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #93c47d;&quot;&gt;// 00000000 00000000 00000000 00000010&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #9fc5e8;&quot;&gt; int&lt;/span&gt; &lt;b&gt;b = 9&lt;/b&gt;;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #b6d7a8;&quot;&gt; &lt;span style=&quot;color: #93c47d;&quot;&gt;// 00000000 00000000 00000000 00001001&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #9fc5e8;&quot;&gt;int&lt;/span&gt; &lt;b&gt;result = a &amp;amp; b&lt;/b&gt;; &lt;span style=&quot;color: #93c47d;&quot;&gt;// 00000000 00000000 00000000 00000000&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.&lt;span style=&quot;color: lime;&quot;&gt;out&lt;/span&gt;.println(&lt;b&gt;result&lt;/b&gt;); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;
} &lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Output&lt;/b&gt;: 0&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: lime;&quot;&gt;Note #4 &lt;/span&gt;: &lt;b&gt;AND&lt;/b&gt; operation is performed on every bit. Hence, &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/span&gt; operator is also known as bit-wise operator in Java.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: left;&quot; /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
here&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://4.bp.blogspot.com/-uqT4W1jNF9A/WUZw6um0eyI/AAAAAAAAAmw/PwL8Dib5wG0RapnKBlUNFOkLOtUW51NbwCLcBGAs/s1600/blog3.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Disadvantage of using &amp;amp; operator &quot; border=&quot;0&quot; data-original-height=&quot;385&quot; data-original-width=&quot;679&quot; height=&quot;362&quot; src=&quot;https://4.bp.blogspot.com/-uqT4W1jNF9A/WUZw6um0eyI/AAAAAAAAAmw/PwL8Dib5wG0RapnKBlUNFOkLOtUW51NbwCLcBGAs/s640/blog3.png&quot; title=&quot;Disadvantage of using &amp;amp; operator &quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Difference between &lt;b&gt;&amp;amp;&amp;amp;&lt;/b&gt; and &lt;b&gt;&amp;amp;&lt;/b&gt; operator &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;background-color: lime;&quot;&gt;Note #5&lt;/span&gt;: &lt;/b&gt;&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;If a String variable stringName is null, executing &lt;i&gt;&lt;b&gt;stringName.length() &lt;/b&gt;&lt;/i&gt;command throws a &lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;java.lang.NullPointerException&lt;/b&gt;&lt;/span&gt;.&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;/b&gt;ignores rest of the condition as soon as it finds a &lt;i&gt;&lt;b&gt;false&lt;/b&gt;&lt;/i&gt; condition from LEFT-&amp;gt; RIGHT.&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;/b&gt;checks for every condition even if it finds a false condition. So, here name.length() command got executed and java.lang.NullPointerException got thrown.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: left;&quot; /&gt;
&lt;h4 style=&quot;text-align: center;&quot;&gt;
&lt;u&gt;Difference between &lt;span style=&quot;color: #cc0000;&quot;&gt;||&lt;/span&gt; and &lt;span style=&quot;color: #cc0000;&quot;&gt;|&lt;/span&gt;&lt;/u&gt;&lt;/h4&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://4.bp.blogspot.com/-JDe55HSCzh4/WUZaEBC-EII/AAAAAAAAAmM/1767YgTTfAkmDA41qsztRGv00ZY3xI5FwCLcBGAs/s1600/OR.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;OR Logic Gate&quot; border=&quot;0&quot; data-original-height=&quot;333&quot; data-original-width=&quot;565&quot; height=&quot;235&quot; src=&quot;https://4.bp.blogspot.com/-JDe55HSCzh4/WUZaEBC-EII/AAAAAAAAAmM/1767YgTTfAkmDA41qsztRGv00ZY3xI5FwCLcBGAs/s400/OR.png&quot; title=&quot;OR Logic Gate&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;OR Logic Gate&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Similar to Logical &lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; operator&lt;/b&gt;, &lt;b&gt;Logical &lt;span style=&quot;color: #cc0000;&quot;&gt;||&lt;/span&gt;&lt;/b&gt; operator is known as:&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Logical Operator&lt;/li&gt;
&lt;li&gt;Conditional Operator and&lt;/li&gt;
&lt;li&gt;Logical OR Operator&lt;/li&gt;
&lt;/ol&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;If a Logical OR operation is performed on multiple conditions, &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;||&lt;/b&gt;&lt;/span&gt; stops executing rest of the conditions as soon as it finds a &lt;i&gt;&lt;b&gt;true &lt;/b&gt;&lt;/i&gt;condition&lt;b&gt;. &lt;/b&gt;Because, the result will be &lt;i&gt;&lt;b&gt;true&lt;/b&gt;&lt;/i&gt; anyways. - &lt;span style=&quot;color: red;&quot;&gt;(Opposite to &amp;amp;&amp;amp; operator)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Just like &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;&amp;amp;&amp;amp;&lt;/b&gt;&lt;/span&gt; operator, &lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;||&lt;/span&gt; &lt;/b&gt;operator is defined for Boolean values only.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Similar to Bit-wise &lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&lt;/span&gt; operator&lt;/b&gt;, &lt;span style=&quot;color: #cc0000;&quot;&gt;|&lt;/span&gt; operator is known as:&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Bit-wise Operator&lt;/li&gt;
&lt;li&gt;Unconditional Operator and&lt;/li&gt;
&lt;li&gt;Bit-wise OR Operator&lt;/li&gt;
&lt;/ol&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;If a Bit-wise &lt;b&gt;OR&lt;/b&gt; operation is performed on multiple conditions, unlike&lt;b&gt; &lt;span style=&quot;color: #cc0000;&quot;&gt;||&lt;/span&gt; operator, &lt;span style=&quot;color: #cc0000;&quot;&gt;|&lt;/span&gt; operator &lt;/b&gt;executes every condition even if it finds a &lt;i&gt;&lt;b&gt;true &lt;/b&gt;&lt;/i&gt;condition&lt;b&gt; &lt;/b&gt;in between&lt;b&gt;.&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Just like &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/span&gt; operator, &lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;|&lt;/span&gt; &lt;/b&gt;operator is defined for&lt;b&gt; boolean&lt;/b&gt; and &lt;b&gt;Integer &lt;/b&gt;values only.&lt;b&gt; &lt;/b&gt;And it performs a Logical OR operation on every bit.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;hr style=&quot;text-align: left;&quot; /&gt;
&lt;h4 style=&quot;text-align: center;&quot;&gt;
&lt;u&gt;Conclusion:&lt;/u&gt;&lt;/h4&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
(Note: There are many other Logical as well as bit operators in Java. But, here we discussed only about &lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/b&gt;, &lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;/b&gt;, &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;||&lt;/b&gt; &lt;/span&gt;and &lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;|&lt;/span&gt;&lt;/b&gt; operator. To read further, check the notes on &lt;a href=&quot;http://cs.techruto.com/2017/06/java-shift-operators.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Bit-Shift operators in Java&lt;/a&gt;.)&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;LOGICAL &lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; and &lt;span style=&quot;color: #cc0000;&quot;&gt;||&lt;/span&gt; OPERATOR &lt;/b&gt;&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Both &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;&amp;amp;&amp;amp;&lt;/b&gt;&lt;/span&gt; and &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;||&lt;/b&gt;&lt;/span&gt; operator stops executing remaining expression as soon as it finds the main result. i.e.,&amp;amp;&amp;amp; stops execution as soon as it finds a &lt;i&gt;&lt;b&gt;false&lt;/b&gt;&lt;/i&gt; condition. Whereas, &lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;||&lt;/b&gt;&lt;/span&gt; stops the operation as soon as it find a&lt;i&gt;&lt;b&gt; true &lt;/b&gt;&lt;/i&gt;condition - This short-circuit like nature gives it the name Short-circuit AND and OR operand. &lt;/li&gt;
&lt;li&gt;Both are defined only for Boolean operands. &lt;/li&gt;
&lt;li&gt;Both gives out Boolean output only- i.e.,&lt;i&gt;&lt;b&gt; true, &lt;/b&gt;or&lt;b&gt; false&lt;/b&gt;&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;It&#39;s alias name includes:- Conditional operator, Logical operator, And Short-circuit operator. &lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;BIT-WISE &amp;amp; and | OPERATOR &lt;/b&gt;&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Unlike Logical operators, Both &#39;&lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&amp;amp;&lt;/span&gt;&#39; &lt;/b&gt;and&lt;b&gt; &#39;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;|&lt;/span&gt;&#39;&lt;/b&gt; operator executes every expression. &lt;/li&gt;
&lt;li&gt;These are defined only for &lt;i&gt;&lt;b&gt;boolean&lt;/b&gt;&lt;/i&gt; and&lt;i&gt;&lt;b&gt; Integer (i.e., byte, short, char, int &lt;/b&gt;and&lt;b&gt; long) &lt;/b&gt;&lt;/i&gt;operands.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&amp;amp;&lt;/b&gt; operator performs the &lt;b&gt;AND&lt;/b&gt; operation and &lt;b&gt;| &lt;/b&gt;operator performs the &lt;b&gt;OR&lt;/b&gt; operation on every bit of the input &lt;i&gt;&lt;b&gt;boolean &lt;/b&gt;&lt;/i&gt;or &lt;i&gt;&lt;b&gt;Integer&lt;/b&gt;&lt;/i&gt; data types - Hence the name Bit-wise operator.&lt;/li&gt;
&lt;li&gt;It&#39;s alias name includes:- Unconditional operator, and Bitwise operator. &lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://cs.techruto.com/feeds/1057856940855577084/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=5718802842428101241&amp;postID=1057856940855577084&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5718802842428101241/posts/default/1057856940855577084'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5718802842428101241/posts/default/1057856940855577084'/><link rel='alternate' type='text/html' href='https://cs.techruto.com/2017/06/logical-vs-bitwise-operator-in-java.html' title='Logical operator Vs Bitwise operator in Java : &amp;&amp; vs &amp;, || vs | '/><author><name>Sasuke Naruto</name><uri>http://www.blogger.com/profile/12066467749580493805</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiodJZB1LFPXE0moMcmctZiB2zeNCtF1R5W7d-frodSshVl5P5D8OpxFt6iPmy52-hl1INiTYq7zlckUiSBtk4ajUftMnUdCNNTlWF6-oZmPIjD0yJw5odCfy-012_MA/s220/pp.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://2.bp.blogspot.com/-KfKZTeXFJjQ/WUZUlq5cHcI/AAAAAAAAAl8/NE9AUorZZkEtsFOh0jRDn4N_OSstm02_gCLcBGAs/s72-c/AND.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5718802842428101241.post-6269422465510566640</id><published>2017-06-09T10:14:00.000-07:00</published><updated>2017-06-27T06:32:15.406-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="binary addition"/><category scheme="http://www.blogger.com/atom/ns#" term="bit manipulation"/><category scheme="http://www.blogger.com/atom/ns#" term="core java"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="numeric promotion"/><category scheme="http://www.blogger.com/atom/ns#" term="shift operators"/><title type='text'>Bit shift operators in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;img src=&quot;https://1.bp.blogspot.com/-nLOE-T-fXwI/WTrYyob-BtI/AAAAAAAAAHc/g7ExTbeVelsWvF50SM4qODfHxZxBCn8yACLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B22%253A48%253A10.png&quot; style=&quot;display: none;&quot; /&gt; &lt;span data-log-string=&quot;synonyms-more-click&quot;&gt;Above and beyond&lt;/span&gt; bitwise operators, &lt;abbr title=&quot;Java Language Specification&quot;&gt;JLS&lt;/abbr&gt; provides 3 bit shift operators for bit manipulation in Java. &lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Namely:&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Left Shift Operator (&lt;b&gt;&amp;lt;&amp;lt;&lt;/b&gt;)&lt;/li&gt;
&lt;li&gt;Signed Right Shift Operator (&lt;b&gt;&amp;gt;&amp;gt;&lt;/b&gt;)&lt;/li&gt;
&lt;li&gt;Unsigned Right Shift Operator (&lt;b&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/b&gt;)&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;Syntax&lt;/b&gt;: &lt;b&gt;&lt;code&gt;&lt;span style=&quot;color: orange;&quot;&gt;Left-hand operand&lt;/span&gt; &lt;span style=&quot;color: #6fa8dc;&quot;&gt;SHIFT OPERATOR&lt;/span&gt; &lt;span style=&quot;color: orange;&quot;&gt;Right-hand operand&lt;/span&gt;&lt;/code&gt;&lt;/b&gt; &lt;br /&gt;
Here,&lt;br /&gt;
Left-hand Operand&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?\rightarrow&quot; title=&quot;\rightarrow&quot; /&gt; is the value to be shifted, i.e., shifting of it&#39;s binary value.&lt;br /&gt;
Right-hand Operand&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; specifies the shift distance.&lt;br /&gt;
&lt;br /&gt;
i.e.,&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;code&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt;&lt;span style=&quot;color: orange;&quot;&gt;Value to be shifted&lt;/span&gt; &lt;span style=&quot;color: #6fa8dc;&quot;&gt;SHIFT OPERATOR&lt;/span&gt; &lt;/span&gt;&lt;span style=&quot;color: orange;&quot;&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt;the shift distance.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;
1. Left Shift Operator &quot;&lt;span style=&quot;background-color: lime;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&quot;:&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
It shifts the  binary value of the left operand &lt;span style=&quot;background-color: #fff2cc;&quot;&gt;with sign&lt;/span&gt; to left side and fills the empty place with &lt;span style=&quot;background-color: #fff2cc;&quot;&gt;0&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s solve some question to understand the concept fo Left shift operator:&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Q.1 Solve 20 &amp;lt;&amp;lt; 2 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; shift 20 by 2 bits in the left.&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; The binary representation of 20 is: 00010100&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://3.bp.blogspot.com/-MylPo2MuEjo/WToPE8phBOI/AAAAAAAAAEU/1lPz4g0mAXUVKJ7T-LE8A8rOByUwu0HYgCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B08%253A29%253A01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Logic behind left shift operation 20 &amp;lt;&amp;lt; 2&quot; border=&quot;0&quot; data-original-height=&quot;269&quot; data-original-width=&quot;779&quot; height=&quot;137&quot; src=&quot;https://3.bp.blogspot.com/-MylPo2MuEjo/WToPE8phBOI/AAAAAAAAAEU/1lPz4g0mAXUVKJ7T-LE8A8rOByUwu0HYgCLcB/s400/Screenshot%2Bfrom%2B2017-06-09%2B08%253A29%253A01.png&quot; title=&quot;Logic behind left shift operation 20 &amp;lt;&amp;lt; 2&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 1.1 &lt;/b&gt;- Logic behind left shift operation 20 &amp;lt;&amp;lt; 2&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Therefore, 20 &amp;lt;&amp;lt; 2 = 80. So, &lt;span style=&quot;color: blue;&quot;&gt; &lt;b&gt;Answer = 80.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;background-color: lime;&quot;&gt;Note:&lt;/span&gt; &lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: black;&quot;&gt;If you closely observe:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;System.out.println(10&amp;lt;&amp;lt;2); // output = &lt;/span&gt;&lt;/span&gt;&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?10*2%5E%7B2%7D&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; = 40&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;System.out.println(10&amp;lt;&amp;lt;3); // output = &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?10*2%5E%7B3%7D&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; = 80&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;System.out.println(10&amp;lt;&amp;lt;4); &lt;/span&gt;&lt;/span&gt;// output = &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?10*2%5E%7B4%7D&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; = 160&lt;br /&gt;
&lt;br /&gt;
&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; Left Shift Operator also act&#39;s as &lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?LeftOperand*2%5E%7BRight%20Operand%7D&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt;. But, remember the default size of the data type.&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt; &lt;/span&gt;&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Q. 2. what&#39;s the result of &lt;code&gt; -20 &amp;lt;&amp;lt; 2 &lt;/code&gt;?&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; &lt;code&gt;&lt;/code&gt;Negative integers are stored as it two&#39;s complement in Java.&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;fieldset&gt;
&lt;legend&gt;&amp;nbsp; Revision: &lt;b&gt;Two&#39;s complement&lt;/b&gt; &lt;/legend&gt;&lt;br /&gt;
Two&#39;s complement of a negative integer can be found by Inverting and adding &#39;1&#39; to the binary representation of the integer with -ve sign.&lt;br /&gt;
&lt;br /&gt;
i.e., to reverse the sign you simply invert the bits (0 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; 1, and 1 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; 0) and add &#39;1&#39; to the resulting number.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://4.bp.blogspot.com/-RYX1jnXRg1A/WTp5mBri-zI/AAAAAAAAAE0/S3R1NIjZSisKBNY0yVzNZf6rLUsxd5nVACLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B16%253A03%253A28.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;237&quot; data-original-width=&quot;454&quot; height=&quot;167&quot; src=&quot;https://4.bp.blogspot.com/-RYX1jnXRg1A/WTp5mBri-zI/AAAAAAAAAE0/S3R1NIjZSisKBNY0yVzNZf6rLUsxd5nVACLcB/s320/Screenshot%2Bfrom%2B2017-06-09%2B16%253A03%253A28.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 1.2&lt;/b&gt; - Basic rules of Binary addition&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Even subtraction (A - B) is considered as (A + (-B)) i.e., addition of a binary value &lt;b&gt;&#39;A&#39;&lt;/b&gt; with the two&#39;s complement of another binary value &#39;&lt;b&gt;B&#39;&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://1.bp.blogspot.com/-_-dbvHlTKGU/WTp9Dta-HPI/AAAAAAAAAFA/Ylf1ftLnQVcMbMhR29gjILnRxCmoaV_2QCLcB/s1600/crop1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Basic Addition in Binary&quot; border=&quot;0&quot; data-original-height=&quot;222&quot; data-original-width=&quot;377&quot; height=&quot;188&quot; src=&quot;https://1.bp.blogspot.com/-_-dbvHlTKGU/WTp9Dta-HPI/AAAAAAAAAFA/Ylf1ftLnQVcMbMhR29gjILnRxCmoaV_2QCLcB/s320/crop1.png&quot; title=&quot;Basic Addition in Binary&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 1.3 &lt;/b&gt;- Basic Addition in Binary&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://3.bp.blogspot.com/-igrQqFP78qs/WTp__UN9XhI/AAAAAAAAAFM/vN75Rj-5brIGWF7owRY131eejQk4UO1wwCLcB/s1600/crop2.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Basic Subtraction in Binary&quot; border=&quot;0&quot; data-original-height=&quot;222&quot; data-original-width=&quot;377&quot; height=&quot;187&quot; src=&quot;https://3.bp.blogspot.com/-igrQqFP78qs/WTp__UN9XhI/AAAAAAAAAFM/vN75Rj-5brIGWF7owRY131eejQk4UO1wwCLcB/s320/crop2.png&quot; title=&quot;Basic Subtraction in Binary&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 1.4&lt;/b&gt; - Basic Subtraction in Binary&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Suppose, we&#39;re working with 8-bit binary values (for simple calculation)  and we want to find how -20 would be expressed in two&#39;s  complement notation.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 1&lt;/b&gt;: &amp;nbsp; Write the integer value (here 20) in it&#39;s binary form:&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://4.bp.blogspot.com/-POLlnKkcHSU/WTqCzZrFGYI/AAAAAAAAAFY/fFvMLhKd3k4WfeklOb5iGQsgUF92grNDgCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B16%253A42%253A37.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Representation of 20 in it&#39;s binary form&quot; border=&quot;0&quot; data-original-height=&quot;76&quot; data-original-width=&quot;391&quot; height=&quot;62&quot; src=&quot;https://4.bp.blogspot.com/-POLlnKkcHSU/WTqCzZrFGYI/AAAAAAAAAFY/fFvMLhKd3k4WfeklOb5iGQsgUF92grNDgCLcB/s320/Screenshot%2Bfrom%2B2017-06-09%2B16%253A42%253A37.png&quot; title=&quot;Representation of 20 in it&#39;s binary form&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 1.5&lt;/b&gt; : Representation of 20 in it&#39;s binary form i.e., &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2820%29_%7B10%7D&quot; title=&quot;(20)_{10}&quot; /&gt; =&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%280&amp;amp;space;0&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;0%29_%7B2%7D&quot; title=&quot;(0 0 0 1 0 1 0 0)_{2}&quot; /&gt;&amp;nbsp;&amp;nbsp; &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
i.e., &lt;img src=&quot;https://latex.codecogs.com/gif.latex?(20)_{10}&quot; title=&quot;(20)_{10}&quot; /&gt; =&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?(0&amp;amp;space;0&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;0)_{2}&quot; title=&quot;(0 0 0 1 0 1 0 0)_{2}&quot; /&gt; &amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Step 2&lt;/b&gt;: Then we invert the digits.  0 becomes 1, and 1 becomes 0:&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;pre&gt;1 1 1 0 1 0 1 1 &lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Step 3&lt;/b&gt;: Then we add &#39;1&#39;:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://1.bp.blogspot.com/-DeutnJGpHBI/WTqGwZFNdEI/AAAAAAAAAFw/xljGMWsJuoAtbLuNGxQyl9ChiOi8-eYYQCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B16%253A59%253A48.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;calculation for two&#39;s complement of -20&quot; border=&quot;0&quot; data-original-height=&quot;177&quot; data-original-width=&quot;252&quot; src=&quot;https://1.bp.blogspot.com/-DeutnJGpHBI/WTqGwZFNdEI/AAAAAAAAAFw/xljGMWsJuoAtbLuNGxQyl9ChiOi8-eYYQCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B16%253A59%253A48.png&quot; title=&quot;calculation for two&#39;s complement of -20&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://2.bp.blogspot.com/-QSNPVhSeDag/WTqFl9ml1uI/AAAAAAAAAFk/OD17gUcAoCwqBVU7w5kQ_lDCDu8iW5SeQCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B16%253A54%253A53.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;
&lt;/a&gt;&lt;/div&gt;
So, we represent &lt;img src=&quot;https://latex.codecogs.com/gif.latex?(-20)_{10}&quot; title=&quot;(-20)_{10}&quot; /&gt; as &lt;img src=&quot;https://latex.codecogs.com/gif.latex?(1&amp;amp;space;1&amp;amp;space;1&amp;amp;space;0&amp;amp;space;1&amp;amp;space;1&amp;amp;space;0&amp;amp;space;0)_{2}&quot; title=&quot;(1 1 1 0 1 1 0 0)_{2}&quot; /&gt; . So, &lt;span style=&quot;color: blue;&quot;&gt; &lt;b&gt;Answer = 11101100.&lt;/b&gt; &lt;/span&gt;&lt;br /&gt;
&lt;pre&gt;&lt;/pre&gt;
That is how one would write -20 in 8-bit binary.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: lime;&quot;&gt;Note&lt;/span&gt;: MSB tells us whether the binary representation is of a -ve or +ve Integer. &lt;br /&gt;
&lt;br /&gt;
if &lt;b&gt;MSB&lt;/b&gt; = 0 &lt;b&gt;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&lt;/b&gt; it represents a +ve integer, and&lt;br /&gt;
if &lt;b&gt;MSB&lt;/b&gt; = 1 &lt;b&gt;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&lt;/b&gt; it represents a -ve integer.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;u&gt;Conversion from Two&#39;s Complement&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Let&#39;s understand this with the help of above example. Let&#39;s convert the two&#39;s complement &lt;img src=&quot;https://latex.codecogs.com/gif.latex?(11101100)_{2}&quot; title=&quot;(11101100)_{2}&quot; /&gt; back to it&#39;s original form.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 1&lt;/b&gt;: Here, MSB = 1. So, we can conclude that it is a binary representation of a &lt;span style=&quot;background-color: cyan;&quot;&gt;&lt;b&gt;-ve&lt;/b&gt;&lt;/span&gt; integer.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: #cfe2f3;&quot;&gt;Rule #1:&lt;/span&gt; to reverse the sign you simply invert the bits (0 goes to 1, and 1 to 0) and add one to the resulting number.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 2:&lt;/b&gt; Invert the digits.  0 becomes 1, and 1 becomes 0:&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
0 0 0 1 0 0 1 1&amp;nbsp; &lt;/div&gt;
&lt;b&gt;Step 3&lt;/b&gt;: Add &#39;1&#39;:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://2.bp.blogspot.com/-2am3QxciSUQ/WTqOkkaPEXI/AAAAAAAAAGA/Yba2z-lSOSkG1WXoguDZ1xNNgDLU_IMbwCLcB/s1600/crop3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Conversion from two&#39;s complement&quot; border=&quot;0&quot; data-original-height=&quot;308&quot; data-original-width=&quot;528&quot; height=&quot;186&quot; src=&quot;https://2.bp.blogspot.com/-2am3QxciSUQ/WTqOkkaPEXI/AAAAAAAAAGA/Yba2z-lSOSkG1WXoguDZ1xNNgDLU_IMbwCLcB/s320/crop3.jpg&quot; title=&quot;Conversion from two&#39;s complement&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
So, from results found by step 1 and step 3. We found that &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811101100%29_%7B2%7D&quot; title=&quot;(11101100)_{2}&quot; /&gt; = &lt;span style=&quot;background-color: cyan;&quot;&gt;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%28-20%29_%7B10%7D&quot; title=&quot;(-20)_{10}&quot; /&gt;&lt;span style=&quot;background-color: white;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: cyan;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;Answer = -20.&lt;/b&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/fieldset&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
So, let&#39;s convert -20 to it&#39;s binary equivalent:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://1.bp.blogspot.com/-EWbTUer4tTg/WTqTT-YpMDI/AAAAAAAAAGM/dsD0M7EI-o8BgEKn_uiW1fOZ4N6-pdoNQCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B17%253A53%253A16.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Two&#39;s complemet of -20&quot; border=&quot;0&quot; data-original-height=&quot;250&quot; data-original-width=&quot;355&quot; height=&quot;225&quot; src=&quot;https://1.bp.blogspot.com/-EWbTUer4tTg/WTqTT-YpMDI/AAAAAAAAAGM/dsD0M7EI-o8BgEKn_uiW1fOZ4N6-pdoNQCLcB/s320/Screenshot%2Bfrom%2B2017-06-09%2B17%253A53%253A16.png&quot; title=&quot;Two&#39;s complemet of -20&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Now, let&#39;s shift it to left with shift distance = 2, as mentioned in Q.2.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://1.bp.blogspot.com/-KHK-Cy_U9xQ/WTqXYYmY6rI/AAAAAAAAAGc/RmOMtXzWZjI50QFq8SDbkjHrEjORsR9UgCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B18%253A10%253A45.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;-20 &amp;lt;&amp;lt; 2&quot; border=&quot;0&quot; data-original-height=&quot;325&quot; data-original-width=&quot;870&quot; height=&quot;148&quot; src=&quot;https://1.bp.blogspot.com/-KHK-Cy_U9xQ/WTqXYYmY6rI/AAAAAAAAAGc/RmOMtXzWZjI50QFq8SDbkjHrEjORsR9UgCLcB/s400/Screenshot%2Bfrom%2B2017-06-09%2B18%253A10%253A45.png&quot; title=&quot;-20 &amp;lt;&amp;lt; 2&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Since, the given integer was represented in 8 bits, we will only consider 8 bits for now. So, we got 1 0 1 1 0 0 0 0 &lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
since MSB = 1. It implies, this binary value is a representation of a &lt;span style=&quot;background-color: cyan;&quot;&gt;-ve integer.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
After inverting the digits &lt;b&gt;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; 0 1 0 0 1 1 1 1&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
After adding 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;b&gt;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&lt;/b&gt; 0 1 0 1 0 0 0 0&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;b&gt;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; &lt;/b&gt;128*0+ 64*1&amp;nbsp;+ 32*0&amp;nbsp;+ 16*1&amp;nbsp;+ 8*0&amp;nbsp;+ 4*0&amp;nbsp;+ 2*0&amp;nbsp;+ 1*0 = 64&amp;nbsp;+ 16 = &lt;span style=&quot;background-color: cyan;&quot;&gt;80&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
So, since we found that it&#39;s a -ve integer =&amp;gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;b&gt;Answer = -80&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;
Q. 3. what&#39;s the result of &lt;code&gt;20 &amp;lt;&amp;lt; -2 &lt;/code&gt;?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&amp;nbsp;&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Here the shift distance is &quot;-2&quot;. So, this expression is considered undefined in C. But, in Java using Netbeans, we got an output for this expression. And Let&#39;s find it out how:&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
2 is represented in binary form as =&amp;gt; 00000010&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Inverting it =&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11111101&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
After adding &#39;1&#39; =&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11111110&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
So, -2 is represented as 11111110 in binary form.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: lime;&quot;&gt;&lt;b&gt;Note&lt;/b&gt;:&lt;/span&gt; According to JSL:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.&lt;/li&gt;
&lt;li&gt;If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator with the mask value 0x3f (0b111111). The shift distance actually used is therefore always in the range 0 to 63, inclusive. &lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;/ol&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
So, the last 5 bits of 11111110 is &lt;b&gt;11110&lt;/b&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
After bitwise AND operation we get&amp;nbsp; &lt;b&gt;11110. &lt;/b&gt;In integer form it&#39;s represented as &lt;b&gt;30.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?\therefore&quot; title=&quot;\therefore&quot; /&gt; Shift distance = 30.&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?\Rightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; If we will shift &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%280&amp;amp;space;0&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;0%29_%7B2%7D&quot; title=&quot;(0 0 0 1 0 1 0 0)_{2}&quot; /&gt;, 30 times to left. We will get 32 0&#39;s. As 20 is a positive integer. And the size of &lt;b&gt;int&lt;/b&gt; in java is 32 bit. &lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; Answer = 0&lt;/b&gt;.&lt;/span&gt; &lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Q. 4. what&#39;s the result of &lt;code&gt;-20 &amp;lt;&amp;lt; -2 &lt;/code&gt;?&lt;/h4&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Two&#39;s complement of -20 =&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811101100%29_%7B2%7D&quot; title=&quot;(11101100)_{2}&quot; /&gt;.&lt;br /&gt;
Two&#39;s complement of -2 =&amp;nbsp;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?(11111110)_{2}&quot; title=&quot;(11111110)_{2}&quot; /&gt;.&lt;br /&gt;
After subjecting the last 5 bits of 11111110 to bitwise AND operation with the mask 0b11111 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; we get 11110 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; it is the binary representation of 30.&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; shift distance = 30.&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; If we will shift &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811101100%29_%7B2%7D&quot; title=&quot;(11101100)_{2}&quot; /&gt;, 30 times to left. We will get 32 0&#39;s. As -20 is an integer. And the size of&lt;b&gt; int&lt;/b&gt; in java is 32 bit. &lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; Answer = 0&lt;/b&gt;&lt;span style=&quot;color: black;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;TEST CODE FOR ANALYSING LEFT SHIFT OPERATOR&lt;/b&gt;&lt;/span&gt; &lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: black; color: white; padding: 17px;&quot;&gt;
&lt;b&gt;public&lt;/b&gt; &lt;span style=&quot;color: #cfe2f3;&quot;&gt;class&lt;/span&gt;&lt;b&gt; &lt;/b&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ShiftOpptn&lt;/span&gt;&lt;b&gt; &lt;/b&gt;{&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;public&lt;/b&gt; &lt;span style=&quot;color: #f4cccc;&quot;&gt;static&lt;/span&gt; void &lt;span style=&quot;color: #ffd966;&quot;&gt;main&lt;/span&gt;(String[] args) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: lime;&quot;&gt; /* Left shift operator */&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;20 &amp;lt;&amp;lt; 2&lt;/span&gt;&lt;/b&gt;);&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: lime;&quot;&gt;// output = 80 &lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;20 &amp;lt;&amp;lt; 2&lt;/span&gt;&lt;/b&gt;));&lt;span style=&quot;color: lime;&quot;&gt;&amp;nbsp;&amp;nbsp; // 1010000&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;-20 &amp;lt;&amp;lt; 2&lt;/span&gt;&lt;/b&gt;);&amp;nbsp; &lt;span style=&quot;color: lime;&quot;&gt;// -80&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;-20 &amp;lt;&amp;lt; 2&lt;/span&gt;)&lt;/b&gt;);&amp;nbsp; &lt;span style=&quot;color: lime;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: lime;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // output =11111111111111111111111110110000&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20 &amp;lt;&amp;lt; -2&lt;/b&gt;&lt;/span&gt;);&amp;nbsp; &lt;span style=&quot;color: lime;&quot;&gt;//&amp;nbsp; 0&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;20 &amp;lt;&amp;lt; -2&lt;/span&gt;&lt;/b&gt;));&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: lime;&quot;&gt;//&amp;nbsp; 0&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;lt;&amp;lt; -2&lt;/b&gt;&lt;/span&gt;); &lt;span style=&quot;color: lime;&quot;&gt;//&amp;nbsp; 0&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;-20 &amp;lt;&amp;lt; -2&lt;/span&gt;&lt;/b&gt;));&amp;nbsp; &lt;span style=&quot;color: lime;&quot;&gt;// 0&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Output:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://1.bp.blogspot.com/-g2mbQjUGIHM/WTqvwf-I3pI/AAAAAAAAAGs/MrcFiZNKXdEzL1nourElJlRl6wWQObxywCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B19%253A44%253A19.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Output produced for above code in Netbeans IDE&quot; border=&quot;0&quot; data-original-height=&quot;190&quot; data-original-width=&quot;406&quot; height=&quot;149&quot; src=&quot;https://1.bp.blogspot.com/-g2mbQjUGIHM/WTqvwf-I3pI/AAAAAAAAAGs/MrcFiZNKXdEzL1nourElJlRl6wWQObxywCLcB/s320/Screenshot%2Bfrom%2B2017-06-09%2B19%253A44%253A19.png&quot; title=&quot;Output produced for above code in Netbeans IDE&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 1.5&lt;/b&gt; - Output produced for above code in Netbeans IDE&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;
&amp;nbsp;2. Signed Right shift operator &quot;&lt;span style=&quot;background-color: lime;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&quot; &lt;/b&gt;&lt;/div&gt;
It shifts the  binary value of the left operand &lt;span style=&quot;background-color: #fff2cc;&quot;&gt;with sign&lt;/span&gt; to RIGHT side and fills the empty place with the &lt;span style=&quot;background-color: #fff2cc;&quot;&gt;value of SIGN BIT&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
For +ve (positive) number it fills with 0. And,&lt;br /&gt;
for -ve (negative) number it fills with 1.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&amp;nbsp; &lt;b&gt;20 &amp;gt;&amp;gt; 2&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-5DsGQtJz-pc/WTotPPy_rZI/AAAAAAAAAEo/jSj9nYAA9Iwpf7hMWwrLYWbpeX-spkZDgCK4B/s1600/Screenshot%2Bfrom%2B2017-06-09%2B08%253A29%253A01%2B%2528copy%2529.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;137&quot; src=&quot;https://4.bp.blogspot.com/-5DsGQtJz-pc/WTotPPy_rZI/AAAAAAAAAEo/jSj9nYAA9Iwpf7hMWwrLYWbpeX-spkZDgCK4B/s400/Screenshot%2Bfrom%2B2017-06-09%2B08%253A29%253A01%2B%2528copy%2529.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 2.1 &lt;/b&gt;- Logic behind signed right shift operation 20 &amp;gt;&amp;gt; 2&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
Therefore,&lt;b&gt; 20 &amp;gt;&amp;gt; 2 = 5 . &lt;/b&gt;And it is a +ve integer. So, rest of the empty space will be filled with the sign value i.e., 0.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; &lt;span style=&quot;color: blue;&quot;&gt;5&lt;/span&gt; will be represented as&amp;nbsp; &lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;b&gt;00000000 00000000 00000000 00000101&lt;/b&gt;&lt;/span&gt; &lt;/code&gt; in binary.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Q.1. What&#39;s the result of -20 &amp;gt;&amp;gt; 2 ?&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Two&#39;s complement of -20 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811101100%29_%7B2%7D&quot; title=&quot;(11101100)_{2}&quot; /&gt;.&lt;br /&gt;
Since, the LHS operand is a -ve integer. The blank space will be filled by the -ve sign value &#39;1&#39;.&lt;br /&gt;
&lt;br /&gt;
So,&amp;nbsp; after shifting 2 bits right &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; 11111011 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; two&#39;s complement of -5.&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; -20 &amp;gt;&amp;gt; 2 = &lt;span style=&quot;color: blue;&quot;&gt;-5&lt;/span&gt;, which is represented as &lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;11111111 11111111 11111111 &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;11111011&lt;/b&gt;&lt;/span&gt; &lt;/code&gt; in binary.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Q.2. What&#39;s the result of 20 &amp;gt;&amp;gt; -2 ?&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Binary form of 20 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%280&amp;amp;space;0&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;0%29_%7B2%7D&quot; title=&quot;(0 0 0 1 0 1 0 0)_{2}&quot; /&gt;&lt;br /&gt;
Two&#39;s complement of -2 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811111110%29_%7B2%7D&quot; title=&quot;(11111110)_{2}&quot; /&gt;.&lt;br /&gt;
Since, the LHS operand is a +ve integer. The blank space will be filled by the +ve sign value i.e., &#39;0&#39;.&lt;br /&gt;
&lt;br /&gt;
So,&amp;nbsp; by JLS&#39;s specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.&lt;br /&gt;
&lt;br /&gt;
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; After shifting the binary of LHS operand to 30 bits right &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; 00000000 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; 0. &lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; 20 &amp;gt;&amp;gt; -2 = &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;, which is represented as &lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;00000000 00000000 00000000 &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;00000000&lt;/b&gt;&lt;/span&gt; &lt;/code&gt; in binary.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;
Q.3. What&#39;s the result of -20 &amp;gt;&amp;gt; -2 ?&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Two&#39;s complement of -20 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811101100%29_%7B2%7D&quot; title=&quot;(11101100)_{2}&quot; /&gt;&lt;br /&gt;
Two&#39;s complement of -2 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811111110%29_%7B2%7D&quot; title=&quot;(11111110)_{2}&quot; /&gt;.&lt;br /&gt;
Since, the LHS operand is a -ve integer. The blank space will be filled by the -ve sign value i.e., &#39;1&#39;.&lt;br /&gt;
&lt;br /&gt;
So,&amp;nbsp;  by JLS&#39;s specified rule for shift operator. least 5 digits of the RHS  operand is passed through bitwise AND operation with the MASK value  0b11111.&lt;br /&gt;
&lt;br /&gt;
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; After shifting the binary of LHS operand to 30 bits right &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; 11111111 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; Two&#39;s complement of -1. &lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; -20 &amp;gt;&amp;gt; -2 = &lt;span style=&quot;color: blue;&quot;&gt;-1&lt;/span&gt;, which is represented as &lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;11111111 11111111 11111111 &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;11111111&lt;/b&gt;&lt;/span&gt; &lt;/code&gt; in binary.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;background-color: lime;&quot;&gt;Note:&lt;/span&gt; &lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: black;&quot;&gt;If you closely observe:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;System.out.println(10&amp;lt;&amp;lt;2); // output =&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Cfrac%7B10%7D%7B2%5E%7B2%7D%7D%20%3D%20%5Cfrac%7B10%7D%7B4%7D%3D%202&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; =&amp;gt; 2 &lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;System.out.println(10&amp;lt;&amp;lt;3); // output = &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Cfrac%7B10%7D%7B2%5E3%7D%20%3D%20%5Cfrac%7B10%7D%7B8%7D%20%3D%201&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; =&amp;gt; 1 &lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;System.out.println(10&amp;lt;&amp;lt;4); &lt;/span&gt;&lt;/span&gt;// output = &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Cfrac%7B10%7D%7B2%5E4%7D%20%3D%20%5Cfrac%7B10%7D%7B16%7D%20%3D%200&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; =&amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
&lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt; Right Shift Operator also act&#39;s as &lt;img id=&quot;equationview&quot; name=&quot;equationview&quot; src=&quot;https://latex.codecogs.com/gif.latex?%5Cfrac%7BLeftOperand%7D%7B2%5E%7BRight%20Operand%7D%7D&quot; title=&quot;This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.&quot; /&gt;. But, remember the default size of the data type.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;TEST CODE FOR ANALYSING RIGHT SHIFT OPERATOR&lt;/b&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div style=&quot;background-color: black; color: white; padding: 17px;&quot;&gt;
&lt;b&gt;public&lt;/b&gt; &lt;span style=&quot;color: #cfe2f3;&quot;&gt;class&lt;/span&gt;&lt;b&gt; &lt;/b&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ShiftOpptn&lt;/span&gt;&lt;b&gt; &lt;/b&gt;{&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;public&lt;/b&gt; &lt;span style=&quot;color: #f4cccc;&quot;&gt;static&lt;/span&gt; void &lt;span style=&quot;color: #ffd966;&quot;&gt;main&lt;/span&gt;(String[] args) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: lime;&quot;&gt; /* Signed Right shift operator */&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #ffd966;&quot;&gt;&lt;span style=&quot;color: #f4cccc;&quot;&gt;1. 20 in binary =&lt;/span&gt; &lt;/span&gt;&quot;+&lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20&lt;/b&gt;&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #ea9999;&quot;&gt;&lt;span style=&quot;color: #f4cccc;&quot;&gt;2. 20 &amp;gt;&amp;gt; 2 =&lt;/span&gt; &lt;/span&gt;&quot;+ (&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20 &amp;gt;&amp;gt; 2&lt;/b&gt;&lt;/span&gt;));&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;Binary = &lt;/span&gt;&quot;+ &lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;20 &amp;gt;&amp;gt; 2&lt;/span&gt;&lt;/b&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;3. -20 &amp;gt;&amp;gt; 2 = &lt;/span&gt;&quot;+ (&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;gt;&amp;gt; 2&lt;/b&gt;&lt;/span&gt;)); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;Binary = &lt;/span&gt;&quot;+ &lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;gt;&amp;gt; 2&lt;/b&gt;&lt;/span&gt;)); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;4. 20 &amp;gt;&amp;gt; -2 = &lt;/span&gt;&quot;+ (&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20 &amp;gt;&amp;gt; -2&lt;/b&gt;&lt;/span&gt;));&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;Binary = &lt;/span&gt;&quot;+ &lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20 &amp;gt;&amp;gt; -2&lt;/b&gt;&lt;/span&gt;));&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;5. -20 &amp;gt;&amp;gt; -2 = &lt;/span&gt;&quot;+ (&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;gt;&amp;gt; -2&lt;/b&gt;&lt;/span&gt;)); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;Binary = &lt;/span&gt;&quot;+ &lt;i&gt;Integer&lt;span style=&quot;color: cyan;&quot;&gt;.&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;toBinaryString&lt;/span&gt;(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;gt;&amp;gt; -2&lt;/b&gt;&lt;/span&gt;));&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Output:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://4.bp.blogspot.com/-QIXJU731QMk/WTrHknMsNOI/AAAAAAAAAG8/FBrdQRBndSkvTNnxUHwjjqdca1jBiZQuQCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B21%253A36%253A18.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Output produced for above code in Netbeans IDE&quot; border=&quot;0&quot; data-original-height=&quot;201&quot; data-original-width=&quot;404&quot; height=&quot;159&quot; src=&quot;https://4.bp.blogspot.com/-QIXJU731QMk/WTrHknMsNOI/AAAAAAAAAG8/FBrdQRBndSkvTNnxUHwjjqdca1jBiZQuQCLcB/s320/Screenshot%2Bfrom%2B2017-06-09%2B21%253A36%253A18.png&quot; title=&quot;Output produced for above code in Netbeans IDE&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig 2.2&lt;/b&gt; Output produced for above code in Netbeans IDE&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt; &lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;3. Unsigned right shift operator &quot;&lt;span style=&quot;background-color: lime;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&quot;&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
It shifts the  binary value of the left operand &lt;span style=&quot;background-color: #fff2cc;&quot;&gt;without preserving SIGN&lt;/span&gt; to RIGHT side and fills the empty place with &lt;span style=&quot;background-color: #fff2cc;&quot;&gt;0&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
So, It is also known as &lt;span style=&quot;background-color: yellow;&quot;&gt;unsigned right shift operator&lt;/span&gt;, and &lt;span style=&quot;background-color: yellow;&quot;&gt;right shift with zero fill&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Q.1. What&#39;s the result of 20 &amp;gt;&amp;gt; 2 ?&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Two&#39;s complement of 20 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%280&amp;amp;space;0&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;0%29_%7B2%7D&quot; title=&quot;(0 0 0 1 0 1 0 0)_{2}&quot; /&gt;.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; is an unsigned right shift operator. So, the blank space will be filled by &#39;0&#39;.&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt;&amp;nbsp; after shifting 2 bits right &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; (00000000 00000000 00000000 00000101)&lt;sub&gt;2&lt;/sub&gt; = (5)&lt;sub&gt;10.&lt;/sub&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; 20 &amp;gt;&amp;gt; 2 = &lt;span style=&quot;color: blue;&quot;&gt;5&lt;/span&gt;, which is represented as &lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;00000000 &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;00000000&lt;/b&gt;&lt;/code&gt;&lt;/span&gt; &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;00000000&lt;/b&gt;&lt;/code&gt;&lt;/span&gt; &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;00000101&lt;/b&gt;&lt;/code&gt;&lt;/span&gt; &lt;/code&gt; in binary.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;
Q.2. What&#39;s the result of -20 &amp;gt;&amp;gt; 2 ?&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Two&#39;s complement of -20 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811101100%29_%7B2%7D&quot; title=&quot;(11101100)_{2}&quot; /&gt;.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; is an unsigned right shift operator. So, the blank space will be filled by&amp;nbsp; &#39;0&#39;.&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt;&amp;nbsp; after shifting 2 bits right &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; (00&lt;span style=&quot;color: red;&quot;&gt;111111111111111111111111111011&lt;/span&gt;)&lt;sub&gt;2&lt;/sub&gt; = (1073741819)&lt;sub&gt;10.&lt;/sub&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; -20 &amp;gt;&amp;gt; 2 = &lt;span style=&quot;color: blue;&quot;&gt;1073741819&lt;/span&gt;, which is represented as &lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;00111111 11111111 11111111 &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;11111011&lt;/b&gt;&lt;/span&gt; &lt;/code&gt; in binary.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Q.3. What&#39;s the result of 20 &amp;gt;&amp;gt; -2 ?&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Binary form of 20 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%280&amp;amp;space;0&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;1&amp;amp;space;0&amp;amp;space;0%29_%7B2%7D&quot; title=&quot;(0 0 0 1 0 1 0 0)_{2}&quot; /&gt;&lt;br /&gt;
Two&#39;s complement of -2 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811111110%29_%7B2%7D&quot; title=&quot;(11111110)_{2}&quot; /&gt;.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; is an unsigned right shift operator. So, the blank space will be filled by &#39;0&#39;.&lt;br /&gt;
&lt;br /&gt;
So,&amp;nbsp;  by JLS&#39;s specified rule for shift operator. least 5 digits of the RHS  operand is passed through bitwise AND operation with the MASK value  0b11111.&lt;br /&gt;
&lt;br /&gt;
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; After shifting the binary of LHS operand to 30 bits right &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; 00000000 &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; 0. &lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; 20 &amp;gt;&amp;gt; -2 = &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;, which is represented as &lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;00000000 00000000 00000000 &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;00000000&lt;/b&gt;&lt;/span&gt; &lt;/code&gt; in binary.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;
Q.4. What&#39;s the result of -20 &amp;gt;&amp;gt; -2 ?&lt;/b&gt;&lt;/div&gt;
&lt;i&gt;&lt;u&gt;&lt;b&gt;Sol.&lt;/b&gt;&lt;/u&gt;&lt;/i&gt; Two&#39;s complement of -20 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811101100%29_%7B2%7D&quot; title=&quot;(11101100)_{2}&quot; /&gt;&lt;br /&gt;
Two&#39;s complement of -2 = &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%2811111110%29_%7B2%7D&quot; title=&quot;(11111110)_{2}&quot; /&gt;.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; is an unsigned right shift operator. So, the blank space will be filled by &#39;0&#39;.&lt;br /&gt;
&lt;br /&gt;
So,&amp;nbsp;  by JLS&#39;s specified rule for shift operator. least 5 digits of the RHS  operand is passed through bitwise AND operation with the MASK value  0b11111.&lt;br /&gt;
&lt;br /&gt;
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; After shifting the binary of LHS operand to 30 bits right &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5CRightarrow&quot; title=&quot;\Rightarrow&quot; /&gt; (000000000000000000000000000000&lt;span style=&quot;color: red;&quot;&gt;11&lt;/span&gt;)&lt;sub&gt;2&lt;/sub&gt; = (3)&lt;sub&gt;10&lt;/sub&gt;&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Ctherefore&quot; title=&quot;\therefore&quot; /&gt; -20 &amp;gt;&amp;gt; -2 = &lt;span style=&quot;color: blue;&quot;&gt;3&lt;/span&gt;, which is represented as &lt;span style=&quot;color: blue;&quot;&gt;&lt;code&gt;&lt;b&gt;00000000 00000000 00000000 &lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;00000011&lt;/b&gt;&lt;/span&gt; &lt;/code&gt; in binary.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;TEST CODE FOR ANALYSING UNSIGNED RIGHT SHIFT OPERATOR&lt;/b&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div style=&quot;background-color: black; color: white; padding: 17px;&quot;&gt;
&lt;b&gt;public&lt;/b&gt; &lt;span style=&quot;color: #cfe2f3;&quot;&gt;class&lt;/span&gt;&lt;b&gt; &lt;/b&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ShiftOpptn&lt;/span&gt;&lt;b&gt; &lt;/b&gt;{&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;public&lt;/b&gt; &lt;span style=&quot;color: #f4cccc;&quot;&gt;static&lt;/span&gt; void &lt;span style=&quot;color: #ffd966;&quot;&gt;main&lt;/span&gt;(String[] args) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: lime;&quot;&gt; /* Unsigned Right shift operator &quot;&amp;gt;&amp;gt;&amp;gt;&quot; */&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #ffd966;&quot;&gt;&lt;span style=&quot;color: #f4cccc;&quot;&gt;1. 20 in binary =&lt;/span&gt; &lt;/span&gt;&quot;+&lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20&lt;/b&gt;&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #ea9999;&quot;&gt;&lt;span style=&quot;color: #f4cccc;&quot;&gt;2. 20 &amp;gt;&amp;gt;&amp;gt; 2 =&lt;/span&gt; &lt;/span&gt;&quot;+ (&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20 &amp;gt;&amp;gt;&amp;gt; 2&lt;/b&gt;&lt;/span&gt;));&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;Binary = &lt;/span&gt;&quot;+ &lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;20 &amp;gt;&amp;gt;&amp;gt; 2&lt;/span&gt;&lt;/b&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;3. -20 &amp;gt;&amp;gt;&amp;gt; 2 = &lt;/span&gt;&quot;+ (&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;gt;&amp;gt;&amp;gt; 2&lt;/b&gt;&lt;/span&gt;)); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;Binary = &lt;/span&gt;&quot;+ &lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;gt;&amp;gt;&amp;gt; 2&lt;/b&gt;&lt;/span&gt;)); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;4. 20 &amp;gt;&amp;gt;&amp;gt; -2 = &lt;/span&gt;&quot;+ (&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20 &amp;gt;&amp;gt;&amp;gt; -2&lt;/b&gt;&lt;/span&gt;));&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;Binary = &lt;/span&gt;&quot;+ &lt;i&gt;Integer&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;.toBinaryString&lt;/span&gt;(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;20 &amp;gt;&amp;gt;&amp;gt; -2&lt;/b&gt;&lt;/span&gt;));&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;5. -20 &amp;gt;&amp;gt;&amp;gt; -2 = &lt;/span&gt;&quot;+ (&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;gt;&amp;gt;&amp;gt; -2&lt;/b&gt;&lt;/span&gt;)); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&lt;span style=&quot;color: #f4cccc;&quot;&gt;Binary = &lt;/span&gt;&quot;+ &lt;i&gt;Integer&lt;span style=&quot;color: cyan;&quot;&gt;.&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;color: cyan;&quot;&gt;toBinaryString&lt;/span&gt;(&lt;span style=&quot;color: yellow;&quot;&gt;&lt;b&gt;-20 &amp;gt;&amp;gt;&amp;gt; -2&lt;/b&gt;&lt;/span&gt;));&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Output:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://1.bp.blogspot.com/-F_awngt2Nyo/WTrPkkzkSII/AAAAAAAAAHM/Y8lvj4ydgLo8ufV_IB-YGVkfBLlNvXV7wCLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B22%253A10%253A29.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;The output produced by above code in Netbeans IDE&quot; border=&quot;0&quot; data-original-height=&quot;205&quot; data-original-width=&quot;415&quot; height=&quot;158&quot; src=&quot;https://1.bp.blogspot.com/-F_awngt2Nyo/WTrPkkzkSII/AAAAAAAAAHM/Y8lvj4ydgLo8ufV_IB-YGVkfBLlNvXV7wCLcB/s320/Screenshot%2Bfrom%2B2017-06-09%2B22%253A10%253A29.png&quot; title=&quot;The output produced by above code in Netbeans IDE&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig 3.1&lt;/b&gt; The output produced by above code in Netbeans IDE&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt; &lt;b&gt;&lt;br /&gt;
&lt;/b&gt; &lt;br /&gt;
&lt;hr /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;/ul&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;More on bit-shift operators &lt;/b&gt;&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The &lt;span style=&quot;background-color: yellow;&quot;&gt;shift operators are left-associative&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;fieldset&gt;
&lt;legend&gt;Did you know?&lt;/legend&gt;ln programming language, &lt;b&gt;associativity&lt;/b&gt;&lt;span style=&quot;color: #6fa8dc;&quot;&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/span&gt; (a.k.a &lt;b&gt;fixity&lt;/b&gt;) of an operatorproperty that determines how operators of the same &lt;a href=&quot;https://en.wikipedia.org/wiki/Order_of_operations&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; title=&quot;Order of operations&quot;&gt;precedence&lt;/a&gt; are grouped in the absence of &lt;a href=&quot;https://en.wikipedia.org/wiki/Bracket_%28mathematics%29&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; title=&quot;Bracket (mathematics)&quot;&gt;parentheses&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
If there are two operators with same precedence before and after an operand then the choice of which operations to apply the operand to, is determined by the &quot;associativity&quot; of the operators.&lt;br /&gt;
&lt;br /&gt;
Operators may be &lt;b&gt;associative&lt;/b&gt; (meaning the operations can be grouped arbitrarily), &lt;b&gt;left-associative&lt;/b&gt; (meaning the operations are grouped from the left), &lt;b&gt;right-associative&lt;/b&gt; (meaning the operations are grouped from the right) or &lt;b&gt;non-associative&lt;/b&gt; (meaning operations cannot be chained, often because the output type is incompatible with the input types). &lt;br /&gt;
&lt;br /&gt;
different programming languages may have different associativity and precedence for the same type of operator.&lt;br /&gt;
&lt;br /&gt;
Consider the expression a ~ b ~ c. If the operator ~ has left associativity, this expression would be interpreted as (a ~ b) ~ c. If the operator has right associativity, the expression would be interpreted as a ~ (b ~ c). If the operator is non-associative, the expression might be a syntax error, or it might have some special meaning. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some mathematical operators have inherent associativity. For example, subtraction and division, as used in conventional math notation, are inherently left-associative. Addition and multiplication, by contrast, are both left and right associative. (e.g. (a * b) * c = a * (b * c)).&lt;br /&gt;
&lt;br /&gt;
The concept of notational associativity described here is related to, but different from the mathematical associativity. An operation that is mathematically associative, by definition requires no notational associativity. (For example, addition has the associative property, therefore it does not have to be either left associative or right associative.) An operation that is not mathematically associative, however, must be notationally left-, right-, or non-associative. (For example, subtraction does not have the associative property, therefore it must have notational associativity)&lt;a href=&quot;https://en.wikipedia.org/wiki/Operator_associativity&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; title=&quot;read more on wikipedia&quot;&gt;[...] &lt;/a&gt;&lt;/fieldset&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;After Unary numeric promotion, if the type of each of the operands of a shift operator is not a primitive integral type, then that will cause a &lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;compile time error&lt;/b&gt;&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;fieldset&gt;
&lt;legend&gt;&lt;abbr title=&quot;For Your Information&quot;&gt;FYI&lt;/abbr&gt; : &lt;b&gt;Variables and Data types in Java&lt;/b&gt;&lt;/legend&gt;&lt;br /&gt;
&lt;b&gt;Variable&lt;/b&gt;:- is any name used to refer a memory location.&lt;br /&gt;
&lt;b&gt;Data type&lt;/b&gt;:- represents the values that can be stored in the variable.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;There are 3 types of variable in Java:&lt;/u&gt;&lt;br /&gt;
1. Local &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; ( variable declared inside the method ).&lt;br /&gt;
2. Instance &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; ( variable declared inside the class but outside the method ).&lt;br /&gt;
3. Static &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; ( Non-local variable that is declared as static ).&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;There are 2 types of compile-time data types in Java:&lt;/u&gt;&lt;br /&gt;
1. Primitive, and&lt;br /&gt;
2. Non-primitive.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://1.bp.blogspot.com/-QdkwAXYSxkE/WToH7TMbR_I/AAAAAAAAAEE/LCxP_8H-j70ooQSVOTRwZpZFAbD6bE80ACLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B07%253A58%253A35.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot; Data types in Java&quot; border=&quot;0&quot; data-original-height=&quot;394&quot; data-original-width=&quot;793&quot; height=&quot;197&quot; src=&quot;https://1.bp.blogspot.com/-QdkwAXYSxkE/WToH7TMbR_I/AAAAAAAAAEE/LCxP_8H-j70ooQSVOTRwZpZFAbD6bE80ACLcB/s400/Screenshot%2Bfrom%2B2017-06-09%2B07%253A58%253A35.png&quot; title=&quot; Data types in Java&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig.3.1&lt;/b&gt; - Data types in Java&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Therefore, by primitive integral type we mean:-&lt;br /&gt;
(a.) Integer : byte, short, int, long. And&lt;br /&gt;
(b.) Floating-point : float, double.&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
&lt;b&gt;Data Type &amp;nbsp; &amp;nbsp; Default size &amp;nbsp; &amp;nbsp;&amp;nbsp; Default value&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
boolean &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 1 bit &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; &amp;nbsp;&amp;nbsp; false&lt;br /&gt;
byte  &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 1 byte&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 0&lt;br /&gt;
char &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 2 byte&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &#39;\u0000&#39;&lt;br /&gt;
short  &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 2 byte&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;br /&gt;
int &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 4 byte&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;br /&gt;
long&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 8 byte&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0L &lt;br /&gt;
float&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 4 byte&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.0f&lt;br /&gt;
double&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp; 8 byte&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.0d&lt;br /&gt;
&lt;br /&gt;
Java follows &lt;b&gt;UNICODE system&lt;/b&gt; instead of ASCII code system. Well,&lt;br /&gt;
Lowest value: &lt;b&gt;&#39;\u0000&#39;&lt;/b&gt;&lt;br /&gt;
Highest value: &lt;b&gt;&#39;\uFFFF&#39;&lt;/b&gt;.&lt;br /&gt;
So, &lt;b&gt;char&lt;/b&gt; in Java takes &lt;b&gt;2 byte&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
UNICODE = Universal Interntionl Standard Character Encoding.&lt;br /&gt;
&lt;br /&gt;
Different country were following different code system. So, to make it common for all, UNICODE System was developed.&lt;br /&gt;
&lt;br /&gt;&lt;/fieldset&gt;
&lt;br /&gt;
&lt;fieldset&gt;
&lt;legend&gt;&lt;abbr title=&quot;For Your Information&quot;&gt;FYI&lt;/abbr&gt; : &lt;b&gt;Numeric promotions&lt;/b&gt;&lt;/legend&gt;&lt;br /&gt;
&lt;b&gt;Numeric promotions&lt;/b&gt;: - are used to convert the operands of a numeric operator to a common type so that the operation can be performed.&lt;br /&gt;
&lt;br /&gt;
There are two kinds of numeric promotion:&lt;br /&gt;
1. Unary numeric promotion. And,&lt;br /&gt;
2. Binary numeric promotion.&lt;br /&gt;
&lt;br /&gt;
1. &lt;a href=&quot;http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.6.1&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Unary numeric promotion&lt;/a&gt;: - During an operation, some operators apply unary numeric promotion on the single operand which is to produce a numeric type value.&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The operand of compile-time type &lt;i&gt;&lt;b&gt;byte&lt;/b&gt;&lt;/i&gt;, &lt;i&gt;&lt;b&gt;short&lt;/b&gt;&lt;/i&gt;, &lt;b&gt;&lt;i&gt;char&lt;/i&gt;&lt;/b&gt;, or &lt;i&gt;&lt;b&gt;int&lt;/b&gt;&lt;/i&gt; is subjected to &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; &lt;a href=&quot;http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;unboxing conversion&lt;/a&gt;, and it is then promoted to a value of type &lt;i&gt;&lt;b&gt;int&lt;/b&gt;&lt;/i&gt; by &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; a &lt;a href=&quot;http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.2&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;widening primitive conversion&lt;/a&gt; or an &lt;a href=&quot;http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.1&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;identity conversion&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Else, if the operand is of compile-time type long, float, or double, it is subjected to unboxing conversion.&lt;/li&gt;
&lt;li&gt;Else, it remains as it is, without converting. &lt;/li&gt;
&lt;/ul&gt;
Unary numeric promotion is performed on expressions in the following situations:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Each &lt;span style=&quot;background-color: yellow;&quot;&gt;dimension expression in an array creation expression&lt;/span&gt;. Ex: inte ar[] = new int[2];&lt;/li&gt;
&lt;li&gt;The index expression in an array access expression. Ex: ar[&#39;\u0001&#39;] = 1;&lt;/li&gt;
&lt;li&gt;The operand of a unary +, -, ~ complimentary, or &amp;lt;&amp;lt;, &amp;gt;&amp;gt;, &amp;gt;&amp;gt;&amp;gt; shift operators. Ex:&amp;nbsp;ar[0] = -&#39;\u0001&#39;;&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Note&lt;/b&gt;: A &lt;i&gt;&lt;b&gt;l&lt;/b&gt;&lt;b&gt;ong&lt;/b&gt;&lt;/i&gt; &lt;span style=&quot;color: orange;&quot;&gt;&lt;i&gt;shift distance&lt;/i&gt;&lt;/span&gt; (RHS operand of the &lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;SHIFT OPERATOR&lt;/span&gt;&lt;/b&gt; &amp;lt;&amp;lt;, &amp;gt;&amp;gt; or &amp;gt;&amp;gt;&amp;gt;) doesn&#39;t promote the &lt;span style=&quot;color: orange;&quot;&gt;&lt;i&gt;value being shifted&lt;/i&gt;&lt;/span&gt; ( LHS operand of the shift operator) to &lt;i&gt;&lt;b&gt;long&lt;/b&gt;&lt;/i&gt;. &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;code&gt; byte a = 17;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #6aa84f;&quot;&gt; // 1. assigning byte value&lt;/span&gt;&lt;br /&gt;
byte b = +a;&lt;span style=&quot;color: #6aa84f;&quot;&gt; // 2.&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt; Compiler error &lt;/span&gt; &lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;2nd statement&lt;/span&gt; creates &lt;span style=&quot;color: red;&quot;&gt;compile error&lt;/span&gt; because of the following rule mentioned by JLS: &quot;if the operand is of compile-time type &lt;b&gt;byte&lt;/b&gt;, &lt;b&gt;short&lt;/b&gt; or &lt;b&gt;char&lt;/b&gt; &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; unary numeric promotion promotes it to a value of type &lt;b&gt;int&lt;/b&gt; by a widening conversion.&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://3.bp.blogspot.com/--lODWnd8cIs/WTl41Ni1lGI/AAAAAAAAADk/US2UCKl-isIfdxvib05qGg2sCBNKcTneQCLcB/s1600/Screenshot%2Bfrom%2B2017-06-08%2B16%253A21%253A41.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Example to illustrate compiler error produced during unary numeric promotion&quot; border=&quot;0&quot; data-original-height=&quot;177&quot; data-original-width=&quot;692&quot; height=&quot;102&quot; src=&quot;https://3.bp.blogspot.com/--lODWnd8cIs/WTl41Ni1lGI/AAAAAAAAADk/US2UCKl-isIfdxvib05qGg2sCBNKcTneQCLcB/s400/Screenshot%2Bfrom%2B2017-06-08%2B16%253A21%253A41.png&quot; title=&quot;Example to illustrate compiler error produced during unary numeric promotion&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 3.2&lt;/b&gt; - Example to illustrate &lt;span style=&quot;color: red;&quot;&gt;compiler error&lt;/span&gt; produced during unary numeric promotion&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
To remove the error, we can explicitely caste it to byte &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt; &lt;code&gt; byte c = (byte) +a; &lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
2. &lt;a href=&quot;http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.6.2&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Binary numeric promotion&lt;/a&gt;: When an                   operator applies &lt;span class=&quot;emphasis&quot;&gt;&lt;i&gt;binary numeric promotion&lt;/i&gt;&lt;/span&gt; to a                   pair of operands, the following rules apply (in                   order):                                &lt;br /&gt;
&lt;div class=&quot;orderedlist&quot;&gt;
&lt;ol class=&quot;orderedlist&quot; type=&quot;1&quot;&gt;
&lt;li class=&quot;listitem&quot;&gt;                         &lt;div class=&quot;norm-static&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-100-A&quot;&gt;&lt;/a&gt; Reference type operand is subjected to                                  unboxing conversion.                         &lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                         &lt;div class=&quot;norm-static&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-100-B&quot;&gt;&lt;/a&gt;                                  Widening primitive conversion is                                  applied to convert either or both operands as specified by the                                  following rules:                         &lt;/div&gt;
&lt;div class=&quot;norm&quot;&gt;
&lt;ul class=&quot;norm&quot; type=&quot;disc&quot;&gt;
&lt;li class=&quot;listitem&quot;&gt;                                  &lt;div class=&quot;norm-static&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-100-B-A&quot;&gt;&lt;/a&gt;                                        If either operand is of type &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;double&lt;/code&gt;&lt;/b&gt;, the other is                                               converted to &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;double&lt;/code&gt;&lt;/b&gt;.                                  &lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                                  &lt;div class=&quot;norm-static&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-100-B-B&quot;&gt;&lt;/a&gt;                                        Otherwise, if either operand is of type &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;float&lt;/code&gt;&lt;/b&gt;, the other                                               is converted to &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;float&lt;/code&gt;&lt;/b&gt;.                                  &lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                                  &lt;div class=&quot;norm-static&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-100-B-C&quot;&gt;&lt;/a&gt;                                        Otherwise, if either operand is of type &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;long&lt;/code&gt;&lt;/b&gt;, the other is                                               converted to &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;long&lt;/code&gt;&lt;/b&gt;.                                  &lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                                  &lt;div class=&quot;norm-static&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-100-B-D&quot;&gt;&lt;/a&gt;                                        Otherwise, both operands are converted to type &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;int&lt;/code&gt;&lt;/b&gt;.                                  &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div class=&quot;norm-static&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-200&quot;&gt;&lt;/a&gt;It implies, during this operation the priority of data type in descending order is &lt;img src=&quot;https://latex.codecogs.com/gif.latex?%5Crightarrow&quot; title=&quot;\rightarrow&quot; /&gt;&amp;nbsp; &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;double, float, long, int.&lt;/code&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;&lt;/code&gt;&lt;/b&gt;After the                   conversion(s), if any, &lt;a href=&quot;http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.13&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;value set conversion&lt;/a&gt; is then applied to each operand.&lt;/div&gt;
&lt;div class=&quot;norm&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-300&quot;&gt;&lt;/a&gt;Binary numeric promotion is                   performed on the operands of certain operators:                &lt;/div&gt;
&lt;div class=&quot;norm&quot;&gt;
&lt;ul class=&quot;norm&quot; type=&quot;disc&quot;&gt;
&lt;li class=&quot;listitem&quot;&gt;                         &lt;div class=&quot;norm&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-300-A&quot;&gt;&lt;/a&gt;&lt;code class=&quot;literal&quot;&gt;*&lt;/code&gt;, &lt;code class=&quot;literal&quot;&gt;/&lt;/code&gt;, and &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;%&lt;/code&gt;&lt;/b&gt; &lt;code&gt;&lt;b&gt;(&lt;/b&gt;&lt;/code&gt;&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;multiplicative operator)&lt;/code&gt;&lt;/b&gt;&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;&lt;/code&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                         &lt;div class=&quot;norm&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-300-B&quot;&gt;&lt;/a&gt; &lt;code class=&quot;literal&quot;&gt;+&lt;/code&gt; and &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;- (additive operator)&lt;/code&gt;&lt;/b&gt;                         &lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                         &lt;div class=&quot;norm&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-300-C&quot;&gt;&lt;/a&gt; &lt;code class=&quot;literal&quot;&gt;&amp;lt;&lt;/code&gt;, &lt;code class=&quot;literal&quot;&gt;&amp;lt;=&lt;/code&gt;,                                &lt;code class=&quot;literal&quot;&gt;&amp;gt;&lt;/code&gt;, and &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;&amp;gt;= &lt;/code&gt;&lt;/b&gt;&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;(comparison operator)&lt;/code&gt;&lt;/b&gt;&lt;/code&gt; &lt;/b&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                         &lt;div class=&quot;norm&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-300-D&quot;&gt;&lt;/a&gt; &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;==&lt;/code&gt;&lt;/b&gt; and &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;!=&lt;/code&gt; &lt;/b&gt;&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;(equality operator)&lt;/code&gt;&lt;/b&gt;                         &lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                         &lt;div class=&quot;norm&quot;&gt;
&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;&amp;amp;&lt;/code&gt;&lt;/b&gt;, &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;^&lt;/code&gt;&lt;/b&gt;, and &lt;b&gt;&lt;code class=&quot;literal&quot;&gt;|&lt;/code&gt; &lt;/b&gt;&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;(integer bitwise operator)&lt;/code&gt;&lt;/b&gt;                         &lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;                         &lt;div class=&quot;norm&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-5.6.2-300-F&quot;&gt;&lt;/a&gt;&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;? :&lt;/code&gt; &lt;/b&gt;&lt;b&gt;&lt;code class=&quot;literal&quot;&gt;(conditional operator)&lt;/code&gt;&lt;/b&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://3.bp.blogspot.com/-oyBWMaK88wI/WToAQNsmJvI/AAAAAAAAAD0/RBkGUrlK_KciijA5fyx8Wmo6wjQnV7VwQCLcB/s1600/Screenshot%2Bfrom%2B2017-06-08%2B17%253A23%253A37.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;440&quot; data-original-width=&quot;679&quot; height=&quot;256&quot; src=&quot;https://3.bp.blogspot.com/-oyBWMaK88wI/WToAQNsmJvI/AAAAAAAAAD0/RBkGUrlK_KciijA5fyx8Wmo6wjQnV7VwQCLcB/s400/Screenshot%2Bfrom%2B2017-06-08%2B17%253A23%253A37.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;Fig. 3.3&lt;/b&gt; - Example to illustrate the&lt;span style=&quot;color: red;&quot;&gt; compile error&lt;/span&gt; caused on breaking the rules of binary numeric promotion&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;&lt;/fieldset&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Capsule Notes &lt;/b&gt;&lt;/div&gt;
The shift operators include left shift &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;, signed right shift &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;, and unsigned right shift &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;.&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;/ul&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The value of &lt;code&gt;n&amp;lt;&amp;lt;s&lt;/code&gt; is &lt;code&gt;n&lt;/code&gt; left-shifted &lt;code&gt;s&lt;/code&gt; bit positions with&lt;b&gt; zero-extension&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;The value of &lt;code&gt;n&amp;gt;&amp;gt;s&lt;/code&gt; is &lt;code&gt;n&lt;/code&gt; right-shifted &lt;code&gt;s&lt;/code&gt; bit positions with &lt;b&gt;sign-extension&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;The value of &lt;code&gt;n&amp;gt;&amp;gt;&amp;gt;s&lt;/code&gt; is &lt;code&gt;n&lt;/code&gt; right-shifted &lt;code&gt;s&lt;/code&gt; bit positions with &lt;b&gt;zero-extension&lt;/b&gt;.&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Negative integers are stores as it&#39;s two&#39;s complement in Java. &lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;background-color: lime;&quot;&gt;&lt;b&gt;Note&lt;/b&gt;:&lt;/span&gt; According to JSL:&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;If the                promoted type of the left-hand operand is &lt;code class=&quot;literal&quot;&gt;int&lt;/code&gt;, only the&lt;b&gt; five                lowest-order bits&lt;/b&gt; of the right-hand operand are used as the &lt;b&gt;shift                distance&lt;/b&gt; after getting subjected to a                bitwise logical AND operator with the mask                value &lt;code class=&quot;literal&quot;&gt;0x1f&lt;/code&gt; (&lt;code class=&quot;literal&quot;&gt;0b11111&lt;/code&gt;). The shift                distance actually used is therefore always in the                range &lt;code class=&quot;literal&quot;&gt;0&lt;/code&gt; to &lt;code class=&quot;literal&quot;&gt;31&lt;/code&gt;, inclusive.                          &lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;background-color: white;&quot;&gt; &lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;jls-15.19-310&quot;&gt;&lt;/a&gt;If the                promoted type of the left-hand operand is &lt;code class=&quot;literal&quot;&gt;long&lt;/code&gt;, then only the &lt;b&gt;six                lowest-order bits&lt;/b&gt; of the right-hand operand are used as the &lt;b&gt;shift                distance&lt;/b&gt;. It is as if the right-hand operand were subjected to a                bitwise logical AND operator with the mask                value &lt;code class=&quot;literal&quot;&gt;0x3f&lt;/code&gt; (&lt;code class=&quot;literal&quot;&gt;0b111111&lt;/code&gt;). The shift                distance actually used is therefore always in the                range &lt;code class=&quot;literal&quot;&gt;0&lt;/code&gt; to &lt;code class=&quot;literal&quot;&gt;63&lt;/code&gt;, inclusive.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;/span&gt;The &lt;span style=&quot;background-color: white;&quot;&gt;shift operators are left-associative.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;After Unary numeric promotion, if the type of each of the operands of a  shift operator is not a primitive integral type, then that will cause a &lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;compile time error&lt;/b&gt;&lt;/span&gt;.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;a href=&quot;https://1.bp.blogspot.com/-nLOE-T-fXwI/WTrYyob-BtI/AAAAAAAAAHc/g7ExTbeVelsWvF50SM4qODfHxZxBCn8yACLcB/s1600/Screenshot%2Bfrom%2B2017-06-09%2B22%253A48%253A10.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;504&quot; data-original-width=&quot;811&quot; height=&quot;198&quot; src=&quot;https://1.bp.blogspot.com/-nLOE-T-fXwI/WTrYyob-BtI/AAAAAAAAAHc/g7ExTbeVelsWvF50SM4qODfHxZxBCn8yACLcB/s320/Screenshot%2Bfrom%2B2017-06-09%2B22%253A48%253A10.png&quot; style=&quot;display: none;&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&amp;nbsp;            &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt; &lt;/span&gt;&lt;/span&gt; &lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;/ul&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;hr /&gt;
&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;
Interview Questions:&lt;/b&gt;&lt;/div&gt;
&lt;b&gt;Q. 1. Why is there no &amp;lt;&amp;lt;&amp;lt; operator in Java 8?&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Ans. &lt;/b&gt;To avoid redundancy. In java the sign values are stored in MSB. In left shift operation, the right side bit gets empty. So, in left shift operation there is no point in talking about signed and unsigned variable. The available left shift operator does the work.&lt;br /&gt;
&lt;br /&gt;
FYI, there is no unsigned right shift operator &quot;&amp;gt;&amp;gt;&amp;gt;&quot; in C, but it&#39;s there is Java. So, we can say that it&#39;s a java specific operator. &lt;br /&gt;
&lt;br /&gt;
p.s. this post content is concisely based on the data provided by the JLS ( Java Language Specification) on Java 8 release.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;span style=&quot;color: red;&quot;&gt;*&lt;/span&gt; ( Most of the questions have already been answered in the explanation. Yet if you find any question left unanswere. Please comment it below... )&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Bibliography:&lt;/b&gt;&lt;/div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;sup&gt;&lt;b&gt;[1]&lt;/b&gt; &lt;/sup&gt;&lt;/span&gt;This article uses material from the Wikipedia article &lt;a href=&quot;https://en.wikipedia.org/wiki/Operator_associativity&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; title=&quot;wiki article on Operator associativity&quot;&gt;Operator associativity&lt;/a&gt;, which is released under the &lt;a href=&quot;https://creativecommons.org/licenses/by-sa/3.0/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; title=&quot;Creative Commons Attribution Share Alike License 3.0&quot;&gt;Creative Commons Attribution-Share-Alike License 3.0&lt;/a&gt; (&lt;a href=&quot;https://en.wikipedia.org/w/index.php?title=Operator_associativity&amp;amp;action=history&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;view authors&lt;/a&gt;).&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://cs.techruto.com/feeds/6269422465510566640/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=5718802842428101241&amp;postID=6269422465510566640&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5718802842428101241/posts/default/6269422465510566640'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5718802842428101241/posts/default/6269422465510566640'/><link rel='alternate' type='text/html' href='https://cs.techruto.com/2017/06/java-shift-operators.html' title='Bit shift operators in Java'/><author><name>Sasuke Naruto</name><uri>http://www.blogger.com/profile/12066467749580493805</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiodJZB1LFPXE0moMcmctZiB2zeNCtF1R5W7d-frodSshVl5P5D8OpxFt6iPmy52-hl1INiTYq7zlckUiSBtk4ajUftMnUdCNNTlWF6-oZmPIjD0yJw5odCfy-012_MA/s220/pp.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://1.bp.blogspot.com/-nLOE-T-fXwI/WTrYyob-BtI/AAAAAAAAAHc/g7ExTbeVelsWvF50SM4qODfHxZxBCn8yACLcB/s72-c/Screenshot%2Bfrom%2B2017-06-09%2B22%253A48%253A10.png" height="72" width="72"/><thr:total>1</thr:total></entry></feed>