# How a Computer Represents a Number Electronically

I understand how computers represent decimal numbers using binary. I have yet to absorb this video on how computers add numbers, which looks helpful.

Before understanding that though, I would like to understand at a deep level how a computer stores and represents a number (8 bit, 16 bit, 32, 64, 128, 256, not sure if they can store above that). There are multiple aspects of this:

• How it is stored on disk.
• How it is stored in memory.
• How it is stored in the CPU / ALU / etc. to actually use to manipulate / perform calculations.

I don't need to know all the details of it, just something to point me in the right direction / an overview. I understand a little about flip flops, which store 1 bit in main memory. I am only interested in here in how numbers are stored in memory and in the CPU / ALU, not the disk. But if the disk is simple enough would be nice to know about that.

In terms of main memory, maybe there is somehow 8/16/32/etc. connections of flip-flops, or perhaps no, main-memory is just bits and you point to locations in these bits. I am thinking in terms of the x86 mov instruction, e.g. mov edx, [ebp + 8], which might move bits between memory or between memory and registers.

In terms of the CPU / ALU, it seems to just boil down to registers, so the numbers would be in the registers. But here they do make the distinction between 8/16/32/etc. bit integers, so maybe there is some electronics structure that implements this.

So this question is about knowing how the following works, at a high level / overview:

• How integers are stored / represented in main memory electronically.
• How integers are stored / represented in the CPU / ALU electronically.

An 8-bit integer is just stored in 8 flip flops. A 16-bit one in 16 flip flops, and so on. Most modern computers are 32 or 64 bit, so use banks of 32 or 64 flip flops for each register.

For speed, these are wired to other parts of the processor via parallel data buses. So entire "words" of memory can be transferred in one go.

There's no problem storing fewer bits. If you run a 16-bit add on a 32-bit computer, it will just use 16 of the 32 bits it has in each register, and ignore the rest. In a sense, it's the machine code instructions that define the meaning of the data. The only difference between an 8-bit integer and an ASCII character is the way in which you choose to interpret the 8 bits.

Most computers use DRAM for their main memory. That works in a different way, as a flip flip requires too many transistors. A DRAM cell is essentially a tiny capacitor connected to a MOSFET. The capacitor stores one bit (charged of discharged) and the transistor is used to read or write it. Over time, the charge on the capacitor leaks away, so the memory has to be "refreshed" periodically, by reading each cell, then writing it back again to recharge the capacitor.

Hard disks store data in magnetic domains on the disk. Magnetise a domain one way and it's a 0; magnetise it the other and it's a 1. The data bits will be stored sequentially around the disk in tracks. They are read or written as the disk spins round under the read/write head.

All bits are stored in memory independent from each other. They may be grouped into words of various size but these have no fixed physical representation, they may even be jumbled across various chips, e.g. on a RAM module. Nor they have any meaning or data type given to them. They are just billions of individual bits. The only reason the computer does not lose track which bit is which is because the word layout and addressing mechanism is the same for writing and reading.

The only place where there are made connections between the individual bits is the ALU. In there, the small amount of individual storage cells (flip flops or capacitors) is connected with their neighbors through combinatorial logic which is controlled by the operation the ALU should do at that moment. And that is controlled by the current operation code previously read from the program memory.

• "connected with their neighbors through combinatorial logic" wondering what this looks like at the electronics level, I am already familiar with most of what you have described, wondering if you could go into more depth into the electronics. Thank you. – Lance Pollard Jul 4 '18 at 22:24
• Take e.g. a number of such ALU flip-flops each storing a bit. That was called accumulator in older CPU designs. Now the combinatorial ALU logic is such the output of accumulator flipflop 0 not only can be passed to data bus line 0, but it can also be passed into the input of flipflop 1 and so on. So depending on the instruction, the contents of the accumulator is either passed to the data bus or shifted left (a multiplication by 2). And similar for right shift and adding. With these three operations, all other math operations can be done. Both integer and floating point. – Janka Jul 4 '18 at 22:34