1
00:00:05,730 --> 00:00:12,100
Hey everybody what's going on this is Caleb with slopes dotcom and this is part two of our binary conversion

2
00:00:12,100 --> 00:00:12,810
app.

3
00:00:12,910 --> 00:00:19,120
In this video we're going to create a class called binary decimal and it's basically going to allow

4
00:00:19,120 --> 00:00:28,060
us to just create a custom type that can contain the bytes that we need to make one bit of binary data

5
00:00:28,670 --> 00:00:32,260
and it can also contain an integer value a decimal.

6
00:00:32,260 --> 00:00:35,480
We're going to use one class to contain both.

7
00:00:35,500 --> 00:00:41,460
Really we should be using you know one class for a binary digit and one class for a decimal digit.

8
00:00:41,470 --> 00:00:44,680
However we're going to be using one class for both.

9
00:00:44,680 --> 00:00:48,750
So go ahead and let's open up the side here.

10
00:00:48,910 --> 00:00:52,310
Right click on binary and then click new file.

11
00:00:52,820 --> 00:00:59,380
Ok this is just going to be a swift file and I'm going to call it a binary decimal.

12
00:00:59,640 --> 00:01:00,100
OK.

13
00:01:01,640 --> 00:01:04,210
And you don't want let's say let's organize our code a little bit.

14
00:01:04,220 --> 00:01:10,700
Let's create a new group and let's create one for controller related files and then we can create one

15
00:01:10,760 --> 00:01:14,780
for Model layer files like our binary decimal.

16
00:01:14,780 --> 00:01:16,210
That's a model file.

17
00:01:16,220 --> 00:01:17,880
So open it up.

18
00:01:17,930 --> 00:01:22,960
And what we're going to do is we're going to create a class called binary decimal.

19
00:01:22,970 --> 00:01:25,620
Like I said put some curly brackets.

20
00:01:25,670 --> 00:01:28,960
And now we need to basically create some properties.

21
00:01:29,000 --> 00:01:34,180
So one byte of binary data has 8 bits.

22
00:01:34,220 --> 00:01:34,880
Right.

23
00:01:34,880 --> 00:01:37,300
So we're going to create a property called bits.

24
00:01:37,340 --> 00:01:39,480
So type var bits.

25
00:01:39,500 --> 00:01:40,570
All righty.

26
00:01:40,640 --> 00:01:45,550
And that is going to be an array of type int and we'll explain why in a little bit.

27
00:01:45,560 --> 00:01:51,730
But it's going to be an array of type int and whip's an array of type and it's going to be optional.

28
00:01:51,890 --> 00:01:56,780
OK because we are going to instantiate it when we create it.

29
00:01:56,780 --> 00:01:59,880
So it's going to be optional from the beginning.

30
00:01:59,990 --> 00:02:01,190
Until we give it a value.

31
00:02:01,220 --> 00:02:01,590
OK.

32
00:02:01,670 --> 00:02:09,290
So then we're going to go ahead and create a property called integer var integer of type int and it's

33
00:02:09,320 --> 00:02:10,590
also going to be optional.

34
00:02:10,750 --> 00:02:11,310
OK.

35
00:02:11,690 --> 00:02:18,910
Now we have the integer is going to store the decimal value for this particular number and the bits

36
00:02:18,920 --> 00:02:24,430
property is going to store an array of all of the zeros and ones for the binary data.

37
00:02:24,440 --> 00:02:27,560
I found that's the easiest way to do it for now.

38
00:02:27,770 --> 00:02:31,790
There may be a more efficient way and you could probably discover that if you dove into it.

39
00:02:31,790 --> 00:02:37,130
So next we need to create some initializers and there are two ways that we're going to create one of

40
00:02:37,130 --> 00:02:38,330
these digits.

41
00:02:38,420 --> 00:02:39,000
OK.

42
00:02:39,290 --> 00:02:45,410
One is either by passing in an array of bits and another one is by passing a decimal value.

43
00:02:45,410 --> 00:02:49,270
So let's go ahead and create two initializers let's do innit.

44
00:02:49,310 --> 00:02:52,160
And we're going to initialize it with bits k.

45
00:02:52,190 --> 00:02:55,910
And of course like before that's an array of type int.

46
00:02:55,930 --> 00:02:56,700
All righty.

47
00:02:56,840 --> 00:03:04,670
And if we pass in an array of bits we're going to set self up bits to be equal to bits.

48
00:03:04,700 --> 00:03:08,670
Now we can also initialize one of these by passing in a decimal.

49
00:03:08,810 --> 00:03:12,330
OK so we can say init decimal.

50
00:03:12,800 --> 00:03:14,840
And that's going to be a straight up integer.

51
00:03:14,840 --> 00:03:16,010
So type int.

52
00:03:16,430 --> 00:03:22,580
And inside of this initializer we're going to set self integer to be equal to the decimal value that

53
00:03:22,580 --> 00:03:23,650
we pass in k.

54
00:03:23,660 --> 00:03:24,230
Easy as that.

55
00:03:24,230 --> 00:03:28,570
Now we have two ways to initialize our class.

56
00:03:28,570 --> 00:03:29,710
Pretty cool.

57
00:03:29,810 --> 00:03:36,320
So beneath that what we're going to do is we are now going to write two functions one that's going to

58
00:03:36,320 --> 00:03:41,540
calculate the integer value for a particular array of bits.

59
00:03:41,750 --> 00:03:45,790
And the other function is going to calculate the binary value for a particular integer.

60
00:03:45,800 --> 00:03:47,420
So it's going to do the opposite.

61
00:03:47,420 --> 00:03:50,090
We're going to call those interchangeably when we tap on the button.

62
00:03:50,090 --> 00:03:51,380
Pretty cool stuff.

63
00:03:51,410 --> 00:03:57,800
So let's begin by actually calculating the binary value for a particular integer and then we're going

64
00:03:57,800 --> 00:04:01,700
to dive into calculating the integer value for a particular bit.

65
00:04:01,700 --> 00:04:13,190
OK so here we go let's write our function here func calculate binary value for int k and this is going

66
00:04:13,190 --> 00:04:16,160
to return a string and you might be wondering well why a string.

67
00:04:16,280 --> 00:04:24,840
Well we have to display it in a text field so let's go ahead and return it in the the type that we need.

68
00:04:24,950 --> 00:04:32,630
So we're going to go ahead and first create all of the rows we need for binary and if you think about

69
00:04:32,690 --> 00:04:36,860
what we learned about binary There are 8 bits in one byte of data.

70
00:04:36,860 --> 00:04:43,040
So we're going to create our eight slots and if you remember going from the right side all the way over

71
00:04:43,040 --> 00:04:49,640
to the left it starts at one then two then four and it doubles all the way up to 128 for one byte.

72
00:04:49,640 --> 00:04:55,490
So let's go ahead and create an array called rows so type let rows and that's going to be an array of

73
00:04:55,490 --> 00:05:05,700
all these values 128 K we're going to start there 64 32 16 8 4 2 and 1.

74
00:05:05,960 --> 00:05:12,140
So those are all of our values that we're going to use as placeholders for wherever our binary values

75
00:05:12,140 --> 00:05:13,000
are going to go.

76
00:05:13,250 --> 00:05:19,370
And then what we're going to do is we're going to go ahead and create another array called binary rows

77
00:05:19,670 --> 00:05:25,370
and then basically we're going to do some checking to see if the decimal number because remember we're

78
00:05:25,370 --> 00:05:28,030
calculating the binary value for an integer.

79
00:05:28,220 --> 00:05:35,630
If that integer value fits into one of these rows then we're going to basically need to return a one

80
00:05:35,750 --> 00:05:37,280
to that row right.

81
00:05:37,280 --> 00:05:42,440
So to do that we're going to go ahead and cycle through all the rows we're going to basically say hey

82
00:05:42,890 --> 00:05:46,240
are integer we're going to compare it to the row.

83
00:05:46,370 --> 00:05:52,260
If our value like let's say that we were doing 1 45.

84
00:05:52,360 --> 00:05:52,990
Right.

85
00:05:53,020 --> 00:05:59,920
So that's bigger than all of the channel so you know 128 for sure will be filled if 128 or sorry if

86
00:05:59,920 --> 00:06:04,530
145 minus 128 is greater than or equal to zero.

87
00:06:04,540 --> 00:06:09,820
We can return a 1 because we know that channel will be filled otherwise if it goes below zero.

88
00:06:10,000 --> 00:06:14,780
We know that that channel is too big or it doesn't fit in that channel so we return a zero.

89
00:06:14,860 --> 00:06:15,910
Pretty cool stuff.

90
00:06:16,180 --> 00:06:22,420
So we have our binary rows here now let's create a variable called binary rows and we're basically going

91
00:06:22,420 --> 00:06:24,310
to append the numbers to that.

92
00:06:24,310 --> 00:06:33,520
So go ahead and type var binary rows and that's going to be an array of type int and we need to explicitly

93
00:06:33,520 --> 00:06:38,140
declare it here because we're returning integers to it.

94
00:06:38,140 --> 00:06:40,210
Swift will yell at us if we don't.

95
00:06:40,210 --> 00:06:45,670
So just go ahead and create an empty array of type int and then what we're going to do is we're going

96
00:06:45,670 --> 00:06:51,230
to cycle through all the rows with a for loop check them to see whether they should be a 1 or 0.

97
00:06:51,310 --> 00:06:52,570
We'll get into that in a second.

98
00:06:52,840 --> 00:07:03,280
And then we're basically going to go ahead and append that particular digit 0 or 1 to to our array of

99
00:07:03,280 --> 00:07:04,420
binary rows right here.

100
00:07:04,420 --> 00:07:09,480
So go ahead and type four row in rows and then for each row.

101
00:07:09,580 --> 00:07:14,250
What we're going to do is we're basically going to see hey should this be a 1 or 0.

102
00:07:14,260 --> 00:07:20,500
And we can easily do that with a quick little function Let's actually write it below here funk 1 or

103
00:07:20,680 --> 00:07:21,720
0.

104
00:07:22,150 --> 00:07:28,300
And we're going to go ahead and check for a value and we'll just call it value of type int and then

105
00:07:28,300 --> 00:07:30,570
we're going to say with bit value.

106
00:07:31,840 --> 00:07:37,500
And then get value there for the internal parameter of type int and we're going to return an int and

107
00:07:37,620 --> 00:07:40,080
at the end of course 0 or 1.

108
00:07:40,300 --> 00:07:45,750
So we're passing in a value and we're passing in a bit value like 128 64 32.

109
00:07:45,970 --> 00:07:52,930
And what we're going to do is we're going to check hey if our value if like one hundred forty five minus

110
00:07:53,110 --> 00:07:58,380
the bit value 128 is greater than or equal to zero.

111
00:07:58,600 --> 00:08:03,300
OK we know that we should be returning a one because it fits in that channel.

112
00:08:03,400 --> 00:08:06,390
It's bigger than the channel that we're comparing it against.

113
00:08:06,440 --> 00:08:10,330
Otherwise OK we're going to return zero.

114
00:08:10,330 --> 00:08:11,990
So think about this 145.

115
00:08:12,220 --> 00:08:16,780
If we do 145 minus 128 we get 17.

116
00:08:16,780 --> 00:08:25,750
Then if we were to check again case 17 is 17 minus next channel is 64 17 minus 64 is that greater than

117
00:08:25,750 --> 00:08:26,720
or equal to zero.

118
00:08:26,980 --> 00:08:27,500
No.

119
00:08:27,610 --> 00:08:33,120
So we're going to return to zero next channel 32 17 minus 32 still zero.

120
00:08:33,130 --> 00:08:36,270
OK so we return 0 are still less than zero.

121
00:08:36,430 --> 00:08:40,370
Then we get to 16 17 minus 16 is greater than or equal to zero.

122
00:08:40,390 --> 00:08:41,650
Yes so we return a 1.

123
00:08:41,650 --> 00:08:49,000
Then we go all the way down and we end up with a 1 in the 1 channel 1 and 16 and 1 128 to make 145.

124
00:08:49,000 --> 00:08:50,050
Pretty cool stuff.

125
00:08:50,050 --> 00:08:55,360
So this little function can determine whether or not it should be a 1 or 0.

126
00:08:55,480 --> 00:09:02,680
So for Roe and Rose we're going to create a binary digit by typing let binary digit and that's going

127
00:09:02,680 --> 00:09:06,250
to be equal to the result of 1 or 0 for value.

128
00:09:06,360 --> 00:09:07,780
And what's our value.

129
00:09:07,780 --> 00:09:13,080
Our value is int member because when we create this we pass in an integer value.

130
00:09:13,210 --> 00:09:16,540
So we're going to use our integer from here.

131
00:09:16,720 --> 00:09:22,390
We're going to use the bit value k of the row because remember we're cycling through every one of these

132
00:09:22,390 --> 00:09:25,920
rows and we're comparing them then whatever we get back.

133
00:09:25,930 --> 00:09:27,910
We're going to append two binary rows.

134
00:09:27,910 --> 00:09:34,390
So go ahead and type binary rows pens and then type binary digit because that's what we just created.

135
00:09:34,390 --> 00:09:37,720
Now let's just build this and make sure that we're good we're getting an error here.

136
00:09:37,750 --> 00:09:43,560
Let's make sure we're paying attention to this along the way and it's saying something is not unwrapped.

137
00:09:43,600 --> 00:09:44,880
Although I'm not sure why.

138
00:09:44,890 --> 00:09:45,370
Oh.

139
00:09:45,740 --> 00:09:47,770
Our integer value is not unwrapped.

140
00:09:47,770 --> 00:09:50,500
And we should make sure that it is.

141
00:09:51,130 --> 00:09:53,260
Let's see is there a better way to do this.

142
00:09:53,260 --> 00:09:54,330
Let's see.

143
00:09:54,640 --> 00:09:59,250
Well we can't create an instance of this without it being instantiated.

144
00:09:59,470 --> 00:10:00,780
Oh I guess we can.

145
00:10:01,060 --> 00:10:07,360
So you know what let's go ahead and let's just use guard let here and call it int equals integer else

146
00:10:07,640 --> 00:10:08,440
we'll return.

147
00:10:08,440 --> 00:10:13,000
This is just a safe way to make sure that we have a value and we'll pass an int instead.

148
00:10:13,000 --> 00:10:15,670
And if we build this oh it's going to return.

149
00:10:15,760 --> 00:10:22,450
Let's return maybe error just for now it's not a great return it's not good air handling but it's better

150
00:10:22,450 --> 00:10:22,980
than what we had.

151
00:10:22,990 --> 00:10:30,460
So we now have determined whether all the values are ones or zeros or not and that's cool.

152
00:10:30,460 --> 00:10:33,910
So now what we need to do is we need to actually print out what we have.

153
00:10:33,910 --> 00:10:36,560
And this is just the way we can check to see what's going on.

154
00:10:36,730 --> 00:10:45,110
So we can just print maybe binary rows and that will show us whether or not what we have is good.

155
00:10:45,130 --> 00:10:46,830
So I want to actually check it out.

156
00:10:46,840 --> 00:10:48,930
So let's go into our view controller here.

157
00:10:49,090 --> 00:10:57,340
Let's set it up here with an instance let's just say let Bin digit equals binary digit or binary decimal

158
00:10:57,370 --> 00:10:58,310
I suppose.

159
00:10:58,480 --> 00:11:08,560
And let's pass in a decimal value of 145 K and then we're going to go ahead and print bin digit dot

160
00:11:09,160 --> 00:11:12,260
calculate binary value for int and let's see what we get.

161
00:11:12,440 --> 00:11:13,260
Build and run.

162
00:11:13,330 --> 00:11:16,240
Oh sorry we still need to return a string here.

163
00:11:16,270 --> 00:11:20,530
Let's return by Mary Rose.

164
00:11:21,040 --> 00:11:25,530
And you know what we can actually cast this as a string whoopsies.

165
00:11:25,600 --> 00:11:26,620
There we go.

166
00:11:26,620 --> 00:11:27,240
There we go.

167
00:11:27,240 --> 00:11:29,650
This is just temporary for now.

168
00:11:29,860 --> 00:11:33,250
Sorry and swift for you need to say string describing.

169
00:11:33,700 --> 00:11:35,320
And now we can print out that array.

170
00:11:35,320 --> 00:11:36,520
So let's go see what we did.

171
00:11:36,520 --> 00:11:41,180
Let's see how it worked for us and let's check it out.

172
00:11:41,200 --> 00:11:43,830
So yeah look at that.

173
00:11:43,870 --> 00:11:48,030
We typed in 145 and we're getting 1 1 1 1 1 1.

174
00:11:48,040 --> 00:11:52,450
All these ones which actually equals 255 in binary.

175
00:11:52,450 --> 00:11:56,470
Like I said it should be 128 16 and 1 but that's not working.

176
00:11:56,470 --> 00:11:59,810
So let's think about what is happening here.

177
00:11:59,920 --> 00:12:08,470
We are passing in our integer value of 145 and we're basically comparing it and we're appending all

178
00:12:08,470 --> 00:12:17,460
of the values to what we're doing let's think oh you know what I just realized what the problem is we're

179
00:12:17,460 --> 00:12:20,760
comparing 145 to every one of these channels.

180
00:12:20,760 --> 00:12:25,230
We're actually we're going to need variables that we can subtract the values as we go through the for

181
00:12:25,230 --> 00:12:30,960
loop so get rid of this guard let And let's just create a variable called new int and that's going to

182
00:12:30,960 --> 00:12:32,970
be equal to integer.

183
00:12:33,240 --> 00:12:33,750
OK.

184
00:12:34,080 --> 00:12:38,010
And you know what we can actually force and wrap it here we know we're going to have a value where we're

185
00:12:38,010 --> 00:12:41,200
going to pass in new int that shouldn't give us a problem.

186
00:12:41,370 --> 00:12:42,950
It's just telling us it was never mutated.

187
00:12:42,950 --> 00:12:50,050
So here's what we need to do after we check to see if the first value 128 should be a 1 or 0.

188
00:12:50,070 --> 00:12:55,330
What we need to do is we actually need to subtract the value of the row.

189
00:12:55,350 --> 00:12:58,100
Right because if 128 is a yes it should be on.

190
00:12:58,140 --> 00:13:02,720
We should subtract that so then we can compare what's left over to the next place.

191
00:13:02,730 --> 00:13:09,210
So go ahead and basically say if the binary digit return is a 1.

192
00:13:09,430 --> 00:13:17,110
OK we're gonna go ahead and say new int equals new int minus Rho.

193
00:13:17,460 --> 00:13:20,790
And that way new it gets smaller every single time.

194
00:13:20,790 --> 00:13:26,040
So let's build and run this now and let's see if that fixed our problem we're subtracting the values

195
00:13:26,040 --> 00:13:32,220
so that we can actually get the right printout and look at what we printed out here we have 1 0 0 1

196
00:13:32,370 --> 00:13:35,010
K in the 16 place 0 0 0 1.

197
00:13:35,010 --> 00:13:36,090
That is exactly right.

198
00:13:36,090 --> 00:13:40,420
That's the binary value for one hundred forty five.

199
00:13:40,440 --> 00:13:41,160
That's amazing.

200
00:13:41,160 --> 00:13:46,860
So let's head back here into our view controller get rid of this because this is just test code and

201
00:13:46,860 --> 00:13:52,650
then go back into your binary decimal function and we have one more function to write.

202
00:13:52,650 --> 00:13:56,430
We can now successfully convert an integer into binary.

203
00:13:56,430 --> 00:13:59,820
However we're not yet returning it as the string that we want.

204
00:13:59,820 --> 00:14:05,370
So what we want to do is we want to go ahead and basically take all the values in this array because

205
00:14:05,370 --> 00:14:10,950
remember it's an array of numbers we want to convert each of them to a string and then smushed them

206
00:14:10,950 --> 00:14:12,320
together into one string.

207
00:14:12,330 --> 00:14:14,270
We can display that in our textfield.

208
00:14:14,430 --> 00:14:18,610
So to do this what we're going to do is we're going to go ahead and get rid of that line.

209
00:14:18,870 --> 00:14:25,890
And then below our for loop we're going to go ahead and say let string two int array or sorry string

210
00:14:25,890 --> 00:14:26,890
from int array.

211
00:14:26,910 --> 00:14:32,240
My bad string from into in-tray is going to be equal to our binary rows.

212
00:14:32,370 --> 00:14:37,500
And we're going to use the map function to essentially go through each item and perform an action on

213
00:14:37,500 --> 00:14:39,410
them transforming them into something else.

214
00:14:39,420 --> 00:14:47,310
So type map and then what we can do is we can go ahead and put some curly braces here inside the map

215
00:14:47,340 --> 00:14:50,930
closure and we can just go ahead and say dollar sign zero.

216
00:14:50,940 --> 00:14:57,480
This is a temporary placeholder for each item in this array we're cycling through and whatever dollar

217
00:14:57,480 --> 00:15:04,610
signs zero is that is the value for each item as we cycle through sort of like Roe is two rows.

218
00:15:04,680 --> 00:15:07,080
The dollar sign zero is to map for this array.

219
00:15:07,080 --> 00:15:13,110
So we're going to cast each item as a string and then we can call a cool function called joined and

220
00:15:13,110 --> 00:15:18,840
it's going to smush those all together into one big string so type string and we're going to cast this

221
00:15:18,840 --> 00:15:20,940
value as a string.

222
00:15:21,090 --> 00:15:27,270
Then we can go ahead and return String from an array dot joined.

223
00:15:27,450 --> 00:15:31,440
And what it's going to do is it's going to smush it together into one string.

224
00:15:31,800 --> 00:15:34,020
And that's that's all we got to do.

225
00:15:34,020 --> 00:15:36,300
We're now returning a string of our binary value.

226
00:15:36,300 --> 00:15:38,000
Really really cool stuff.

227
00:15:38,010 --> 00:15:43,130
So now let's write the function that we need for converting a binary value into an integer value.

228
00:15:43,140 --> 00:15:52,850
So let's go ahead and let's say phunk calculate end value for binary.

229
00:15:53,670 --> 00:15:54,260
OK.

230
00:15:54,420 --> 00:16:01,110
Now of course we need to return a string here as well and that's just easier when dealing with textfield

231
00:16:01,120 --> 00:16:02,970
that's how I'm going to do it for this.

232
00:16:02,970 --> 00:16:04,320
All right so here's what we're going to do.

233
00:16:04,320 --> 00:16:07,850
We're going to go ahead and begin with a value animal to player.

234
00:16:08,160 --> 00:16:14,400
We're basically going to go ahead and take our value and we're going to compare it against a multiplier

235
00:16:14,760 --> 00:16:22,190
then every time we're going to double that multiplier so we get to 4 8 632 64 128.

236
00:16:22,200 --> 00:16:23,480
You see where I'm going here.

237
00:16:23,490 --> 00:16:23,960
Let's do it.

238
00:16:23,980 --> 00:16:28,950
You'll understand that when we actually see it in practice so I'm going to start by creating a variable

239
00:16:28,950 --> 00:16:33,490
called value var value equals zero.

240
00:16:33,780 --> 00:16:38,740
Then we're going to create a multiplier var multiplier equals 1.

241
00:16:38,760 --> 00:16:44,550
That's our first multiplier because the first channel is one then we're going to go ahead and use our

242
00:16:44,610 --> 00:16:50,220
bits that are used when we actually create an instance of this class.

243
00:16:50,250 --> 00:16:55,400
So we're going to go ahead and say if let bits equals bits.

244
00:16:55,630 --> 00:17:00,030
Ok that's pretty redundant but basically what we're saying is if Bitts has a value we're going to create

245
00:17:00,030 --> 00:17:02,500
a constant called bits that has that value.

246
00:17:02,910 --> 00:17:08,040
And we're going to go ahead and then use a for loop to cycle through all the bits we passed in.

247
00:17:08,040 --> 00:17:11,240
So type 4 bit in bits.

248
00:17:11,520 --> 00:17:17,260
But the is when we cycle through it it's going to start at the beginning at 128 but we want to start

249
00:17:17,260 --> 00:17:19,510
at 1 and multiply up.

250
00:17:19,510 --> 00:17:22,490
So we're going to go ahead and say it's not reversed.

251
00:17:22,870 --> 00:17:28,360
And then we can go through backwards we can say let new value OK.

252
00:17:28,390 --> 00:17:31,880
Meaning the new value of the bit.

253
00:17:31,960 --> 00:17:33,580
So we're going to start with one.

254
00:17:33,580 --> 00:17:35,980
We're going to say let new value equals bit.

255
00:17:36,100 --> 00:17:41,020
In this case 1 k times the multiplier.

256
00:17:41,050 --> 00:17:42,830
So the new value is 1.

257
00:17:43,060 --> 00:17:50,710
That's the first channel k value is going to be equal to the value which right now is zero.

258
00:17:50,740 --> 00:17:52,660
Plus the new value.

259
00:17:52,660 --> 00:17:54,590
So right now we're at 1.

260
00:17:54,880 --> 00:17:58,020
Then we need to make the multiplier 2.

261
00:17:58,360 --> 00:17:59,180
OK.

262
00:17:59,470 --> 00:18:06,880
So what we're going to say is multiplier equals multiplier times 2 because every time we're multiplying

263
00:18:06,880 --> 00:18:07,700
it by 2.

264
00:18:07,960 --> 00:18:10,270
So let's think about what we're doing after this.

265
00:18:10,270 --> 00:18:11,710
We've gone through the for loop.

266
00:18:11,770 --> 00:18:18,160
We have basically multiplied what we needed and now at the end of the for loop we can say return String

267
00:18:19,420 --> 00:18:24,370
value and what we're doing is we're taking that integer or if we're encapsulating it as a string and

268
00:18:24,370 --> 00:18:26,160
we're sending it back to the function.

269
00:18:26,170 --> 00:18:28,070
So let's go through this one more time.

270
00:18:28,090 --> 00:18:35,260
Oh whoops we need to actually think about this in a different way we're inside of if let let's use guard

271
00:18:35,260 --> 00:18:37,210
let that might be a little easier way to do this.

272
00:18:37,210 --> 00:18:46,840
Guard let bits equals bits else we can return nothing.

273
00:18:47,290 --> 00:18:47,860
That's fine.

274
00:18:47,860 --> 00:18:50,250
Or maybe we can just return error.

275
00:18:51,520 --> 00:18:51,960
That's fine.

276
00:18:51,970 --> 00:18:52,570
OK.

277
00:18:52,750 --> 00:18:53,260
Cool cool.

278
00:18:53,260 --> 00:18:55,600
So get rid of that little curly brace there.

279
00:18:55,600 --> 00:18:58,360
Select all of this and move it back.

280
00:18:58,480 --> 00:19:02,820
And now we can go ahead and actually use everything how we want the error should be gone.

281
00:19:02,970 --> 00:19:06,940
And so let's go through this again so we just went through one.

282
00:19:06,940 --> 00:19:13,630
We know if our values one hundred forty five k we have a value in the ones place we do one times one.

283
00:19:14,050 --> 00:19:19,750
That's the new value and we get one as our value multipliers now too.

284
00:19:19,750 --> 00:19:22,600
So let's go through again the next value is zero.

285
00:19:22,600 --> 00:19:28,980
So the next bit is zero so let new value equals zero times 2 which is equal to zero.

286
00:19:29,140 --> 00:19:36,160
The value is going to be equal to 1 plus 0 K because the current value is 1 because we did have something

287
00:19:36,160 --> 00:19:38,090
in the 1 channel 1 plus 0.

288
00:19:38,110 --> 00:19:42,850
Now the multiplier is 2 times 2 which is 4 for the 4 channel.

289
00:19:42,880 --> 00:19:48,520
So we're going to have a zero here Zero here but then we get to 16 and we have a 1.

290
00:19:48,550 --> 00:19:49,510
So check it out.

291
00:19:49,750 --> 00:19:52,780
Let new value equals one time 16.

292
00:19:52,780 --> 00:19:56,930
So new values equal to 16 value equals 1.

293
00:19:56,950 --> 00:20:00,030
Because remember the value is still 1 plus 16.

294
00:20:00,040 --> 00:20:04,150
Now it's 17 multiplier is four times 2.

295
00:20:04,150 --> 00:20:04,870
Now it's eight.

296
00:20:04,870 --> 00:20:06,430
Do we have anything in eight.

297
00:20:06,430 --> 00:20:06,670
No.

298
00:20:06,670 --> 00:20:07,630
Do we have anything in 16.

299
00:20:07,630 --> 00:20:07,870
No.

300
00:20:07,870 --> 00:20:08,470
How about 32.

301
00:20:08,470 --> 00:20:09,030
No.

302
00:20:09,220 --> 00:20:10,630
Then we get to 128.

303
00:20:10,630 --> 00:20:14,760
We end up with 128 plus 17 which gets us to 145.

304
00:20:14,760 --> 00:20:20,320
So we're basically just looping through multiplying our multiplier by times to anything that's a zero

305
00:20:20,320 --> 00:20:21,490
will stay at zero.

306
00:20:21,490 --> 00:20:24,520
Anything that's a one will become the value of that channel.

307
00:20:24,520 --> 00:20:25,330
That's how it's going to work.

308
00:20:25,330 --> 00:20:28,150
So our our class is now done.

309
00:20:28,150 --> 00:20:33,910
We are able to do all of the math that we need to to convert binary to decimal and decimal to binary.

310
00:20:33,910 --> 00:20:38,940
Let's head over to the next video where we're going to put the two together our UI and this code.

311
00:20:38,980 --> 00:20:39,680
I'll see you there.
