I have always wondered what happens "under the hood" when a machine performs integer/float arithmetic. In context of Java/C++, I'm wondering how to do integer/float addition, subtraction, multiplication, division, modulus, and maybe exponentiation and stuff using only the binary operators like &, ^, |, and the shift operators <<, >>, and >>>. I get that this may be a little unnecessary since we already have built-in operators like +, -, *, /, and %, but I would like to know deeper into how it works "under the hood".
I'm already familiar with the structure of Java's integer (byte to long) and float (float and double) types, but unsure how to operate on them.
Louis Denning wrote:I have always wondered what happens...
Sounds to me like two questions there:
1. How does binary (specifically, in the case of Java, 2's complement) arithmetic work.
2. How do I work with different integer/float types.
For the first, I suspect the link I provided might help; it should be pointed out that Java does NOT have a "rotate" operator, which is also used in some arithmetic functions (as I recall; it's been 30 years since I worried about that stuff ). All the wrapper classes have rotate() methods though.
For the second: I think you'll need to be more explicit. Specifically, what is it you don't understand: conversion? (widening or narrowing).
How much under the hood do you want to go? Turtles go all the way down to the quantum level. In a nut shell, this is what happens at various layers when you do something like 1+ 2
1) JVM reads the opcode and issues a command to the OS
2) OS stores the operands in a memory location, and issues an ADD command to the CPU giving the memory location of the operands, and the memory location where the result should be
3) CPU sends a command to the RAM to fetch the first operand and stores it in a register. Similarly fetches the next operand and stores it in another register. Activates the correct internal circuits that performs the add operation that puts the result in a third register. Issue another PUT command that puts the contencts of third register into memor. I can go more into how the FETCH and PUT commands work but I won;t because they are not interesting
4) The way those internal circuits work is every arthimetic operation can be converted into a boolean formula. Circuit designers put together logic gates that perform the operation
5) The way logic gates are costructed by putting transistors together in certain configuration that perform the AND, OR and NOR function
6) The way transistors work is that they are semiconductors whose conductiveness can be controlled electrically
7) The way semiconductors work is that they are made up of silicon that has an atomic weight of 14. SInce SI has 14 electrons, which means it has 4 valence elctrons, that make it suitable to be able to control it's conductiveness by applying an external field
8) The reason SI has 14 valence electrons is because quantum theory!!! You lost me there actually.. That's the extent of my knowledge
So, yeah, each of these are semester long courses in a Computer Engineering course. Learning everything takes you 4 years. Forgeting everything takes you 4 beers. You want to learn it on your won, you will have to find the right books.