Forums - Open Redstone Engineers
Converting to binary using a stack - Printable Version

+- Forums - Open Redstone Engineers (https://forum.openredstone.org)
+-- Forum: ORE General (https://forum.openredstone.org/forum-39.html)
+--- Forum: Tutorials (https://forum.openredstone.org/forum-24.html)
+---- Forum: Advanced Tutorials (https://forum.openredstone.org/forum-26.html)
+----- Forum: Concepts (https://forum.openredstone.org/forum-28.html)
+----- Thread: Converting to binary using a stack (/thread-412.html)



Converting to binary using a stack - fl3tching101 - 06-20-2013

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. Blush 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!)


RE: Converting to binary using a stack - David - 07-04-2013

Use the enter key on your keyboard, it's not there for nothing!


RE: Converting to binary using a stack - JeremyG - 07-05-2013

Add
ed

so
me

ent
ers

f
or

y
a

:

3

EDIT: I love you xeo <3


RE: Converting to binary using a stack - David - 07-07-2013

LOL, GJ Xeo, GJ. Tongue


RE: Converting to binary using a stack - Chibill - 07-15-2013

I may use this in me and hime's MCX.

Visualizing Minecraft!