Remember, Bitcoins exist only as records of transactions; there are no physical or digital Bitcoins that you can view or count. This means that you can end up with many different transactions tied to a specific Bitcoin address.
It’s not uncommon, then, for your Bitcoin wallet to contain multiple addresses, each of which represents one or more transactions. For our example, assume that you have received 3 Bitcoins from transaction A, 4 Bitcoins from transaction B, and 2 Bitcoins from transaction C. These add up to 9 BTC, but they’re really just three separate transactions stored in three separate addresses in your Bitcoin wallet.
The transactions and associated Bitcoins are not automatically combined in your wallet to make a file that contains those 9 Bitcoins. Your wallet simply contains records of each of the transactions.
And here’s another peculiarity of Bitcoin accounting. Because everything is based on transactions rather than individual Bitcoin, your Bitcoin client can only send amounts equal to the number of Bitcoin involved in a given transaction; it can’t split the transaction value. So if a given transaction was for 4 BTC, you don’t really have four individual Bitcoins that you can spend one at a time. You have a transaction worth 4 BTC that has to be handled all as a whole.
What happens, then, when you want to spend some of the Bitcoins stored in your wallet? The Bitcoin client tries to be smart about all outgoing transactions, by using transactions records with the right amounts to add up to the exact number of Bitcoins you want to spend.
Let’s say you have those 9 Bitcoins in your wallet, from three separate transactions, and you want to buy something worth 5 BTC. Your Bitcoin client could try to spend the Bitcoins in the order in which they were acquired; that would mean taking the 3 BTC acquired from the first transaction and 2 of the 4 BTC from the second transaction—effectively splitting that second transaction. Instead, the Bitcoin client will take the 3 BTC from the first transaction and the 2 BTC from the third transaction, and will leave all the Bitcoin from the second transaction intact. That makes for simpler record keeping.
Calculating a 5 BTC transaction.
The challenge comes when you want to spend Bitcoin that don’t add up to the right number available from individual transactions. For example, maybe you want to buy something that costs 8 BTC. You don’t have an 8 BTC transaction in your wallet, and there’s no way any combination of the transactions can add up to 8 BTC. How is this handled?
So your Bitcoin client has to send the amount equal to a given input transaction, and the recipient client will then send the amount overpaid back to you as a new transaction, effectively making the change. It’s kind of like paying for a $3 burger with a $5 bill and getting $2 back.
In our example, your Bitcoin client will send the 3 BTC from the first transaction, the 4 BTC from the second transaction, and the 2 BTC from the third transaction, for a total of 9 BTC. Because you’re buying something worth 8 BTC, the store you’re buying from will send you 1 BTC in a new transaction, to be recorded at a new address in your Bitcoin wallet.
Calculating an 8 BTC transaction.
The same holds true when you purchase something that costs less than a single Bitcoin. If you pay for a 0.8 BTC item with 1 BTC, you’ll get 0.2 BTC back in change.
By the way, all Bitcoin transactions are irreversible. If you make a mistake, you can’t undo it. You can, however, ask the other party to issue a refund, in the form of a new Bitcoin transaction. But you can’t get your original Bitcoins back.