okay now we’re gonna use a stack to are solve our problem of converting
decimal to binary and other bases so binaries especially
important to keep your science and programming are because computer store everything in
primary ones and zeros on well it’s really hard to work factual
ones and zeros solar programmers working text us more
hacks which is easier to use than binary are because it has a short number digits it’s like cars realtor 10 about the
digits 0 through 9 but they add on these extra digits ABCDE in there so you actually group groups have for
binary digits each group digits for produces one Texas bondage up by the way
the hexadecimal code will be the new way you do IP addresses
arm on the new internet so slick at how you
convert to based to and talk about jazz basses and in general so what base for a number
means if it’s a dust bowl numbers and based 10 sometimes such corruption but putting a
some us lower case 10 here an so the no normal number you use to 33 can be equally Express and binary as this number and the way the digits
work this goes back to beginning refer tech
are you a busy road digit the ones digit the Tuesday the tens what at Israel’s
digit the tens digit in the hundreds digit in
binary so let’s look at how the digits actually
correspond her to the base our so this in just more which is the based 10 this is called the the ones digit the tens digit and the
hundreds digit resistant represents how many hundreds
family towns and how many ones in binary it works the same way accepted space to so this is the ones
digit the to use digital two fours digit the 8 digit so it goes up up by multiplying
by to get each successive digit just like the tens goes up but not playing by
10 so you can write this out in algebra the number to 33 is actually two times tend to the second power plus
three times 10 that the first power first three times tenders are of power
and for minor you have to the seventh through the six to fifth to fourth we have each digit
more apply some digits or just the term is a row
you’re playing by zero so those are these digits more planned
out you know 7 all the way down to 20 which is one
and so that gives you the actual values so is an easy are going to do this it you’re converting binary what you do
is you divide by two and collect the
remainders a when you divide by two so here’s how it works: you gonna be
doing to operations you take the original number
an you divided using an injured by by two when you get
a new number that new numbers gonna go in the next operation but the new take the original number and
you its remainder by doing modules to and you gonna remainder one so because
that’s an odd number and so you push the one on to a stacked okay and then you the after the first operation you’re down
116 which is the number remaining after you divide by two are you divide that by to you 58 see you
can use that next step you save it and then you get three manger 116 doing that divide which is using the
modulars you gonna get a0 and caps because that’s
even so you push the sir on the stack you
keep doing that and chill you get 20 as the when you do the divider all done
so and as you’ve been doing that you’ve been pushing these numbers on this taxi
push to one as 00 01 Azera 1011 so what happens you’ve been pushing
these numbers on the stack going in this reaction but actually read them out as a binary
number you go the opposite direction you reverse the numbers so now you just popping up the stack and
put it into a string and you’re gonna have are binary the
morning binary value so let’s look at the code for that and
the code is gonna be a method called divide by two you
give it a number and it’s going to return a string which
is going to be a series of ones and zeros which will be the right reputation for
this number I took the code it’s in the book and I just are modified it your ego
divide by two so basically I used more modern fight on
were parables euros name separate by
underscores and so you’ll see divide by two your
number you go to stack while the decimal
numbers greater than 0 such kind keep going until you’re done what it’s
going to do to get the remainder of the right numbers that’s gonna be 10 pushes
out onto the stack and that divides the number by two and
replaces dust will number about that looks again so this is gonna keep being a smaller
and smaller numbers advice by two when this loop is all done it’s now
going to pop the stack and bolstering sources while the stack is not empty are the string that authorities started with
which is empty it’s going to be that empty string for whatever reason it
plus the new thing I popped a pop from the stacking
too broke to a string circuit can meet said and so it’s going to keep adding these
to the writer the string sorrel build a binary number just
returns that it’s pretty simple are so you’ll see this is what he does
in the book is to vice heat causes for 42 so printout I’ll see right to see forty 2010 1010 and so that matches what they have in
the book and I just have a some a search for easy
ones when you’re writing a program you should always tested so think about when you think about test
cases you wanna subsea test cases you know the answer to are so
I know that to was a binary 10 that’s pretty easy are
because they work with binary a lot I know up by eight that’s just one less than the link to
the buy it is 255 so it’s going to be a ones and i know if
i take 8 and add one to Eternity that set in
the one bit set so here’s a Ford joinery digits this is the expert
before bit two-bit the one bit so only the Eaton one better set tourism easy things I can
check I assert that the those are true so all these expressions should be true
and it doesn’t print that I have an air sign
o the assertions have passed okay now let’s look at expanding this to
do for more work so we think about it are we can actually
convert any base so if we want to convert to base eight
for example we can divide by a to get the remainder and get the
remainder using I marge was a so in a particular base the
modulus operator can return the last digit have any
number that base so you get the the original number modulus the base
will give you the last digit and it turns out if you divide and I she had to slash is here and fight
on work to do in danger divider the number remaining by the base you get a new remaining
number so we can modify the code to do this that we already had and would show you the code from the book
your so this is called base converter works the same way you pass an original
number and the base you want to convert to and it has a string here with the digits
for up to base 16 so handle everything from the are just to do sir on one for base
to through through 10 of 100+ based 10 and 0.2% after who are covered 16 and
everything in between so bill to stack group through doing the
numbers for you see it’s going the remainder of the base and dividing by
the base every time to the loop when it’s all
done it just puts all those digits onto the string and returns it so it’s
very similar to the other method and I print out three here I convert the number 26 to base 2 basie and p16 so let’s run this where you go so this is our great way to use a stack
to make things really support convert from one from any
base to a particular base and display it that

1 Comment

KAMIL KARIM · January 2, 2018 at 5:32 pm

Can we get code

Leave a Reply

Your email address will not be published. Required fields are marked *