So I found this method when looking up stacks on Wikipedia as one of their many uses. For those who don't know what a stack is...look it up I don't have time to explain! Ok, ok, fine I'll explain.
So a stack, in this case, is a LIFO (Last In First Out) data type, meaning literally the last number put into the set is the first to come back out. Stack is a perfect name because it works just like a stack of something.
If you imagine a stack of, say, books, you can't (theoretically) remove the book at the bottom of the stack because the rest of the books would fall.
So the last piece of data put "onto" the stack has to come out first, then the next, and the next, until the first thing that was put down can be pulled out. So, onto the explanation of how to convert from decimal to binary using a stack.
So you take your number, let's say 42. In a loop, you divide your number by two, and if it has a remainder you push a 1, if it doesn't you push a 0. (Push means place onto the stack)
Thus:
42/2 = 21 remainder 0. Push 0.
21/2 = 10 remainder 1. Push 1.
10/2 = 5 remainder 0. Push 0.
5/2 = 2 remainder 1. Push 1.
2/2 = 1 remainder 0. Push 0.
1/2 = 0 remainder 1. Push 1.
Now the stack is built, however if you read it off, it's incorrect, right? 010101 != 42? Well that is because to read a stack, you have to follow LIFO. So you pop off the numbers and read them and you will get the correct answer of 101010.(Pop means to get the number off the top of the stack.)
So there you go, how to convert a number from decimal to binary using stacks. To test yourself on that concept, I have a program I wrote in C++ using this method. (I will attach it to this post.) Happy popping! (lol)
(Or maybe I won't upload them because it won't allow me to in like any form!)
So a stack, in this case, is a LIFO (Last In First Out) data type, meaning literally the last number put into the set is the first to come back out. Stack is a perfect name because it works just like a stack of something.
If you imagine a stack of, say, books, you can't (theoretically) remove the book at the bottom of the stack because the rest of the books would fall.
So the last piece of data put "onto" the stack has to come out first, then the next, and the next, until the first thing that was put down can be pulled out. So, onto the explanation of how to convert from decimal to binary using a stack.
So you take your number, let's say 42. In a loop, you divide your number by two, and if it has a remainder you push a 1, if it doesn't you push a 0. (Push means place onto the stack)
Thus:
42/2 = 21 remainder 0. Push 0.
21/2 = 10 remainder 1. Push 1.
10/2 = 5 remainder 0. Push 0.
5/2 = 2 remainder 1. Push 1.
2/2 = 1 remainder 0. Push 0.
1/2 = 0 remainder 1. Push 1.
Now the stack is built, however if you read it off, it's incorrect, right? 010101 != 42? Well that is because to read a stack, you have to follow LIFO. So you pop off the numbers and read them and you will get the correct answer of 101010.(Pop means to get the number off the top of the stack.)
So there you go, how to convert a number from decimal to binary using stacks. To test yourself on that concept, I have a program I wrote in C++ using this method. (I will attach it to this post.) Happy popping! (lol)
(Or maybe I won't upload them because it won't allow me to in like any form!)