title: How are ints in c stored in memory?
body: <p>I thought that ints in C were stored with the most significant bit first, for example, the number <code>5</code> would be <code>0...0101</code>. I thought I could manipulate specific bits by coercing C to let me pretend a specific memory address was an <code>int</code> and adding to the bits there like they were an <code>int</code>.</p>
  
  <p>I tried to set up 0 bits of 0s in memory, then tried adding <code>255</code> to different memory addresses and it seems to work as though the least significant digit is stored in memory before the most significant digit because when I added 1 to my memory address and changed the bits there I got a larger number instead of a smaller one. If the most significant bit was stored earlier in memory, adding <code>255</code> to the memory address 1 byte higher over shouldn't affect the number at the original address at all because the last 8 bits are the beginning of the next <code>int</code>. I was wondering if I was interpreting this correctly, and that ints were stored with the least significant bit first.</p>
  
  <pre><code>#include "stdio.h"
  #include "string.h"
  #include "stdlib.h"
  
  int main() {
      int *x = malloc(8); //getting 4 memory addresses
      int *y = malloc(8);
      int *z = malloc(8);
      int *a = malloc(8);
  
      x[0] = 0; //setting 64 bits past memory addresses to 0s
      x[1] = 0;
      y[0] = 0;
      y[1] = 0;
      z[0] = 0;
      z[1] = 0;
      a[0] = 0;
      a[1] = 0;
  
      *((int*)((int)x)) = 255; //adding to x's memory address
      *((int*)((int)y + 1)) = 255; //adding 1 byte over from y
      *((int*)((int)z + 2)) = 255; //adding 2 bytes over from z
      *((int*)((int)a + 3)) = 255; //adding 3 bytes over from a
  
      printf("%d\n", sizeof(int));
      printf("%d,%d\n", x[0], x[1]);
      printf("%d,%d\n", y[0], y[1]);
      printf("%d,%d\n", z[0], z[1]);
      printf("%d,%d\n", a[0], a[1]);
  
      printf("%d\n", x);
      printf("%d\n", &amp;x[1]);
      return 0;
  }
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>4
  255,0
  0,-16777216
  0,16711680
  0,65280
  12784560
  12784564
  </code></pre>
  
  <p>Actual Output:</p>
  
  <pre><code>4
  255,0
  65280,0
  16711680,0
  -16777216,0
  12784560
  12784564
  </code></pre>