WEBVTT 1 00:00:01.570 --> 00:00:04.490 So if you've been following through the previous video, 2 00:00:04.490 --> 00:00:06.650 we talked about the binary number system. 3 00:00:06.650 --> 00:00:09.540 You may actually have made the following two observations. 4 00:00:09.540 --> 00:00:15.040 Firstly, converting from decimal To binary tedious once you get up above four bits or 5 00:00:15.040 --> 00:00:16.370 eight numbers in the range 0 through 15. 6 00:00:16.370 --> 00:00:19.790 And binary numbers have a lot of numbers have a lot of digits and writing and 7 00:00:19.790 --> 00:00:24.150 speaking them is both boring and, more importantly, error prone. 8 00:00:24.150 --> 00:00:27.560 So as a result, programmers very often use hexadecimal instead. 9 00:00:27.560 --> 00:00:32.970 Hexidecimal, or as it's usually called X is basic 16 and because 16 is 10 00:00:32.970 --> 00:00:38.410 an exact power of two, converting between binary and hex is actually quick and easy. 11 00:00:38.410 --> 00:00:42.480 Now once again, I'm gonna point out you don't need to understand this section 12 00:00:42.480 --> 00:00:44.700 in order to complete the rest of the course. 13 00:00:44.700 --> 00:00:48.690 So don't feel that if you haven't got a good understanding of this section that 14 00:00:48.690 --> 00:00:51.800 The next sections are going to get worse because that's not the case, but 15 00:00:51.800 --> 00:00:55.290 the thing here is that our discussion of binary wouldn't be complete 16 00:00:55.290 --> 00:00:57.210 without covering hexadecimal. 17 00:00:57.210 --> 00:00:58.750 So if you want to skip the rest of this video, 18 00:00:58.750 --> 00:01:00.500 and move on to the next section then please do. 19 00:01:00.500 --> 00:01:03.690 And we'll get back to regular Python programming. 20 00:01:03.690 --> 00:01:04.230 Look. You can 21 00:01:04.230 --> 00:01:07.070 also come back to this section once you've mastered Python, and 22 00:01:07.070 --> 00:01:09.280 you no longer having to remember loads of new things. 23 00:01:09.280 --> 00:01:10.230 So continuing on. 24 00:01:10.230 --> 00:01:13.650 Hex has got a number of advantages over binary. 25 00:01:13.650 --> 00:01:19.110 Firstly hex numbers are much shorter and that makes writing them a lot easier. 26 00:01:19.110 --> 00:01:23.430 For the number 0 through 255 can be written in just two hex digits and 27 00:01:23.430 --> 00:01:27.840 the number 0 to 65,535 With just four hex digits. 28 00:01:27.840 --> 00:01:31.870 The other thing is that working in hex provides a convenient way to remain close 29 00:01:31.870 --> 00:01:33.110 to the underlying binary. 30 00:01:33.110 --> 00:01:36.450 And this can be very useful when you're actually dealing with memory addresses, or 31 00:01:36.450 --> 00:01:38.870 operations that work on individual bits. 32 00:01:38.870 --> 00:01:42.630 And that's like using and or an xor, which we saw in the previous video. 33 00:01:44.110 --> 00:01:45.160 So moving on to the next slide. 34 00:01:45.160 --> 00:01:50.450 Working in base 16 means that we actually need a way to represent the 16 digits 35 00:01:50.450 --> 00:01:55.400 from 0 to 15 in the same way that, in base 10, we had the digits from 0 to 9. 36 00:01:55.400 --> 00:01:59.130 So rather than create 6 more digits to represent 10, 11, 12, 13, 14, and 15, 37 00:01:59.130 --> 00:02:03.720 th symbols A to F are actually used instead. 38 00:02:03.720 --> 00:02:07.900 Now each hex digit represents four binary digits called a nibble. 39 00:02:07.900 --> 00:02:10.690 And seriously, I know it's a weird name but that's what they're called. 40 00:02:10.690 --> 00:02:15.680 So to represent an 8 bit-byte, we actually use two hex digits. 41 00:02:15.680 --> 00:02:18.740 So let's go back now and change the program that we wrote 42 00:02:18.740 --> 00:02:21.590 in the previous video to print out the numbers in hex. 43 00:02:21.590 --> 00:02:24.160 So I'm gonna shoot back over to the Intelligo, so 44 00:02:24.160 --> 00:02:26.080 a simple change of we'll just briefly type it in again. 45 00:02:26.080 --> 00:02:31.621 I've got a new for, so for i in range 17, and we'll 46 00:02:31.621 --> 00:02:38.734 do our print(" 47 00:02:38.734 --> 00:02:48.033 in hex is ".format) 48 00:02:49.630 --> 00:02:52.890 Note that I use x here to represent hex and hot h. 49 00:02:52.890 --> 00:02:53.970 So, I've actually run this. 50 00:02:55.850 --> 00:02:59.120 You can see the numbers and the cross pointing value in hex. 51 00:02:59.120 --> 00:03:01.410 So, 0 through nine is equivalent to decimal. 52 00:03:01.410 --> 00:03:06.108 But once we get to 10, notice how instead of 10 we went to oi, 11 is ob. 53 00:03:06.108 --> 00:03:09.060 12 is [INAUDIBLE] and so on, right through to 16, and 54 00:03:09.060 --> 00:03:11.560 16 effectively becomes 10, 1, and 0. 55 00:03:11.560 --> 00:03:16.380 So [INAUDIBLE] partially uses the letter B in the format stream to represent binary. 56 00:03:16.380 --> 00:03:20.270 We saw that in the previous video when I used X as I pointed out on the screen, 57 00:03:20.270 --> 00:03:23.630 not H to represent X If you want to extend the range to 256, 58 00:03:23.630 --> 00:03:27.340 you can actually see how counting in hex works. 59 00:03:27.340 --> 00:03:30.370 So adding one then carries over to the next column as a source. 60 00:03:30.370 --> 00:03:35.160 So for example, 1F is 116 plus 15 units, which is 31. 61 00:03:35.160 --> 00:03:39.570 20 and hex is two 16s, and no units, which is 32. 62 00:03:39.570 --> 00:03:43.100 Now, if a hexadecimal number contains one of the letters a to f, 63 00:03:43.100 --> 00:03:46.030 it's actually obviously easy to see that it's a hex number. 64 00:03:46.030 --> 00:03:49.080 But however, the number 20 is not obviously hexadecimal. 65 00:03:49.080 --> 00:03:53.530 So if we're dealing with hex numbers, we need some way to identify them as such. 66 00:03:53.530 --> 00:03:58.420 Python along with many other languages allows or uses the prefix 0x to actually 67 00:03:58.420 --> 00:04:02.830 identify a hex number as the following simple program we're about to demonstrate. 68 00:04:02.830 --> 00:04:07.530 So I'm gonna type in that so obviously we can type x=20. 69 00:04:07.530 --> 00:04:09.760 Exit 20, but based on that, 70 00:04:09.760 --> 00:04:14.530 you wouldn't know whether you meant 20 decimal or 20 hex. 71 00:04:14.530 --> 00:04:17.290 But if I type in 0X 20, that actually 72 00:04:17.290 --> 00:04:21.600 tells Python that we want to actually type in the hex number for 20. 73 00:04:21.600 --> 00:04:29.110 So y equals 0X0A, and then we could do a print X, print Y. 74 00:04:29.110 --> 00:04:30.130 Let's do a modification. 75 00:04:30.130 --> 00:04:33.690 Print X times Y So let's run that. 76 00:04:33.690 --> 00:04:36.440 And you can see the results there. 77 00:04:36.440 --> 00:04:38.820 First one, 20, which is actually 32. 78 00:04:38.820 --> 00:04:41.800 So we're asking it to print out the decimal equivalent. 79 00:04:41.800 --> 00:04:44.700 And y, which was 0x0a, the equivalent is 10. 80 00:04:44.700 --> 00:04:49.230 And the last one, multiplying them together So it's basically 32 by 10 81 00:04:49.230 --> 00:04:53.910 effectively, or 20x multiplied by [INAUDIBLE] X, and 82 00:04:53.910 --> 00:04:58.080 that's 320 as a decimal number, is the actual total in decimals. 83 00:04:58.080 --> 00:04:59.880 And I didn't point it out in the previous video, but 84 00:04:59.880 --> 00:05:04.730 you can actually specify binary literals using the prefix ob. 85 00:05:05.880 --> 00:05:11.480 So we could do something like 0b00101010 To actually print out a value in binary. 86 00:05:11.480 --> 00:05:15.160 So if we run that, that would be the binary equivalent, and or in decimal, 87 00:05:15.160 --> 00:05:18.210 42 is equivalent of that binary number there. 88 00:05:18.210 --> 00:05:19.260 Now the other thing to point out, 89 00:05:19.260 --> 00:05:21.650 is that leading zeros are not actually necessary either. 90 00:05:21.650 --> 00:05:23.710 So in other words, we could have done the same thing here, 91 00:05:23.710 --> 00:05:24.970 we could have just removed those two. 92 00:05:26.470 --> 00:05:27.800 Run it. 93 00:05:27.800 --> 00:05:29.380 We've got exactly the same value. 94 00:05:29.380 --> 00:05:30.640 Hexadecimal addition and 95 00:05:30.640 --> 00:05:34.910 subtraction can actually be performed just like decimal addition and subtraction. 96 00:05:34.910 --> 00:05:38.940 But with that said there's very few people these days who actually think in hex. 97 00:05:38.940 --> 00:05:42.620 Now it use when we didn't have modern programming languages and compilers 98 00:05:42.620 --> 00:05:46.470 that we have these days programmers often work with machine code or assembler And 99 00:05:46.470 --> 00:05:50.170 you can actually find programs he thought encountered effectively in hex, but 100 00:05:50.170 --> 00:05:51.590 this is becoming rarer. 101 00:05:51.590 --> 00:05:55.070 I actually, myself, developed games back in the day and 102 00:05:55.070 --> 00:06:00.440 I didn't actually have an [INAUDIBLE] so I actually had to deal with hex and 103 00:06:00.440 --> 00:06:04.972 I had to understand how to actually calculate numbers in hex but 104 00:06:04.972 --> 00:06:06.780 also [INAUDIBLE] what they actually meant in their values. 105 00:06:06.780 --> 00:06:08.710 So, It's something that you can certainly learn, but 106 00:06:08.710 --> 00:06:10.800 these days it's not something you really need to. 107 00:06:10.800 --> 00:06:12.580 So let's swing back now on to the next slide. 108 00:06:14.550 --> 00:06:18.620 So in this calculation I'm gonna show you 8A take 4E. 109 00:06:18.620 --> 00:06:20.550 So 8E requires us borrowing from the next column. 110 00:06:20.550 --> 00:06:21.790 So we actually borrow 16. 111 00:06:21.790 --> 00:06:25.740 Now, at this point, most people would probably add 16 to 10. 112 00:06:25.740 --> 00:06:27.640 Or A and Hex to give 26. 113 00:06:27.640 --> 00:06:33.300 So, taking away E which is 14 leaves 12 and so we have c in the first column. 114 00:06:33.300 --> 00:06:35.820 Seven take four leaves three in the second column. 115 00:06:35.820 --> 00:06:39.190 So, the answer is three c in hex or 60 in decimal. 116 00:06:39.190 --> 00:06:42.010 So if you had to perform addition and subtraction in Hex 117 00:06:42.010 --> 00:06:45.650 without using a calculator, it's natural to convert the digits to decimal. 118 00:06:45.650 --> 00:06:48.750 Now the standard calculators that come with Windows, Linux and 119 00:06:48.750 --> 00:06:52.970 the Mac all have an advanced mode that can perform Hex arithmetic, and 120 00:06:52.970 --> 00:06:57.270 you see how easy it is to do it in Python, so Now frankly there's no real reason for 121 00:06:57.270 --> 00:07:00.380 risking errors by performing these calculations yourself. 122 00:07:00.380 --> 00:07:01.940 So in Windows there's a couple of options. 123 00:07:01.940 --> 00:07:04.480 There's either a scientific option under the view menu or 124 00:07:04.480 --> 00:07:07.540 in Windows 10 it's got a programmer mode in the settings menu. 125 00:07:07.540 --> 00:07:10.690 On the Mac, there's a programmer mode under the view menu. 126 00:07:10.690 --> 00:07:15.080 And under Ubuntu Linux, there's also a programmer mode under the mode menu. 127 00:07:15.080 --> 00:07:17.810 So there's a few options if you do actually wanna go in and 128 00:07:17.810 --> 00:07:19.270 check out the calculator and try it out. 129 00:07:19.270 --> 00:07:23.360 It's actually quite useful to do that just to sort of see how it all works, 130 00:07:23.360 --> 00:07:26.710 but again it's not something you need to understand fully yourself. 131 00:07:26.710 --> 00:07:28.320 Okay, so moving onto the next slide. 132 00:07:30.080 --> 00:07:32.020 Gonna talk now about Octal. 133 00:07:32.020 --> 00:07:38.250 So Octal have like hexadecimal, Octal is base 8 and uses the digits 037. 134 00:07:38.250 --> 00:07:42.470 As you've probably expected I'd soon assume you now understand binary decimal. 135 00:07:42.470 --> 00:07:47.060 And so representing 255 an octal, we've got three times 64 which is 192, 136 00:07:47.060 --> 00:07:52.470 plus seven times eight which is 56 and that gives us 248. 137 00:07:52.470 --> 00:07:54.810 Then adding seven to give 255. 138 00:07:54.810 --> 00:07:58.400 So adding one to that we carry one from the first to second column 139 00:07:58.400 --> 00:08:01.890 Then carry one again from the second to third column, giving you 400 octal, 140 00:08:01.890 --> 00:08:08.580 which is written as 0 in the lower case o 400, the little o in other words. 141 00:08:08.580 --> 00:08:12.520 So it's usual to use lower case so it's not confused with the digit 0. 142 00:08:12.520 --> 00:08:16.240 So Python will allow an upper case letter O, though But 143 00:08:16.240 --> 00:08:18.040 it's not advisable to actually do that. 144 00:08:18.040 --> 00:08:20.810 so octal is really rarely used these days. 145 00:08:20.810 --> 00:08:25.120 One of the few instances that JP could come up with was Linux file permissions, 146 00:08:25.120 --> 00:08:27.028 which are three groups of three bits each. 147 00:08:27.028 --> 00:08:31.460 So it's represents the read, write, and execute permission. 148 00:08:31.460 --> 00:08:34.730 And the first group being the owner's permission, the next group of three bits, 149 00:08:34.730 --> 00:08:36.170 the group's permission, and 150 00:08:36.170 --> 00:08:39.600 finally The last three bits was the permission for all users. 151 00:08:39.600 --> 00:08:44.920 I'm just showing you this on a slide. 152 00:08:44.920 --> 00:08:50.310 So each octal digit corresponds to a three bit binary number, as shown there. 153 00:08:50.310 --> 00:08:52.310 So all seven combinations have been shown, 154 00:08:52.310 --> 00:08:56.680 although not all of them would be used in practice when specifying file permissions. 155 00:08:56.680 --> 00:08:59.630 So the most common permission is to allow read and write access to the owner 156 00:08:59.630 --> 00:09:04.160 of the file And read only to everyone else this will be written as 157 00:09:04.160 --> 00:09:09.010 what's octal number 644 and that will be written as rw-r--r--. 158 00:09:09.010 --> 00:09:11.900 Again, if that didn't make a lot of sense to you, that's okay. 159 00:09:11.900 --> 00:09:15.080 You don't really need to know this, it's more something you can come back to later. 160 00:09:15.080 --> 00:09:17.540 If you're feeling this is a bit overwhelming, don't give up, 161 00:09:17.540 --> 00:09:20.320 move on to the next section if you're finding this a little bit difficult. 162 00:09:20.320 --> 00:09:23.620 Okay, so let's go back to IntelliJ So it's actually time for a challenge. 163 00:09:23.620 --> 00:09:28.560 So we're gonna come up with a challenge to help you understand binary hex and octals. 164 00:09:28.560 --> 00:09:32.180 I'm gonna close down the run window and I'm gonna 165 00:09:32.180 --> 00:09:35.610 comment out this code to to say you've got it there later if you wanna use it. 166 00:09:35.610 --> 00:09:37.810 And I'm gonna paste in what the challenge is. 167 00:09:37.810 --> 00:09:39.190 Gonna paste in there now. 168 00:09:39.190 --> 00:09:40.310 So here is the challenge. 169 00:09:40.310 --> 00:09:44.060 So when converting a decimal number to binary, you look for 170 00:09:44.060 --> 00:09:48.580 the highest power of two, smaller than the number, and you put a one in that column. 171 00:09:48.580 --> 00:09:53.120 You then take the remainder and you repeat the process with the highest power. 172 00:09:53.120 --> 00:09:54.260 Putting a one in it. 173 00:09:54.260 --> 00:09:55.600 Into the remainder. 174 00:09:55.600 --> 00:09:57.010 And is zero otherwise. 175 00:09:57.010 --> 00:10:00.270 And you keep repeating until you've dealt with all the powers down to two. 176 00:10:00.270 --> 00:10:01.710 That is one in other words. 177 00:10:01.710 --> 00:10:04.590 So, what we're going to be wanting to do is writing a program that requests 178 00:10:04.590 --> 00:10:08.330 a number from the keyboard and then prints out its binary representation. 179 00:10:08.330 --> 00:10:10.230 Now, obviously you could use a format string. 180 00:10:10.230 --> 00:10:14.470 But, that's not actually allowed for this challenge because it's too easy. 181 00:10:14.470 --> 00:10:18.140 So, the program should actually cater for numbers up to 65,535. 182 00:10:18.140 --> 00:10:19.640 Other words 2 to the power of 16 take 1. 183 00:10:19.640 --> 00:10:23.070 And that's a hint here, you need integer division. 184 00:10:23.070 --> 00:10:27.490 That's the two slashes for integer division as we've talked about previously 185 00:10:27.490 --> 00:10:30.040 and the modular the percent to get the remainder. 186 00:10:30.040 --> 00:10:35.484 You also need to use the two stars to raise one number to the power of another. 187 00:10:35.484 --> 00:10:38.160 For example 2 * * 8 raises 2 to the power of 8. 188 00:10:38.160 --> 00:10:43.080 And that's an optional extra try avoiding printing leading zeros. 189 00:10:43.080 --> 00:10:47.980 Then once the program is working, modify it to print Octal rather than binary. 190 00:10:47.980 --> 00:10:50.210 So that's is the challenge, see how well you go with it. 191 00:10:50.210 --> 00:10:53.200 Go away and try your best with it and when you're ready to come back and 192 00:10:53.200 --> 00:10:56.540 see what we've come up with, come back and we'll get started on the code. 193 00:10:56.540 --> 00:10:58.139 Pause the video now and I'll see you when you get back. 194 00:11:04.140 --> 00:11:05.130 Okay, so how did you get on? 195 00:11:05.130 --> 00:11:08.550 Hopefully you figured it out, or you managed to get it working, and 196 00:11:08.550 --> 00:11:09.850 let's talk about the solution. 197 00:11:09.850 --> 00:11:13.350 So the way we come up with it was because the highest number we've got to 198 00:11:13.350 --> 00:11:16.000 deal with is one less than two to the power of sixteen, 199 00:11:16.000 --> 00:11:19.520 The first number we need to divide by is 2 to the power of 15. 200 00:11:19.520 --> 00:11:21.040 That is, 32,768. 201 00:11:21.040 --> 00:11:27.670 So we then just keep dividing and printing a zero or a one as appropriate 202 00:11:27.670 --> 00:11:31.550 before repeating with a remainder and the next lowest powers of two. 203 00:11:31.550 --> 00:11:35.190 So we therefore need to loop from 15 down to zero to get the powers of two. 204 00:11:35.190 --> 00:11:39.620 So for our solution, what we're about to do is boot up a list of the powers And 205 00:11:39.620 --> 00:11:42.010 we iterate through the list to print the digits. 206 00:11:42.010 --> 00:11:45.070 So this could all be done in a single loop if you wanted to, then. 207 00:11:45.070 --> 00:11:46.730 So let's actually make a start. 208 00:11:46.730 --> 00:11:47.690 We'll make a bit of space here. 209 00:11:49.830 --> 00:11:54.540 Okay, so we're gonna start by typing powers, create a list. 210 00:11:54.540 --> 00:11:55.380 Powers in empty. 211 00:11:56.790 --> 00:12:00.099 And put for power in range. 212 00:12:03.503 --> 00:12:08.960 15, which is saying this is using the for 213 00:12:08.960 --> 00:12:11.170 loops and stepping and so forth in previous videos. 214 00:12:11.170 --> 00:12:14.286 I'm gonna do powers.append. 215 00:12:14.286 --> 00:12:17.040 [SOUND] To, power. 216 00:12:17.040 --> 00:12:22.330 [INAUDIBLE] So that's actually gonna build our lists. 217 00:12:22.330 --> 00:12:25.621 So in other words, what it should be doing is actually giving us 32,768, 218 00:12:25.621 --> 00:12:27.900 16,384 and right on down to three to one. 219 00:12:27.900 --> 00:12:32.020 I'm gonna actually try that to see that it's working, 220 00:12:32.020 --> 00:12:33.292 by doing a print [INAUDIBLE]. 221 00:12:33.292 --> 00:12:35.430 So let's do that first to make sure it's working. 222 00:12:35.430 --> 00:12:40.790 And you can see that's working so 223 00:12:40.790 --> 00:12:43.640 that's our list which is working containing our powers. 224 00:12:43.640 --> 00:12:44.590 So that's good so far. 225 00:12:45.800 --> 00:12:47.000 So let's continue on. 226 00:12:47.000 --> 00:12:49.630 Now what we want to do is allow the user to talk a number. 227 00:12:49.630 --> 00:12:55.402 So X equal int(input) Please enter a number. 228 00:12:55.402 --> 00:13:00.990 [SOUND] And 229 00:13:00.990 --> 00:13:03.980 what we need to do is actually circle through each of our powers. 230 00:13:03.980 --> 00:13:06.200 So basically eat right through the list now to print the digits. 231 00:13:06.200 --> 00:13:11.700 To do that you can type for power in powers. 232 00:13:11.700 --> 00:13:12.610 We're going through the list. 233 00:13:15.030 --> 00:13:20.290 Print x, x being the number that has been entered by the user, integer division for 234 00:13:20.290 --> 00:13:27.370 power and end equals like so cuz you want it all on the one 235 00:13:27.370 --> 00:13:31.420 line we don't wanna move to the next line and we put x to find the remainder. 236 00:13:33.340 --> 00:13:33.840 Power. 237 00:13:35.200 --> 00:13:38.840 So if we actually run this now, and let's just try to the value ten. 238 00:13:40.130 --> 00:13:44.080 We can see we've got the binary equivalent of one, zero, one, zero binary. 239 00:13:44.080 --> 00:13:49.220 And of course, working that out, we've got a zero in the first digit 240 00:13:49.220 --> 00:13:52.160 The right most bit if you will of the binary number. 241 00:13:52.160 --> 00:13:54.660 We've got a one and that's the value of two there. 242 00:13:54.660 --> 00:13:59.590 Then moving over we've got over here the other one and that's two times two, 243 00:13:59.590 --> 00:14:01.310 which is four times two is eight. 244 00:14:01.310 --> 00:14:03.110 So eight plus two is ten. 245 00:14:03.110 --> 00:14:06.840 So you can see our binary converters actually working just fine. 246 00:14:06.840 --> 00:14:08.610 We mentioned an optional challenge. 247 00:14:08.610 --> 00:14:13.210 So we consider flag once when we hit a non zero value to print. 248 00:14:13.210 --> 00:14:16.220 And we don't actually want to print anything until that flag is set. 249 00:14:16.220 --> 00:14:19.590 The way we can do that is we can modify that here, so power in powers, and 250 00:14:19.590 --> 00:14:25.190 up here we're going to put bit equals x, integer division divided by power. 251 00:14:25.190 --> 00:14:30.350 And then we'll put if bit is not equal to zero 252 00:14:32.280 --> 00:14:35.700 printing is equal to true. 253 00:14:37.480 --> 00:14:41.590 Then here, what we'll do is we'll put if printing [INAUDIBLE] 254 00:14:41.590 --> 00:14:42.150 print, but 255 00:14:42.150 --> 00:14:45.250 instead of doing the division again we'll just use bit at this point 256 00:14:45.250 --> 00:14:48.849 because we've already done the integer division on line 43. 257 00:14:49.865 --> 00:14:54.225 Like so and that should now remove the leading zeros because we're actually 258 00:14:54.225 --> 00:14:57.885 not printing anything until we get a non zero value the first time. 259 00:14:57.885 --> 00:14:59.645 We're setting printing to true. 260 00:14:59.645 --> 00:15:01.725 Of course it can be good practice printing 261 00:15:03.605 --> 00:15:06.335 equals false before we start the loop as well. 262 00:15:06.335 --> 00:15:09.275 We run that and we get to the number ten again. 263 00:15:11.230 --> 00:15:14.660 We're now left with one zero one zero which is again is the bond 264 00:15:14.660 --> 00:15:18.350 unit of decimal ten minus the zeros, the leading zeros. 265 00:15:18.350 --> 00:15:22.690 And you look at that and you think that works, and it mostly does the example I 266 00:15:23.800 --> 00:15:28.070 typed in it does but you should always test what are called boundary conditions. 267 00:15:28.070 --> 00:15:31.380 Now, these are boundaries that are at the extreme of allowing importance. 268 00:15:31.380 --> 00:15:33.260 So testing with the highest number allowed. 269 00:15:33.260 --> 00:15:35.640 65535, so we run it. 270 00:15:35.640 --> 00:15:36.240 65535. 271 00:15:36.240 --> 00:15:43.800 That works fine with all 1's remembering that that's the maximum number There. 272 00:15:43.800 --> 00:15:46.050 That we're allowed in this conversion. 273 00:15:46.050 --> 00:15:50.490 But, if we run it again and it's a zero, we get nothing. 274 00:15:50.490 --> 00:15:53.010 And of course zero is a valid binary number. 275 00:15:53.010 --> 00:15:53.980 So, how do we fix that? 276 00:15:53.980 --> 00:15:56.120 Well, the cure is to alter our tests so 277 00:15:56.120 --> 00:15:59.230 that we're actually setting printing to true as we can see on line 47. 278 00:15:59.230 --> 00:16:02.420 But we want to actually test that. 279 00:16:02.420 --> 00:16:03.970 Bit is not equal to zero. 280 00:16:03.970 --> 00:16:06.460 Or that we're processing the last power. 281 00:16:06.460 --> 00:16:10.780 In other words, processing the very last digit if you will on r in binary. 282 00:16:10.780 --> 00:16:14.860 So you can put all power is equal to 1. 283 00:16:14.860 --> 00:16:16.580 First you run that. 284 00:16:16.580 --> 00:16:21.590 Then we type 0, which you get a 0 chart which is correct cause the last power. 285 00:16:21.590 --> 00:16:26.110 Was calculated correctly and our condition was actually 286 00:16:26.110 --> 00:16:29.890 executed because the power was one and we hadn't had a zero up until then. 287 00:16:29.890 --> 00:16:33.020 The 65535 should still work as you can see on the screen there. 288 00:16:33.020 --> 00:16:36.710 The other you can do if you wanted to is you can play around with this 289 00:16:36.710 --> 00:16:40.640 looking at line The line where we use 15. 290 00:16:40.640 --> 00:16:44.270 So what we could do here is we could actually convert this to octal, 291 00:16:44.270 --> 00:16:44.770 our result. 292 00:16:44.770 --> 00:16:48.980 We could change the two here to an eight because two was for binary. 293 00:16:48.980 --> 00:16:52.880 We could actually change that to an eight to work in octal if we wanted to. 294 00:16:52.880 --> 00:16:54.720 And for efficiency, you should also reduce the range. 295 00:16:54.720 --> 00:16:58.690 Because the highest power of 8 we're now interested in is basically 262,144, 296 00:16:58.690 --> 00:17:01.460 which is greater than the highest number we're allowing for binary. 297 00:17:01.460 --> 00:17:04.710 So play around with that, as well. 298 00:17:04.710 --> 00:17:05.295 And you can do. 299 00:17:05.295 --> 00:17:07.795 Do something similar by converting it to octal. 300 00:17:07.795 --> 00:17:11.315 I'm gonna finish this video now but leave you with something to think about. 301 00:17:11.315 --> 00:17:13.415 So we shouldn't have actually written any code for this challenge. 302 00:17:13.415 --> 00:17:18.617 The specifications stated that the program should be coded for numbers up to 65,5- 303 00:17:18.617 --> 00:17:23.077 35, but with that said [INAUDIBLE] which would happen if a larger number is entered 304 00:17:23.077 --> 00:17:26.017 and there's also an assumption that the numbers would be positive. 305 00:17:26.017 --> 00:17:28.807 The problem here was the specification was inadequate. 306 00:17:28.807 --> 00:17:31.767 So, as programmers, we've got a duty of care to the people we're programming for 307 00:17:31.767 --> 00:17:35.437 and if we give it a specification that leaves such questions unanswered 308 00:17:35.437 --> 00:17:38.077 then we really need to go back to whoever produced the spec and 309 00:17:38.077 --> 00:17:41.250 seek clarification before producing any code. 310 00:17:41.250 --> 00:17:42.790 So that's it I am going to end the video there. 311 00:17:42.790 --> 00:17:44.110 I hope that's been helpful to you. 312 00:17:44.110 --> 00:17:46.150 We will see you now in the next section.