You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ For this lesson you'll need to have _Python 2.7_ installed. If you're on Mac or
10
10
11
11
Let's jump right into some code. Create a new Python file called `binary_example1.py`. Type this into it:
12
12
13
-
```
13
+
```py
14
14
num =1
15
15
print num
16
16
@@ -21,7 +21,7 @@ for i in range(11):
21
21
22
22
Run it, and you'll see that this is what you get:
23
23
24
-
```
24
+
```sh
25
25
$ python binary_example1.py
26
26
1
27
27
2
@@ -80,7 +80,7 @@ So that is the basics of how transistors came to be used in computer hardware. H
80
80
81
81
Let's look at `binary_example1.py` again.
82
82
83
-
```
83
+
```py
84
84
num =1
85
85
print num
86
86
@@ -99,7 +99,7 @@ The **left shift operator** (`<<`) takes the binary version of the number to the
99
99
100
100
So throughout our program, the variable `num` is backed by these values:
101
101
102
-
```
102
+
```py
103
103
00000001# num = 1
104
104
00000010# num = num << 1
105
105
00000100# num = num << 1
@@ -112,7 +112,7 @@ So throughout our program, the variable `num` is backed by these values:
112
112
113
113
See how the `1` bit shifts to the left with every cycle of the `for` loop? We can map these **octets** to our program output to see how they match up.
114
114
115
-
```
115
+
```py
116
116
00000001# 1
117
117
00000010# 2
118
118
00000100# 4
@@ -126,7 +126,7 @@ See how the `1` bit shifts to the left with every cycle of the `for` loop? We ca
126
126
127
127
What about `256`, `512`, `1024` and `2048`? Python knows that it needs another **byte** to store those larger numbers, so we get another **octet** to left shift into.
128
128
129
-
```
129
+
```py
130
130
0000000000000001# 1
131
131
0000000000000010# 2
132
132
0000000000000100# 4
@@ -147,7 +147,7 @@ Now we need to understand why the position of the `1` bit determines which numbe
147
147
148
148
Let's make a new Python file and call it `binary_example2.py`. It's going to be a lot like our first example, but we are going to replace the **left shift** with another expression.
149
149
150
-
```
150
+
```py
151
151
num =1
152
152
print num
153
153
@@ -158,7 +158,7 @@ for i in range(11):
158
158
159
159
**What do you think this program outputs?** Here's a hint: remember how after we played the _2048_ game I said to think about the numbers as `2 * 2`, `2 * 4`, `2 * 8`, and so on? Lo and behold!
160
160
161
-
```
161
+
```sh
162
162
$ python binary_example2.py
163
163
1
164
164
2
@@ -233,7 +233,7 @@ So letters on a computer are just an illusion! At least an illusion as far as th
233
233
Here's our last example, `binary_example3.py`. I am using the `ord()` and `chr()` functions to find the number value for a character, messing with that
0 commit comments