title: How to print bits in c
body: <p>I'm writing a function to print bits in c, I'm only allowed to use <code>write</code> function. my function doesn't work for other numbers. </p>
  
  <pre><code>void    print_bits(unsigned char octet)
  {
      int oct;
      int div;
  
      div = 128;
      oct = octet;
      while (!(div &lt;= 1))
      {
          if (div &lt;= oct)
          {
              write(1, "1", 1);
              oct = oct % div;
          }
          else
          {
              write(1, "0", 1);
              div = div / 2;
          }
      }
  }
  </code></pre>
  
  <p><a href="http://code.geeksforgeeks.org/pWp5UP" rel="nofollow">live code</a></p>