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
 
4. Assembly Language -- some command and variable names but still processing memory cells and accessing addresses/registers and other hardware components directly.

    example:         Mov 7,3456

5. High-Level Language (Java, C++, Python)
 
Symbolic and logical, with few if any direct references to addresses, registers, or other hardware components
 
6. Natural Language, e.g., English


Converting Binary to Decimal and ASCII

Here's a couple of video clips explaining the process:

Binary Numbers in 60 Seconds

Binary Numbers from CSUNPLUGGED



Bits and Bytes

We have only 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

Base-10 is your friend

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, 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.

Characters and Strings

We say that 8 bits is a byte. 
 
In the past, letters, digits and other characters were represented with one byte (8 bits), with each symbol being mapped to a number between 0-255. 
 
The ASCII table provides the mapping: http://www.lookuptables.com/
 
So as not to be English-centric, modern computing systems now use Unicode, which can represent many different languages and requires 2 bytes (16 bits) to represent each symbol. For English symbols, the ASCII table can still be used to lookup the value of each symbol.

A string is a sequence of characters ending with a 0. So the word 'dog' is really the ascii character for 'd', the ascii character for 'o' and the ascii character for 'g' and a 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


Note that digits are characters. So '0' is the ASCII symbol 48 (decimal) and '9' is the ASCII symbol 57. The string '724' is:          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. Encode the following three decimal numbers in binary:    

24    1025  43 

4. What is 'cat' in bits?

5. How many possible characters can be represented with unicode (16 bits)?
 
6. What about negative numbers? How do you think these are represented?



[talk about kilo, mega, giga, ...]

You can check your binary conversion answers here: http://mistupid.com/computers/binaryconv.htm

Recent site activity