text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
>> I'm going to go ahead and do this only finitely many times.
3,845.31
2.458
Let's use a for loop, which I alluded to earlier.
3,847.768
2.279
Let's do this.
3,850.047
0.583
Give me another variable int i gets 0.
3,850.63
2.8
i is less than, let's say, 64 i++.
3,853.43
4
And now let me go ahead and print out n is percent i, comma n.
3,857.43
6.58
And then n-- this is still going to take forever.
3,864.01
3.537
Let's do this.
3,867.547
0.583
>> n gets n times 2.
3,868.13
2.49
Or we could be fancy and do times equals 2.
3,870.62
3.52
But let's just say n equals itself, times 2.
3,874.14
2.98
In other words, in this new version of the program,
3,877.12
2.201
I don't want to wait forever from like 800,000 to 4 billion.
3,879.321
2.499
Let's just get this over with.
3,881.82
1.25
>> Let's actually double n each time.
3,883.07
1.85
Which, recall, doubling is the opposite of having, of course.
3,884.92
2.74
And whereas last week we have something again, and again,
3,887.66
2.375
and again, super fast, doubling will surely
3,890.035
2.165
get us from 1 to the biggest possible value that we can count to with an int.
3,892.2
5.88
>> So let's do exactly this.
3,898.08
1.67
And we'll come back to this before long.
3,899.75
1.97
But this, again, is just like the repeat block in Scratch.
3,901.72
2.46
And you'll use this before long.
3,904.18
1.42
>> This just means count from zero up to, but not equal, to 64.
3,905.6
4.57
And on each iteration of this loop, just keep incrementing i.
3,910.17
4.115
So i++-- and this general construct on line 7 is just a super common way
3,914.285
4.705
of repeating some lines of code, some number of times.
3,918.99
3.3
Which lines of code?
3,922.29
1.072
These curly braces, as you may have gleaned from now,
3,923.362
2.208
means, do the following.
3,925.57
1.21
>> It's in like Scratch, when it has the yellow blocks
3,926.78
2.73
and other colors that kind of embrace or hug other blocks.
3,929.51
3.17
That's what those curly braces are doing here.
3,932.68
2.07
So if I got my syntax right-- you can see the carrot symbol in C means
3,934.75
5.45
that's how many times I was trying to solve this problem.
3,940.2
2.506
So let's get rid of that one altogether, and close that window.
3,942.706
2.624
And we'll use the new one.
3,945.33
1.19
Make overflow, dot slash overflow, Enter, all right,
3,946.52
5.46
it looks bad at first.
3,951.98
1.11
But let's scroll back in time, because I did this 64 times.
3,953.09
3.11
>> And notice the first time, n is 1.
3,956.2
2.5
Second time, n is 2, then 4, then 8, then 16.
3,958.7
4.41
And it seems that as soon as I get to roughly 1 billion,
3,963.11
6.34
if I double it again, that should give me 2 billion.
3,969.45
3.35
But it turns out, it's right on the cusp.
3,972.8
2.18
>> And so it actually overflows an int from 1 billion
3,974.98
3.95
to roughly negative 2 billion, because an integer,
3,978.93
4.584
unlike the numbers we were assuming last week,
3,983.514
1.916
can be both positive and negative in reality and in a computer.
3,985.43
2.967
And so at least one of those bits is effectively stolen.
3,988.397
2.333
So we really only have 31 bits, or 2 billion possible values.
3,990.73
3.46
>> But for now, the takeaway is quite simply, whatever these numbers are
3,994.19
4.03
and whatever the math is, something bad happens eventually,
3,998.22
4.06
because eventually you are trying to permute the bits one too many times.
4,002.28
4.7
And you effectively go from all 1's to maybe all 0's, or maybe
4,006.98
4.08
just some other pattern that it clearly, depending on context,
4,011.06
3.2
can be interpreted as a negative number.
4,014.26
2.082
And so it would seem the highest I can count in this particular program
4,016.342
2.958
is only roughly 1 billion.
4,019.3
1.91
But there's a partial solution here.
4,021.21
1.55
You know what?
4,022.76
0.72
>> Let me change from an int to a long long.
4,023.48
4.12
And let me go ahead here and say-- I'm going to have
4,027.6
3.033
to change this to an unsigned long.
4,030.633
1.657
Or, let's see, I never remember myself.
4,032.29
4.57
>> Let's go ahead and make overflow.
4,036.86
3.06
No, that's not it, LLD, thank you.
4,039.92
1.94
So sometimes Clang can be helpful.
4,041.86
1.57
I did not remember what the format specifier was for a long long.
4,043.43
4.12
>> But, indeed, Clang told me.
4,047.55
1.4
Green is some kind of good, still means you made a mistake.
4,048.95
2.62
It's guessing that I meant LLD.
4,051.57
1.62
>> So let me take it's advice, a long long decimal number, save that.
4,053.19
5.56
And let me rerun it, dot slash overflow, Enter.
4,058.75
4.44
And now what's cool is this.
4,063.19
1.83
>> If I scroll back in time, we still start counting at the same place-- 1, 2, 4,
4,065.02
4.12
8, 16.
4,069.14
1.08
Notice, we get all the way up to 1 billion.
4,070.22
4.64
But then we safely get to 2 billion.
4,074.86
2.21
>> Then we get to 4 billion, then 8 billion, 17 billion.
4,077.07
4.23
And we go higher, and higher, and higher.
4,081.3
2.04
Eventually, this, too, breaks.
4,083.34
2.4
>> Eventually, with a long long, which is the 64-bit value, not
4,085.74
3.61
a 32-bit value, if you count too high, you wrap around 0.
4,089.35
4.31
And in this case, we happen to end up with a negative number.
4,093.66
2.75
>> So this is a problem.
4,096.41
1.14
And it turns out that this problem is not all that arcane.
4,097.55
2.889
Even though I've deliberately induced it with these mistakes,
4,100.439
2.621
it turns out we see it kind of all around us, or at least some of us do.
4,103.06
3.089
>> So in Lego Star Wars, if you've ever played the game,
4,106.149
2.79
it turns out you can go around breaking things up in LEGO world,
4,108.939
4.891
and collecting coins, essentially.
4,113.83
2.81
And if you've ever played this game way too much time,
4,116.64
2.56
as this unnamed individual here did, the total number
4,119.2
3.43
of coins that you can collect is, it would seem, 4 billion.
4,122.63
4.07
>> Now, with it's actually rounded.
4,126.7
1.54
So LEGO was trying to keep things user friendly.
4,128.24
1.999
They didn't do it exactly 2 to the 32 power, per last week.
4,130.239
3.54
But 4 billion is a reason.
4,133.779
1.531
It seems, based on this information, that LEGO, and the company that
4,135.31
3.669