Computers are not magic. The software you use-- voice recognition, cell phones, social networks-- is the result of years of development and layers and layers technology. In this overview of how computers work, we focus on binary arithmetic. Software is build on levels of abstraction, each one further from the actual hardware... 1. Electric currents-- these are what really exist inside the computer. 2. 0s and 1s- ON current is 1 and an OFF current is 0 3. Machine Lanaguage -- Numbers for commands and data, e.g., 23 7 3456 The 23 might be a command such as Mov (move a number into an address), the 7 might be the number, the 3456 the address. 4. Assembly Language -- some command and variable names but still processing memory cells and accessing addresses/registers and other hardware components directly. example: Mov 3456 addr Mov 7,addr 5. High-Level Language (Java, C++, Python) Symbolic and logical, with few if any direct references to addresses, registers, or other hardware components Interpreter-- converts high-level code into assembly/machine 6. Natural Language, e.g., English Representing Data in a Computer's MemoryInside the computer, there are only on/off currents (0s and 1s), but we want to represent numbers and symbolsA bit is a single 0/1 A byte is 8 bits, e.g., 01010101 Representing Integers729 is of course seven hundred and twenty nine. Our brain knows that instantly. But what is really happening?The right-most digit is multiplied by 1, the second-right-most digit is multiplied by 10, and the third right-most digit is multiplied by 100. So we get: 9*1 +2*10+7*100. In general, with base-10, we multiply the ith digit (starting from the right) by 10i, e.g., 9*100+2*101+7*102.Base-2, Binary, works exactly the same, only our brains are not used to it. To represent a positive integer, we must convert binary into decimal. We do this by multiplying the ith bit by 2i. So the right-most bit is multiplied by 20, or 1, the second-right-most by 21 or 2, and the third right-most bit by 22 or 4, and so on. So 1010 binary is: 1*23+ 0*22+1*21+0*20 = 8+0+2+0 = 10 decimal Another way to put it: starting from the right most bit and going left, we assign 1,2,4,8,16,32, etc. to each bit that is 1. We can also take a binary number and convert it to binary. One way to do this is to write out the base-10 values of each bit until you find one that is larger than the number you are trying to convert. So for The base-10 number 155, we'd write out the values corresponding to the : value: 256 128 64 32 16 8 4 2 1 To get 155, we can't have the 256 bit set, but we can have a 128, so we put a '1' there. value: 256 128 64 32 16 8 4 2 1 0 1 We still need 155-128=27. 64 and 32 are too big, so we place 0s there and put a 1 on 16: value: 256 128 64 32 16 8 4 2 1 0 1 0 0 1 We still need 155-128-16=11. So we give a '1' to the 8-value digit. We still need 3 more, so we put a 0 in the 4-value, and 1s in the 2-value and 1-value. This gives us the binary number: value: 256 128 64 32 16 8 4 2 1 0 1 0 0 1 1 0 1 1 So 155 decimal is equal to 010011011 binary. Integers are generally allocated 4 bytes (32 bits) or on 64-bit computers, often 8 bytes. Representing Floats Floating point numbers are represented similarly to integers, but some bits are reserved for the fractional part of the number. For this reason, floats are generally allocated more bits (e.g., 64 instead of 32). Example: Let's say we were going to represent a float with 6 bits, the 2 right-most ones being the fractional part. Then what would be the value of: 101010? Characters and StringsEvery symbol on the keyboard-- letters, digits, etc.-- are represented internally in a computer's memory with a number. We call these characters. |