title: Reading and changing bits in C uint32_t
body: <p>I am using this code</p>
  
  <pre><code>int get_bit(int n, int bitnr) {
  int mask = 1 &lt;&lt; bitnr;
  int masked_n = n &amp; mask;
  int thebit = masked_n &gt;&gt; bitnr;
  return thebit;
  }
  
  void printbits(uint32_t bits) {
      int i;
      for (i = 0; i &lt; 32; i++)
          printf("%d", get_bit(bits, i));
      printf("\n");
  }
  </code></pre>
  
  <p>to get and print the bits of a uint32_t,
  and in another function this code</p>
  
  <pre><code>uint32_t bits= 0;
  bits|= 1&lt;&lt; 0;
  </code></pre>
  
  <p>to change the most significant bit (left-most) from 0 to 1.</p>
  
  <p>the problem is when printing bits using the printbits function, it prints them right, but when using <code>printf("%#x", bits);</code> I'm getting the hex value of the bits as if they are read from right to left!</p>
  
  <p>so the printbits gives me '10000000000000000000000000000000'
  but the hex value printed is the value of '00000000000000000000000000000001'.</p>
  
  <p>Help appreciated</p>