Binary is 0s and 1s

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 Memory

Inside the computer, there are only on/off currents (0s and 1s), but we want to represent numbers and symbols
 
A bit is a single 0/1
 
A byte is 8 bits, e.g.,  01010101

Representing Integers

729 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 Strings

Every symbol on the keyboard-- letters, digits, etc.-- are represented internally in a computer's memory with a number. We call these characters.
 
The mappings between numbers and symbols are defined in the ASCII table: http://www.lookuptables.com/.
 
The mapping table was known as the ASCII table, or ASCII code. With ASCII, each symbol was represented with a number between 0 and 255 (question: how many bits are necessary to store such a number?)

So as not to be English-centric, modern computing systems now use Unicode, which can represent many different languages and requires additional bits to represent each symbol. But for English symbols, the ASCII table can still be used to lookup the value of each symbol.

A string is a sequence of characters, e.g., 'dog'. Internally, we store the unicode number for each character and a special end-of-string character. So the word 'dog' is really the unicode number for 'd', the unicode number for 'o' and the unicode character for 'g', and finally the end-of-string character which is '\0'. 'd' is 100 decimal in the ASCII table, so "dog" is represented with the following decimal numbers (stored in 16 bits each): 

    100     111     103     0


Numbers are generally stored as integers, as explained above. But when we get input from the user or display values, we must handle numbers represented as a sequence of digits. Just like with letters, digits are characters. The digit '0' is the unicode number 48 and '9' is the unicode number 57. The string '724' is stored internally as:          55 50 52 0

Worksheet

1. Consider the following sixteen bits:
 
0000 0000 0100 1101
 
a. What is the value of the number if it represents a positive integer (whole number)?
 
b. If the string of bits represents a symbol, which symbol is it? You can use the ascii lookup table http://www.lookuptables.com/
 
2. Computers used to have 64K of memory. What is K? Is it a round number? Why don't computer scientists work with nice round numbers like 1000?

3. What is a megabyte? A gigabyte? What is a thousand gigabytes? A million gigabytes?

4. Show how the following three base-10 numbers would be represented in binary.   

24    1025  43 

5. Show how 'cat' is represented in bits?

6. How many possible characters can be represented with unicode (16 bits)?

7. What's the largest integer that can be represented in 4 bytes, if we only consider positive numbers?
 
8. What if we also consider negative numbers? How do you think these are represented?


9. Write a python function that takes a string of digits as a parameter and returns an integer which is the base-10 value of those digits, e.g., if the parameter is "723" the function returns the integer 723 (Yes, I know, Python has an int function-- you are writing that here).

10. Write a python function that takes a string of base-10 digits as a parameter and returns a string of base-2 digits so that the base-2 digits are the converted value of the original base-10 digits. For example, if "11" were sent in, the function would return "1011".

Work the above questions out with pencil and paper. When you finish, you can check your binary conversion answers here: http://mistupid.com/computers/binaryconv.htm

Binary conversion help:

Binary Numbers from CSUNPLUGGED


Binary Numbers in 60 Seconds

Recent site activity