How are bits stored in memory? (In chunks? Can there be bits of multiple sizes stored toghether?)
You'll be better off experimenting in C and/or assembly, rather than Java. Those languages are lower-level and expose the address space directly.
I used to think that each memory location contains 8, 16, 32 or 64 bits. So 0101 would be stored in an 8 bit machine as 00000101 (sign extended if it was negative). This was all fine and dandy until I wrote a program in java out of curiosity to find out some more inner workings of this system.
All memory locations in x86 systems contain 8 bits (1 byte). If a value contains more data than can fit into a single byte, it is stored using multiple bytes. For example, in C, the "float" type is stored using 4 bytes (32 bits).
All of it looks fine except for the space. It has 6 bits instead of 8. I'm now wondering how all of that information is stored in memory. If all of it was stored in 8 bit chunks, like
The space is also stored in a single byte. Your print code is forgetting to pad out to 8 spaces. 100000 == 00100000 == 0x20.
The space has 8 bits too. It's just that Integer.toBinaryString doesn't print leading 0
bits the way you used it.
With all the leading 0
bits, it actually looks like this in memory:
H : 01001000
e : 01100101
l : 01101100
l : 01101100
o : 01101111
: 00100000
W : 01010111
o : 01101111
r : 01110010
l : 01101100
d : 01100100
Your original intuition was (mostly) correct: all memory locations consist of the same number of bits. On all modern machines, there are eight bits in a "byte", where a byte is the smallest chunk of memory that the machine can access individually.
Look closely at your output. You have seven digits in all of them except the space. The space just happens to begin with two zeroes in its binary representation, while the other letters begin with one.