title: Decimal Division by left shift
body: <p>I have been given a question to convert base from 10 to 2 <strong>without using division(/) and modulus(%)</strong>, so I came up with solution of using bitwise AND(&amp;) and right shift(&gt;&gt;) operators.</p>
  <p>So I start to learn what these two operators exactly do but still for some questions I could not find answer or understand the logic behind.</p>
  <p>If I understand correctly division works according the digits place value, in decimal and binary both. When we divide the number by 10 or 2 ,we shift the place value by one place to right in both, which this will result in division by 10 in decimal and by two in binary.</p>
  <blockquote>
  <p>X=120   (in base of ten)  if  X&gt;&gt;1  we will have X=12   (division by 10)</p>
  </blockquote>
  <blockquote>
  <p>Y=1000  (in base of two)  if  Y&gt;&gt;1  we will have X=100  (division by 2)</p>
  </blockquote>
  <p>but when I use this piece of code:</p>
  <pre><code>#include&lt;iostream&gt;
  using namespace std ;
  
  int main()
  {
      int a,b=1;
      cout &lt;&lt;&quot;enter an integer&quot;&lt;&lt;endl;
      cin&gt;&gt; a;
      cout&lt;&lt;(a &amp; b)&lt;&lt;endl;
      a=a&gt;&gt;1;
      cout&lt;&lt;a;
      cout&lt;&lt;endl;
      system(&quot;pause&quot;);
      return 0 ;
  }
  </code></pre>
  <p>I get comfused cause in my mind it was like this</p>
  <blockquote>
  <p>a=120   (in base of ten)  if  X&gt;&gt;1  we will have X=12   (division by
  10)</p>
  </blockquote>
  <p>but the result was this</p>
  <blockquote>
  <p>a=120   (in base of ten)  if  X&gt;&gt;1  we have X=60        (division by 2!!)</p>
  </blockquote>
  <p>I do not understand two main points about the result:</p>
  <p>First, if this operator(&gt;&gt;) just shift the place value of the digits in code and don't change the base of the number(10) it should produce another result(12) than we can see in result of code (which it is 60). Why we can see this result(60) but not 12?</p>
  <p>Second, if it does binary left shift (which it seems like this for me), does it change the decimal to binary at first by IDE or not?</p>
  <p>And about the bitwise AND if it is logical gate(which it seems it is) :</p>
  <p>1.How can we put other values except 0 and 1 and still have an answer?</p>
  <p>2.According Bitwise AND rules</p>
  <blockquote>
  <p>Y&amp;1=Y</p>
  </blockquote>
  <p>Then it should be 120 but the result of code is 1. What is explanation for this?</p>
  <p>3.How it can generate the remainder (according which mathematical operations and logic)?</p>