Cs50X Introduction To Computer Science: 1. Week 0 1.1 Binary
Cs50X Introduction To Computer Science: 1. Week 0 1.1 Binary
1. Week 0
1.1 Binary
Back when you learned how to read and write numbers, you learned about
the digits 0-9. To make numbers larger than 9, you learned all you needed
to do was to use some combination of these digits, for example 52. This is
decimal notation.
Although decimal, which has 10 digits 0-9, is more familiar to humans,
computers only work with binary, which has two digits, 0 and 1.
With just two digits, we can represent all the same information that we can
with ten.
In decimal, 123 is one hundred and twenty-three, with the rightmost digit
representing the ones column, next digit from the right representing the
tens column, the next the hundreds column and so on
Binary uses columns in the same way, but rather than each column
representing a power of 10, it instead represents a power of 2.
Thus to get the number 111 in binary,
Once we have used up all the places, we need more bits (binary digits),
each of which stores a 0 or 1 (just as we would need additional decimal
digits to go from representing 999 to 1,000).
Each 0 or 1 is stored in the state of a transistor in a computer which we
can also represent by a light bulb or a switch.
We can abstract on top of numbers to represent many other kinds of
information.
o We can represent letters using ASCII, a scheme that maps letters to
numbers (e.g. 65 maps to A, 66 to B, and so on). We can use this to
spell out messages like the below:
Numbers can also be used to represent audio, video, images, etc the
mapping from numbers to other information is determined by the kind of
file (.e.g., a file ending in .jpg will be interpreted as an image).
One mapping from numbers to images is known as RGB, because it
represents each pixel by the intensity of red, green, and blue in that pixel.
These values are combined to produce a colour that the pixel is displayed.
1.2 Algorithms
For example, how might we find someone like Mike Smith in the phone
book?
o Look up each page in order sequentially until you reach Mike Smith.
This is correct but very slow.
o Flip two pages at a time? This runs the risk of overshooting the page
with the name Mike Smith on it.
o Usually when were looking for someone in the phone book, we
open to the middle, taking advantage of the fact the phone book is
sorted alphabetically. The middle is the M section, so we can
literally tear the problem in half by ripping the phone book in half
and keeping only the half we think Mike Smith is in. If we keep
repeating this, keeping only half of the book each time, eventually
were left with a single page containing Mike Smiths phone number.
For a 1,000-pagep hone book, this only takes about ten steps!
axis represents the size of the problem (in this case, the number of pages
in the phone book), whilst the
o
o
o
The red line is the first algorithm, where the time to find Mike Smith is
linearly proportional to the number of pages in the phone book.
The yellow line is the second algorithm, which though twice as fast, still
increases linearly with the size of the problem.
The green line, however is not linear, but is instead logarithmic, and
increases much more slowly than either of the linear algorithms. With this
strategy, even if the size of the phone book doubled, it would only take
one more step to solve the problem and if the phone book contained four
billion pages, wed still only need 32 steps. (Assuming the phone book is
alphabetised).
Lines 1, 2, 3,
5, 7, and 10 are
simple declarative statements, or functions, that describe actions.
Lines 4, 6, 9, and 12 are conditional statements, or branches; note that
line 5 is indented to show that we only call Mike if that branch is
followed.
With lines 8 and 11, go to line 3, a loop is formed to reuse some of the
earlier steps.
If we reach line 12, then Mike Smith isnt in the phone book, we should
give up.
o
o
outputs