From 4d10e6c5a5384fb0a9c99d9775982990a2d58ffa Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Tue, 15 Sep 2015 15:58:07 +0200
Subject: [PATCH] Binary literal exercise

---
 Doc/Sd1/languageFundamentals.xml | 100 ++++++++++++++++++++++++++-----
 1 file changed, 84 insertions(+), 16 deletions(-)

diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml
index c582ca9a5..dcabf9c37 100644
--- a/Doc/Sd1/languageFundamentals.xml
+++ b/Doc/Sd1/languageFundamentals.xml
@@ -301,24 +301,91 @@
   <section xml:id="sw1IntegerLiterals">
     <title>Integer value literals</title>
 
-    <section xml:id="sd1BinaryLiteral">
+    <section xml:id="sd1BinaryIntLiteral">
       <title>Binary literals</title>
 
-      <para>TODO:</para>
+      <qandaset defaultlabel="qanda" xml:id="sd1QandaBinaryIntLiteral">
+        <qandadiv>
+          <qandaentry>
+            <question>
+              <orderedlist>
+                <listitem>
+                  <para>Using the decimal system to represent integer values
+                  we tend to ignore other important numbering systems. Write
+                  an application which prints the decimal value of
+                  <inlineequation>
+                      <m:math display="inline">
+                        <m:msub>
+                          <m:mi>1110100100</m:mi>
+
+                          <m:mi>2</m:mi>
+                        </m:msub>
+                      </m:math>
+                    </inlineequation> by writing the former as a binary int
+                  literal. Verify the printed value by an independent
+                  calculation.</para>
+                </listitem>
+
+                <listitem>
+                  <para>Then construct a second binary literal representing a
+                  negative value of your choice.</para>
+                </listitem>
+              </orderedlist>
+            </question>
+
+            <answer>
+              <orderedlist>
+                <listitem>
+                  <programlisting language="java">   public static void main(String[] args) {
+      
+      System.out.println(0B1110100100);
+      System.out.println(  512 +
+                            256 +
+                             128 +
+                               32 +
+                                  4);
+   }</programlisting>
+
+                  <para>This yields:</para>
+
+                  <programlisting language="none">932
+932</programlisting>
+                </listitem>
+
+                <listitem>
+                  <para>A negative value in <link
+                  xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's
+                  complement</link> representation starts with a
+                  <quote>1</quote> at its highest bit. Binary literals in
+                  <xref linkend="glo_Java"/> represent <code>int</code>
+                  values. An <code>int</code> in <xref linkend="glo_Java"/>
+                  uses 4 bytes and thus occupies 4 x 8=32 bits. Therefore
+                  choosing a negative value is a simple task: Start
+                  with<quote>1</quote>at the leading bit and let it follow by
+                  32 - 1 = 31 random bit values:</para>
+
+                  <programlisting language="java">   public static void main(String[] args) {
+      System.out.println(0B10000000_00111001_01101001_01110100);
+   }</programlisting>
+                </listitem>
+              </orderedlist>
+            </answer>
+          </qandaentry>
+        </qandadiv>
+      </qandaset>
     </section>
 
     <section xml:id="sw1SectHexadecimalLiterals">
       <title>Hexadecimal literals</title>
 
-      <para>Using the decimal system to represent integer values we tend to
-      ignore other important numbering systems. As you may know the <xref
-      linkend="glo_RGB"/> color model uses triplets of numbers to define color
-      value components representing intensities of three base colors
-      <foreignphrase><emphasis role="bold">R</emphasis>ed</foreignphrase>,
-      <foreignphrase><emphasis role="bold">G</emphasis>reen</foreignphrase>
-      and <foreignphrase><emphasis
-      role="bold">B</emphasis>lue</foreignphrase>. The component values range
-      from 0 to 255, the latter defining maximum intensity.</para>
+      <para>As you may know the <xref linkend="glo_RGB"/> color model uses
+      triplets of numbers to define color value components representing
+      intensities of three base colors <foreignphrase><emphasis
+      role="bold">R</emphasis>ed</foreignphrase>, <foreignphrase><emphasis
+      role="bold">G</emphasis>reen</foreignphrase> and
+      <foreignphrase><emphasis role="bold">B</emphasis>lue</foreignphrase>.
+      The component values range from 0 to 255, the latter defining maximum
+      intensity.</para>
 
       <para>The color <quote>red</quote> for example is being represented by
       (255, 0, 0). So the red component has maximum intensity, blue and green
@@ -345,11 +412,11 @@
         <qandadiv>
           <qandaentry>
             <question>
-              <para>Calculate the decimal representation of the hexadecimal
-              value <inlineequation>
+              <para>Write a program printing the decimal value of the
+              hexadecimal representation <inlineequation>
                   <m:math display="inline">
                     <m:msub>
-                      <m:mi>CO</m:mi>
+                      <m:mi>C0</m:mi>
 
                       <m:mi>16</m:mi>
                     </m:msub>
@@ -360,7 +427,7 @@
               value <inlineequation>
                   <m:math display="inline">
                     <m:msub>
-                      <m:mi>CO</m:mi>
+                      <m:mi>C0</m:mi>
 
                       <m:mi>16</m:mi>
                     </m:msub>
@@ -408,7 +475,8 @@
               is <emphasis role="bold">always</emphasis> large enough to hold
               color intensity values ranging from 0 to 255. Give an answer
               being based on the <link
-              xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">specification</link>.</para>
+              xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">specification</link>
+              and not just by try-and-error.</para>
 
               <para>The programmer tries to adopt the <link
               linkend="sw1QandaMaxMinInt">related int example</link>
-- 
GitLab