<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>programming tutorials Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/programming-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/programming-tutorials/</link>
	<description></description>
	<lastBuildDate>Sun, 20 Jul 2025 09:16:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>programming tutorials Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/programming-tutorials/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Master Stacks: Fun Guide to Data Structures, LIFO &#038; Real-World Uses</title>
		<link>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/</link>
					<comments>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 20 Jul 2025 09:15:59 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[browser history stack]]></category>
		<category><![CDATA[call stack]]></category>
		<category><![CDATA[coding for beginners]]></category>
		<category><![CDATA[computer science basics]]></category>
		<category><![CDATA[data reversing]]></category>
		<category><![CDATA[data structure basics]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[learn coding]]></category>
		<category><![CDATA[LIFO data structure]]></category>
		<category><![CDATA[programming tutorials]]></category>
		<category><![CDATA[push pop stack]]></category>
		<category><![CDATA[stack data structure]]></category>
		<category><![CDATA[stack in C++]]></category>
		<category><![CDATA[stack operations]]></category>
		<category><![CDATA[stack programming]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=186</guid>

					<description><![CDATA[<p>Learn stacks: fun guide to data structures, LIFO, &#038; uses like browser history. Clear diagrams for beginners!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/">Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses" width="1380" height="776" src="https://www.youtube.com/embed/mAqB77KUUKY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Want to nail down stacks? This beginner-friendly video breaks down the stack data structure with clear diagrams and a laid-back vibe. We’ll walk you through how stacks work, why they reverse data (hello, LIFO &#8211; Last In, First Out), and how they’re used in real-world stuff like browser history, undo features in editors, and even the call stack in your code. Whether you’re just starting out or brushing up on data structures, we keep it simple with hands-on demos of pushing, popping, and checking stack size—no boring jargon here! Subscribe for more coding tutorials to level up your skills, and let us know in the comments what you want to learn next. Check out our site via the QR code in the video for more coding goodness!</p>



<p>Introduction to Stack 00:00:00<br>Explaining Stack Concept 00:00:06<br>Drawing Stack Diagram 00:00:30<br>Adding Elements to Stack 00:00:53<br>Stack Rules and Operations 00:01:16<br>Pop Operation and Interface 00:06:09<br>Stack Size and Empty Check 00:07:59<br>Tracing Stack Operations 00:11:46<br>Stack as Data Reverser 00:17:44<br>LIFO/FILO Explanation 00:21:13<br>Stack Use Cases 00:24:10<br>Call Stack Mention 00:26:18<br>Conclusion and Subscribe Request 00:27:21<br>Outro and Website Promotion 00:28:34</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>Hello there.</p>



<p>In this video, I&#8217;m going to show you a data structure called a stack.</p>



<p>We&#8217;re going to make a diagram of it.</p>



<p>We&#8217;re going to explain how it works.</p>



<p>We&#8217;re going to look at its typical interface,</p>



<p>and we&#8217;re just going to kind of work through the concept</p>



<p>of what is a stack and what does it do and all that stuff.</p>



<p>Where&#8217;s your daddy and what does he do?</p>



<p>So for starters, what am I talking about?</p>



<p>What am I talking about?</p>



<p>Let me open up a little notepad here.</p>



<p>And I&#8217;m going to start drawing.</p>



<p>A stack is pretty simple when you first think about it.</p>



<p>It just takes a little bit more work to really get all the details down.</p>



<p>Imagine I have a data structure and maybe like at the bottom here,</p>



<p>there&#8217;s like some grass.</p>



<p>And so we can say that the data structure is empty when we first start.</p>



<p>Okay, so there&#8217;s kind of a stack here, but it&#8217;s empty.</p>



<p>So there&#8217;s nothing really on screen.</p>



<p>Imagine then I wanted to add integers into this stack.</p>



<p>So I&#8217;m going to say let&#8217;s add the integers eight and then two and then three and then four for some reason.</p>



<p>Okay, so how do we do this?</p>



<p>The first thing we do is we take the eight and we just put it on top of the stack.</p>



<p>You can imagine this as a stack of boxes or a stack of books or a stack of whatever.</p>



<p>When we add items, we&#8217;re always going to add to the top of the stack.</p>



<p>When the stack is empty, we&#8217;re obviously just going to add like at the very bottom, you know, at the ground floor.</p>



<p>But that&#8217;s sort of the top.</p>



<p>when we add the two here the next thing that happens is we just put the two on top of the eight</p>



<p>and the next thing that we do is we uh put the three on top of the two and so forth right so</p>



<p>it&#8217;s important to understand a few a few rules about stacks let me add that four real fast</p>



<p>in stacks you&#8217;re only allowed to add to the top or remove from the top you&#8217;re not allowed to add</p>



<p>or remove from anywhere else let me put a t for top right here so basically you&#8217;re allowed to add</p>



<p>So basically you&#8217;re allowed to add and remove from the top.</p>



<p>So if I wanted to add, let&#8217;s say a 12, the 12 would have to go on top of the four.</p>



<p>It couldn&#8217;t go anywhere else in this data structure.</p>



<p>And if I wanted to remove data from this stack, I could only remove the four.</p>



<p>I could not remove the three or two or the eight or anything.</p>



<p>You&#8217;re also not allowed to look at the middle of the stack.</p>



<p>You&#8217;re not allowed to look anywhere but the top.</p>



<p>So if I wanted to look at the three, the two or the eight, I wouldn&#8217;t be allowed to do it.</p>



<p>I can only look at the four.</p>



<p>look at the four and if I want to see that three I have to pop the four off</p>



<p>first in order to just see what&#8217;s under it and technically unless you have the</p>



<p>data you added memorized somewhere else you probably shouldn&#8217;t know what&#8217;s under</p>



<p>the four until you actually remove it okay so we can add and remove from the</p>



<p>top we can&#8217;t look in the middle we can&#8217;t look at the bottom or anything like that</p>



<p>and let&#8217;s do a little let&#8217;s do a little sequence of pops let me show you what</p>



<p>So first off, you can imagine a stack is sort of like a vector or a list with less functionality or more restrictions.</p>



<p>It&#8217;s useful to add restrictions on top of more robust data structures for the purposes of, I don&#8217;t know, kindling your imagination or allowing yourself to use easier implementations or whatever.</p>



<p>If you&#8217;re a musician or artist, probably at some point you&#8217;ve been stumped and you&#8217;ve had writer&#8217;s block and you can&#8217;t figure out what to create.</p>



<p>can&#8217;t figure out what to create so uh you know i know sometimes musicians when they get writer&#8217;s</p>



<p>block they&#8217;ll they&#8217;ll go to the swap meet and they&#8217;ll buy like an old dusty dirty uh casio</p>



<p>keyboard from 1985 and then they&#8217;ll write a full song just with that one piece of equipment and it</p>



<p>really stirs their creativity or it sparks their creativity and then when they&#8217;re done then they&#8217;ll</p>



<p>probably upgrade to better equipment but anyway so there&#8217;s there&#8217;s a bunch of different reasons to</p>



<p>use you know lesser data structures although this is not really lesser it&#8217;s very useful</p>



<p>It&#8217;s very useful in its own right.</p>



<p>So let&#8217;s see the interface.</p>



<p>So what can we do in a stack?</p>



<p>I&#8217;m going to put S for stack.</p>



<p>Actually, I&#8217;m just going to type this up as code.</p>



<p>No need to use the pen the whole entire time.</p>



<p>Let&#8217;s see.</p>



<p>So I&#8217;m going to put S for stack.</p>



<p>Maybe I&#8217;ll say, you know, stack that holds integers.</p>



<p>And I&#8217;ll just put an S here.</p>



<p>If you don&#8217;t know how to code right now, at this point in your career, just bear with</p>



<p>me.</p>



<p>variable of type stack and in C++ these stacks you know stacks are a template of data structure</p>



<p>which means the integers that we are holding inside of the stack they could be any other data</p>



<p>type that we wanted to including custom classes so in C++ we would use these angle brackets to</p>



<p>say I want my stack to hold integers and then I&#8217;m going to say s is the variable that I&#8217;ll use for</p>



<p>the stack so but this is not really a programming video this is just about stacks so if I wanted to</p>



<p>stack to push something like onto the top of the stack i would use the the method push the method</p>



<p>push usually has this prototype let&#8217;s see i&#8217;ll do stack push and it usually takes in a t type element</p>



<p>and again the t type it&#8217;s a templated data type which means right now if i just declared a stack</p>



<p>that was of type integer it holds integers then the t would actually expand under the hood in c</p>



<p>hood in C++ anyway at compile time to be an integer. So it would be like typing int e, right?</p>



<p>So whenever I type t, just understand that that means one instance of a thing that you could put</p>



<p>into the stack. So we&#8217;ve got a push here and usually the return type is void and it takes in</p>



<p>one element to push. And that means if I&#8217;m going to actually call, let me put some comments here.</p>



<p>If I&#8217;m going to actually call push on the stack, I&#8217;ve got to give it something to push and the</p>



<p>type of the thing that I&#8217;m pushing has to match what the stack was declared to hold. So in this</p>



<p>case, it&#8217;s integers. So I&#8217;m going to put an eight to match the diagram up above. And then after that,</p>



<p>maybe I wanted to push another number. So it&#8217;s going to be a two. And I&#8217;m just going to match</p>



<p>what the diagram has up above. So eight, two, three, four. So that&#8217;s the interface for pushing</p>



<p>usually in C++. And other languages should be similar. The other thing you can do with stacks</p>



<p>We haven&#8217;t done that yet, but let me show it to you first.</p>



<p>POP usually comes in two forms,</p>



<p>depending on what implementation you&#8217;re looking at.</p>



<p>So sometimes POP returns nothing.</p>



<p>It simply removes an item from the top of the stack.</p>



<p>And sometimes it returns a copy of the item</p>



<p>that you&#8217;re actually removing.</p>



<p>So I&#8217;m going to put like stack here and then like stack</p>



<p>just to show you that it&#8217;s inside of the namespace.</p>



<p>Oh, I don&#8217;t know.</p>



<p>Maybe that&#8217;s too C++y.</p>



<p>I&#8217;ll get rid of that stuff.</p>



<p>Hold on.</p>



<p>hold on so we&#8217;ll maybe do this just to show you that we have three separate</p>



<p>parts to this little code snip so sometimes pop doesn&#8217;t give you a copy of</p>



<p>the thing it removes sometimes it gives you a copy of the thing it removes in</p>



<p>cases where pop does not give you a copy of the thing it removes then you would</p>



<p>probably want to call top right before you popped and top will return a copy of</p>



<p>Just to clarify, if I wanted to remove something and get a copy of it and I had this line 13</p>



<p>form of pop from whatever implementation I was using, then well, I would just call pop</p>



<p>and I would do something like this, you know, auto element equals, you know, the stack dot</p>



<p>pop.</p>



<p>And what would happen is under the hood, it removes the item and then it also returns</p>



<p>a copy to me.</p>



<p>Sorry for being too C++ in this video.</p>



<p>On the other hand, if I have the form that has void for pop,</p>



<p>then I would have to do something like this.</p>



<p>I&#8217;d have to say auto element equals stack.top just to get a copy first,</p>



<p>get a copy of that for before I remove it.</p>



<p>And then when I&#8217;m finished, then I can say s.pop just to grab a copy of it and then remove it.</p>



<p>So for now, we&#8217;re just going to use the form that gives you a copy of the data</p>



<p>at the same time that you&#8217;re removing it.</p>



<p>thing to keep in mind let&#8217;s see there are two other functions i wanted to mention mention real</p>



<p>fast so for now i&#8217;m going to say that we only have the t pop type you can also check to see if a stack</p>



<p>is uh is empty and you can also check its size uh usually those are functions that have those names</p>



<p>but i&#8217;m going to do let&#8217;s see boolean empty and then i&#8217;m going to say size type size again sorry</p>



<p>an unsigned 64-bit integer so just like a count or a size something that&#8217;s just</p>



<p>going to be a whole number and never negative so we have this little</p>



<p>interface that this data structure has and we have been calling push so far so</p>



<p>let&#8217;s go ahead and do some pops I&#8217;m gonna say auto auto elements equals s</p>



<p>dot pop maybe I&#8217;ll do two of those in a row maybe instead of calling that</p>



<p>just so that we can have different variables to put our pops in.</p>



<p>And then maybe after that, I&#8217;ll do some more pushes.</p>



<p>I&#8217;ll say, let&#8217;s push like a 15 and let&#8217;s push like a 25.</p>



<p>And then I think that means it&#8217;s probably time to erase that,</p>



<p>the top part of the diagram with the 8, 2, 3, 4</p>



<p>in favor of just whatever pushes and pops we&#8217;re doing.</p>



<p>So let me just do this real fast.</p>



<p>Okay, so we did our pushes.</p>



<p>two three four and then I&#8217;m going to try to grab a couple pieces of data with these pops</p>



<p>and for pop all you have to do is just to well I should have mentioned this before but first</p>



<p>check to make sure that there is something to pop for example if I had a blank stack let&#8217;s say I had</p>



<p>a blank stack right here there&#8217;s just like nothing on it and this green line is non-standard I&#8217;m just</p>



<p>drawing it for fun but imagine I had a blank stack with no items and I decided to call pop on it</p>



<p>well you&#8217;re not allowed to pop them an empty stack there&#8217;s nothing there to pop which means that&#8217;s a</p>



<p>stack there&#8217;s nothing there to pop which means that&#8217;s a really really naughty thing in c++ and</p>



<p>other languages we would say that something exceptionally bad has happened the user tried</p>



<p>to pop something that wasn&#8217;t there and so then we would do something called throwing an exception</p>



<p>at the user again this is not really a code video but if you know a little code hopefully you</p>



<p>understand try catch blocks and throwing exceptions when bad things happen so we would just throw an</p>



<p>exception at the user if you don&#8217;t know how to code at this point then just keep in mind we would</p>



<p>mind we would refuse to do it we would say something bad has happened we can&#8217;t do that</p>



<p>and then when you&#8217;re doing your pushes sometimes depending on what what you&#8217;re using to implement</p>



<p>the stack under the hood inside of your code you might decide that you&#8217;re using let&#8217;s say you&#8217;re</p>



<p>using an array under the hood which has a fixed capacity if you run out of capacity and you can&#8217;t</p>



<p>then you would throw if the user tried to push something into a full stack we would call that a</p>



<p>stack overflow and then when we tried to pop from an empty stack we would call that stack underflow</p>



<p>but for the purposes of this video just assume that the stack is just a diagram and it has no</p>



<p>capacity so we can just add as many things as we want on it or maybe we&#8217;re using a linked list</p>



<p>under the hood which would have an unlimited capacity or limited only by the machine&#8217;s</p>



<p>But anyway, so let&#8217;s go ahead and try to do this pop.</p>



<p>So let&#8217;s do a pop here.</p>



<p>Well, in the pop, we just look at the top only, right?</p>



<p>So we say, well, you know what?</p>



<p>Let&#8217;s make this more interesting.</p>



<p>Hang on a second.</p>



<p>Let&#8217;s make this more interesting.</p>



<p>Let&#8217;s do this.</p>



<p>I&#8217;m going to do auto a equals s dot top.</p>



<p>So I&#8217;m going to grab a value without actually removing it.</p>



<p>Okay.</p>



<p>So more interesting.</p>



<p>So we push the 8 and we push the 2.</p>



<p>So at that point in time, actually let&#8217;s trace this from scratch.</p>



<p>So we have like an empty stack here.</p>



<p>There&#8217;s nothing there.</p>



<p>And we&#8217;re going to push the 8.</p>



<p>So the 8 goes on the very top, just like we did last time.</p>



<p>I&#8217;m going to put an 8 there.</p>



<p>And then we&#8217;re going to push the 2 next.</p>



<p>So the 2 just kind of goes on top.</p>



<p>And then the next line, line 12, we&#8217;re just checking the top.</p>



<p>we&#8217;re allowed to actually look at the top if we want to. So since two is at the top, that means</p>



<p>a is equal to two. I&#8217;m going to put a two here to remind ourselves that a is now equal to two.</p>



<p>Then we&#8217;re going to do a push three. So I&#8217;m just going to stick a three on top of the stack.</p>



<p>And then we&#8217;re going to push a four, four is on top of the stack. Now, again, we&#8217;re not allowed</p>



<p>to look at anything in the middle, anything, anything, but the top. So for example, when the</p>



<p>it and remove it but now that we have a four on top we can no longer even see the three it&#8217;s not</p>



<p>available to us anymore so then we&#8217;re going to pop and put that value into b so um you know what&#8217;s</p>



<p>at the very top it&#8217;s a four so that means we&#8217;re going to actually remove the four and stick that</p>



<p>into b so i&#8217;m going to put four right here to remind ourselves that b is now equal to four</p>



<p>and uh for the purposes of this video you can just imagine that this you know the top of the stack is</p>



<p>of the stack is just totally gone deallocated depending on what implementation you use you</p>



<p>might want to think about crossing it out maybe there&#8217;s like some junk data there now but for the</p>



<p>purposes of this video in this diagram we&#8217;re just saying it&#8217;s gone so it&#8217;s just totally gone</p>



<p>okay then let&#8217;s do another pop so we&#8217;re going to pop and that&#8217;s going to become c so obviously the</p>



<p>top is uh is now the three so that means the three is going to go into the c variable so here&#8217;s a</p>



<p>c variable so here&#8217;s a three right there then the three gets deallocated</p>



<p>and now the top is the two so then we&#8217;ll push a 15 and we&#8217;ll push a 25 so again we just put those</p>



<p>new items on top of the stack even if there was stuff uh there before it&#8217;s gone now so we&#8217;re just</p>



<p>be a 15 and a 25 on top of the stack so we got this and then 25 and then we&#8217;re done adding our</p>



<p>stuff so uh the other functions that we talked about are empty and size uh at the very beginning</p>



<p>when we had just an empty stack like that if we were to call on the empty function it would tell</p>



<p>us true the stack is empty and then every step after that where we had some data in the stack</p>



<p>data in the stack like as soon as we even added that eight at the very beginning from that point</p>



<p>forward then empty would have returned false to us saying no the stack is uh it&#8217;s not empty</p>



<p>the size would have changed uh during every step of the way so you know at line 10 the size would</p>



<p>have been one after we&#8217;re finished with line 10 after line 11 the size would have been 2. 12</p>



<p>wouldn&#8217;t have changed the size so it still would have been 2. 13 would have upgraded the size to</p>



<p>have upgraded the size to four once we popped after that pop was finished it would have been</p>



<p>three again then the second pop would have brought it back down to two and then those two pushes</p>



<p>would have put it back up to three and then four and so then the final size whoops that was horrible</p>



<p>the final size of the stack is four and you can tell that there&#8217;s just four items</p>



<p>another way to double check yourself if you&#8217;re kind of trying to trace this on your own is just</p>



<p>count the number of pushes and subtract the number of pops so how many pushes do we have one two three</p>



<p>2, 3, 4, 5, 6.</p>



<p>So that&#8217;s like 6 minus however many pops we saw.</p>



<p>We see 2 pops.</p>



<p>So 6 minus 2 is equal to 4.</p>



<p>So, you know, when you&#8217;re writing this sort of thing down on,</p>



<p>like as a diagram to practice,</p>



<p>then you know it&#8217;s a 4 just by looking.</p>



<p>But what if you did something wrong?</p>



<p>It&#8217;s a great idea to double check yourself at all times.</p>



<p>So 6 minus 2 is 4.</p>



<p>So we know that the size of the stack is 4.</p>



<p>Looks pretty good.</p>



<p>Looks pretty good and we know the values of A, B, and C and you know one of those was</p>



<p>top two of those were pops.</p>



<p>And yeah, okay.</p>



<p>So notice something though that&#8217;s kind of peculiar about the data that came out.</p>



<p>If we added the data, like I guess while we were adding the data, we added first an eight</p>



<p>and then a two and then a three and then a four.</p>



<p>I&#8217;m just looking at the pushes right now and then a 15 and then a 25, right?</p>



<p>So we added the data in that order.</p>



<p>order. But then when we grab the data out, let me put this in, but maybe like a red bracket</p>



<p>or something. When we grab data out of it, the pops and the tops gave us two and then</p>



<p>a, okay, nevermind. That was not a pop. It gave us a four and then a three if we&#8217;re just</p>



<p>look at the, look at the first two items. Sorry, sorry, sorry. Look at the last two items right</p>



<p>before, and I didn&#8217;t draw this very well. Imagine that we had just pushed the eight and the two and</p>



<p>the three and the four only forget about these other pieces of data. So at this point, when we</p>



<p>start calling our pops, we have only an eight, two, three, four stack, right? So the data that</p>



<p>has a four and then a three if we&#8217;re removing data from the stack.</p>



<p>So notice how the four and the three are backwards.</p>



<p>Notice also like if we kept popping out data, you know, we got the four and then we got</p>



<p>the three, the next, what&#8217;s the next thing that would come out?</p>



<p>It would be the 25, right?</p>



<p>And then it would be the 15.</p>



<p>And then it would be the two.</p>



<p>And then it would be the eight.</p>



<p>So if you really think about it, the stack is reversing our input data.</p>



<p>We added a three and then a four, but we received a four and then a three.</p>



<p>And of course, it&#8217;s a little bit muddled because we have some pushes in between some tops and pops and stuff.</p>



<p>But just looking at what goes into the stack, it&#8217;s backwards, right?</p>



<p>Eight, two, 15, 25.</p>



<p>Let me finish this push sequence up here.</p>



<p>So we added three and four, and then we added a 15.</p>



<p>And then, oh, my penmanship, dude.</p>



<p>shit dude 15 and then we added a 25 so if you look at the data that comes out this is like a 15 and a</p>



<p>25 backwards and then the three and the four are backwards and then the two and the eight are also</p>



<p>backwards and so one thing that a stack does is it reverses data let me give you a cleaner example</p>



<p>with no pops in between the pushes just to show you what we&#8217;re talking about a little bit more</p>



<p>bit more clearly okay so i&#8217;m going to do maybe like another code page here whoops do that and</p>



<p>then i&#8217;m going to say got our stack and we&#8217;re just going to push in some data</p>



<p>and then we&#8217;ll just say s.pop and maybe i&#8217;ll just do that however many times</p>



<p>six two three four five six so now just a very quick trace because this is basically the same</p>



<p>intermixed. So I&#8217;m going to do like the bottom of the stack here. And then, well, I mean like an</p>



<p>empty stack. That&#8217;s not really a bottom of a stack. So we&#8217;re going to push an eight.</p>



<p>And then we&#8217;re going to push a two. And then we&#8217;re going to push a three. And then we&#8217;re</p>



<p>going to push a four. Then a 15. Got to work on my fives, dude. Okay. And then 25.</p>



<p>Okay, one more time.</p>



<p>Oh yeah, I guess I got to slow down.</p>



<p>So what&#8217;s going to come out?</p>



<p>Remember the top is the only place that will give us data.</p>



<p>So if we start popping all these one by one, we&#8217;re going to end up with a 25 for that first</p>



<p>pop.</p>



<p>Maybe I should write it down over here.</p>



<p>Do like a 25 and maybe I&#8217;ll add some line breaks in the code here so that it&#8217;s easier</p>



<p>for me to write with a pen.</p>



<p>Do this and that.</p>



<p>Okay, so the 25, I&#8217;m just going to do a red line through it to designate or to denote</p>



<p>deallocation or just like gone-ness.</p>



<p>Then the top is at the 15.</p>



<p>We do another pop.</p>



<p>It&#8217;s going to be 15.</p>



<p>Oh, I did my first perfect five today.</p>



<p>And then the top of the stack is now at the four.</p>



<p>So then the four is going to pop out next.</p>



<p>And then the three is going to pop out next.</p>



<p>The top is now at the three.</p>



<p>the three so the three is coming out for that pop we deallocate the three the top goes down one and</p>



<p>then we pop it becomes the two that&#8217;s deallocated now and then we grab the eight because that&#8217;s</p>



<p>where the top is so now we&#8217;ve grabbed all of our data out and I just want you to look at this one</p>



<p>more time notice the data is eight that the data that went in was eight two three four fifteen twenty</p>



<p>The data that came out was backwards 25, 15, 4, 3, 2, 8 or 8, 2, 3, 4, 15, 25.</p>



<p>If you kind of like look up.</p>



<p>So a stack is a data reverser.</p>



<p>A stack is also known as something called a, let&#8217;s see,</p>



<p>last in first out data structure, LIFO,</p>



<p>or first in last out, phylo data structure.</p>



<p>What that basically means is just it&#8217;s reversing the data or priority goes to the item that is the youngest.</p>



<p>So if you looked at the stack at the point where we were about to remove the 25,</p>



<p>25 was the most recently added or the youngest item or the item with, I guess, the timestamp, the furthest in the future,</p>



<p>however you want to look at it.</p>



<p>And that&#8217;s the item that came out.</p>



<p>So if you think about it, it was the last item in.</p>



<p>And therefore, when we did another pop, it was the first item out.</p>



<p>So LIFO or PHILO.</p>



<p>and uh you know this eight down here that was obviously the last item that we were able to grab</p>



<p>out of it so when we said you know hop our final pop before the stack became empty uh the eight was</p>



<p>definitely the first item in so first in was the last out if you really want to you can kind of like</p>



<p>jumble these uh these acronyms uh i think most like people just use lipo or phylo</p>



<p>life if you say something to me that I understand I think it&#8217;s probably fine</p>



<p>but it&#8217;s it&#8217;s non-standard I like to say you know foley first out last in that</p>



<p>definitely makes sense it&#8217;s just not very common another thing that I like to</p>



<p>do for fun is also I&#8217;ll mix up the phylo and I&#8217;ll say lo-fi because last out is</p>



<p>first in and now I&#8217;m cooler than other computer people because I use the word</p>



<p>lo-fi I&#8217;ve now got like a giant mustache and like a little like swap meet shirt</p>



<p>like swap meat shirt and I have craft beers and I have work boots on and I&#8217;d</p>



<p>roll up my jeans and my, my, my, my, my sleeves are rolled up and all that stuff.</p>



<p>You know what I&#8217;m saying?</p>



<p>I&#8217;m cooler than everybody else now because I used loaf pie anyway.</p>



<p>So we used integers.</p>



<p>It&#8217;s just important to understand that in modern coding,</p>



<p>I think I might&#8217;ve mentioned this before with the T&#8217;s,</p>



<p>these stacks are templated data structures,</p>



<p>we can hold another data type besides integers as long as we declare what the stack will hold in</p>



<p>advance. So instead of integers, you could imagine there&#8217;s like a custom class, like my class,</p>



<p>every single, you know, item in the stack is actually a full instance of a my class object,</p>



<p>or, you know, floats or strings or just, you know, any data structure you want,</p>



<p>you could put another data structure inside of a stack. People do that sometimes just for fun,</p>



<p>sometimes just for fun you could take a vector and stick it inside of a stack a list put it inside</p>



<p>of a stack you know whatever you want to do just to show that you understand everything</p>



<p>um so again by the way the stack is empty if we tried to pop from it at this point</p>



<p>this would be called a stack underflow we would probably want to throw an exception at the user</p>



<p>or just say we&#8217;re not allowed to do it let&#8217;s see um so i guess now i should tell you some</p>



<p>common uses for stacks i&#8217;m looking at my notes right here uh obviously our stack has reversed</p>



<p>has reversed the data. So stacks are kind of good for data reversing. Imagine if you had like a word</p>



<p>and you wanted to detect if the word was a palindrome or not, you would just like put all</p>



<p>the letters of the word into your stack and then grab them back out and see if the word was still</p>



<p>in the same order. If it was, then your word was probably a palindrome. If you don&#8217;t remember what</p>



<p>a palindrome is, I&#8217;m going to write down the word radar in reverse from backwards, from back to</p>



<p>front, right? It&#8217;s the same word. So that&#8217;s a palindrome. You could also use stacks kind of</p>



<p>as a trail of breadcrumbs. Like you could imagine that early implementations of browsers</p>



<p>had your browsing history in a stack somewhere so that every time you visited a webpage, then</p>



<p>some kind of block of information, maybe like a custom class instance was put onto a stack</p>



<p>somewhere and it contained information about like when you visited the webpage, what the URL was,</p>



<p>it is that the browser wants to remember and then later when you hit the back button then you can</p>



<p>imagine the browser is popping from the stack in order to go backwards in your history or your</p>



<p>spotify playlist or whatever it is you&#8217;re doing honestly you know these systems are much more</p>



<p>advanced than just using you know vanilla stacks now but you could imagine doing something on your</p>



<p>own for fun or just what it might have been like in the very beginning i&#8217;m going to erase fully</p>



<p>because that&#8217;s just that&#8217;s just that&#8217;s just cringe lo-fi i think is way cooler um so trail of bread</p>



<p>So a trail of breadcrumbs, like browser history, you could also imagine undo history, like</p>



<p>if you had a, I don&#8217;t know, like a text editor of some sort or like a image editor of some</p>



<p>sort, every major change that you did to it, maybe the program under the hood is adding</p>



<p>your change to the stack.</p>



<p>And that makes it easier to sort of reverse your changes as those changes are popped.</p>



<p>And there&#8217;s like a sort of mathematical uses like balancing parentheses and things, but</p>



<p>balancing parentheses and things, but I&#8217;m not going to talk about that too much.</p>



<p>And one of the most important uses for stacks inside of your computer is actually the call</p>



<p>stack. I&#8217;m going to talk about that in a future video. But again, remember each, you know, little</p>



<p>item on the stack, it could be a different data structure on its own. It doesn&#8217;t have to be an</p>



<p>integer. So imagine that I bundled up a bunch of information about a function call, and I called it</p>



<p>I just stuck a call frame on the stack.</p>



<p>That&#8217;s basically the call stack.</p>



<p>But that&#8217;s for another video.</p>



<p>Anyway, so let&#8217;s see.</p>



<p>Is there anything else that I wanted to show you?</p>



<p>I think we&#8217;re actually done just talking about the basics of stacks.</p>



<p>I&#8217;m not talking about time complexities or anything like that in this video.</p>



<p>Check a future video if you&#8217;re interested in time complexities per certain implementations</p>



<p>and so forth.</p>



<p>But for now, this is how a stack works.</p>



<p>or LIFO or LOFI data structure.</p>



<p>Nobody&#8217;s going to say LOFI but me, just FYI.</p>



<p>But maybe everybody will start doing it now.</p>



<p>I don&#8217;t know.</p>



<p>If it becomes a trend, okay?</p>



<p>You heard it here first.</p>



<p>Give everybody this link.</p>



<p>All right, I hope you feel like an expert on stacks</p>



<p>at this point.</p>



<p>I hope you had a little bit of fun</p>



<p>and learned a little bit of something, some stuffs.</p>



<p>I&#8217;ll see you in the next video.</p>



<p>hey everybody thanks for watching this video again from the bottom of my heart I really</p>



<p>appreciate it I do hope you did learn something and have some fun if you could do me a please a</p>



<p>small little favor could you please subscribe and follow this channel or these videos or whatever it</p>



<p>is you do on the current social media website that you&#8217;re looking at right now it would really mean</p>



<p>the world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



<p>videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general. So please</p>



<p>do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night</p>



<p>and I just wake up because I know somebody subscribed or followed. It just wakes me up</p>



<p>and I get filled with joy. That&#8217;s exactly what happens every single time. So you could do it as</p>



<p>a nice favor to me or you could you could troll me if you want to just wake me up in the middle</p>



<p>of the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p>Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website</p>



<p>which I think is also named somewhere at the bottom of this video and</p>



<p>It&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p>I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p>If you have a suggestion for</p>



<p>Clarifications or errata or just future videos that you want to see</p>



<p>please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know just send</p>



<p>me a comment whatever i also wake up for those in the middle of the night i get i wake up in a cold</p>



<p>sweat and i&#8217;m like it would really it really mean the world to me i would really appreciate it so</p>



<p>again thank you so much for watching this video and um enjoy the cool music</p>



<p>as as i fade into the darkness which is coming for us all</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/">Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master Two&#8217;s Complement: Convert &#038; Subtract Negative Binary Numbers</title>
		<link>https://www.NeuralLantern.com/master-twos-complement-convert-subtract-negative-binary-numbers/</link>
					<comments>https://www.NeuralLantern.com/master-twos-complement-convert-subtract-negative-binary-numbers/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 18 May 2025 22:53:54 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Conversions]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[binary conversion]]></category>
		<category><![CDATA[binary math]]></category>
		<category><![CDATA[binary numbers]]></category>
		<category><![CDATA[binary subtraction]]></category>
		<category><![CDATA[coding for beginners]]></category>
		<category><![CDATA[computer science basics]]></category>
		<category><![CDATA[negative numbers binary]]></category>
		<category><![CDATA[programming tutorials]]></category>
		<category><![CDATA[signed integers]]></category>
		<category><![CDATA[two's complement]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=110</guid>

					<description><![CDATA[<p>Learn two's complement to represent negative binary numbers with easy examples. Master sign bits &#038; subtraction. Subscribe!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-twos-complement-convert-subtract-negative-binary-numbers/">Master Two&#8217;s Complement: Convert &amp; Subtract Negative Binary Numbers</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Master Two&#039;s Complement: Convert &amp; Subtract Negative Binary Numbers" width="1380" height="776" src="https://www.youtube.com/embed/i2tfzMqlewQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Want to conquer negative numbers in binary? This fun, beginner-friendly tutorial breaks down two&#8217;s complement with step-by-step examples (-109, -29) and shows how to convert, pad, and subtract in binary. Learn sign bits, carry bits, and avoid common mistakes. Subscribe for more coding tips and tech tutorials that make learning a blast! Scan the QR code for more resources and join our community!</p>



<p>Introduction to Two&#8217;s Complement 00:00:00<br>Signed vs. Unsigned Integers 00:00:28<br>Sign Bit Explanation 00:01:55<br>Positive and Negative Representation 00:02:06<br>Range of Signed Integers 00:02:48<br>Padding Signed Integers 00:05:36<br>Converting to Negative (Example: -109) 00:07:00<br>Binary Addition and Carry Bits 00:10:16<br>Correcting Conversion Mistakes 00:16:38<br>Converting Negative 29 00:13:32<br>Subtraction Using Two&#8217;s Complement 00:18:21<br>Adding Binary Numbers (109 &#8211; 29) 00:20:41<br>Verifying Results 00:23:56<br>Conclusion and Call to Action 00:25:40</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>Hey there! Let&#8217;s talk about representing negative numbers in binary.</p>



<p>We&#8217;re going to use a system called 2&#8217;s complement which is going to allow us to represent signed integers,</p>



<p>which means there will be a positive sign or a negative sign on the integer</p>



<p>and we&#8217;ll still be able to do it in pure binary.</p>



<p>So let me talk a little bit about what I mean first here.</p>



<p>signed and unsigned okay so if i just type the number 14 by itself that is uh that&#8217;s an unsigned</p>



<p>integer we don&#8217;t really know if it&#8217;s positive or negative like do we know that it&#8217;s negative 14 or</p>



<p>do we know that it&#8217;s positive 14 right so the sign is what lets us know if something is positive or</p>



<p>negative okay so if i if i type a number in binary let&#8217;s just do a pure binary number one two three</p>



<p>if you know how to convert binary to decimal you probably recognize right away that this is just</p>



<p>the number zero even if we add a couple of bits here that&#8217;s just like the number three</p>



<p>and so this is the number three but so far if if you haven&#8217;t learned signed integers in twos</p>



<p>complement or in binary then you don&#8217;t really know that there&#8217;s a sign you just kind of assume</p>



<p>that the number is positive by default if you&#8217;re not using twos complement and you&#8217;re just saying</p>



<p>and you&#8217;re just saying like well let&#8217;s just do a bunch of binary you know digits then yeah it&#8217;s a</p>



<p>safe assumption that the the sign is positive but we&#8217;ll use two&#8217;s complement which starts with the</p>



<p>idea that the highest bit the leftmost bit the bit with the height you know the most power that&#8217;s</p>



<p>going to turn into the sign that&#8217;s that&#8217;s going to turn into the plus or the minus so like you</p>



<p>the sign.</p>



<p>So this means that we have to decide, you know, does a zero mean positive or does it</p>



<p>mean negative or what means what?</p>



<p>So in two&#8217;s complement, we&#8217;ll say that zero is positive and one is negative.</p>



<p>So you can see right here that we&#8217;re still actually representing the number three, but</p>



<p>it&#8217;s positive three for sure because this sign bit right here is zero.</p>



<p>That&#8217;s positive.</p>



<p>Once we decide to represent two&#8217;s complement, then we can say that for sure.</p>



<p>On the other hand, if we put a one here,</p>



<p>then we definitely know that this number is now negative.</p>



<p>However, we don&#8217;t actually know that it&#8217;s a three</p>



<p>because the numbers don&#8217;t work out the same anymore</p>



<p>in two&#8217;s complement.</p>



<p>So the positive numbers, they will mostly look the same.</p>



<p>The negative numbers, they&#8217;ll look a lot different,</p>



<p>but they&#8217;ll still be valid in order,</p>



<p>you know, in terms of us being able to add them together,</p>



<p>subtract them from each other and things like that.</p>



<p>So two&#8217;s complement is pretty cool.</p>



<p>Let&#8217;s do, let&#8217;s see, what else can I tell you?</p>



<p>Oh, one thing to understand is that</p>



<p>One thing to understand is that in an 8-bit unsigned integer in binary, let&#8217;s say 1, 2,</p>



<p>3, 4, 5, 6, 7, 8, let&#8217;s say unsigned bits, maybe I&#8217;ll just put like, maybe just some</p>



<p>like V for value, all the bits are values, that means the range is 0 to 255 with 256</p>



<p>total combinations.</p>



<p>But if we want to use a signed number, then we&#8217;ll actually have the signed bit be the first bit,</p>



<p>and all the rest will be value bits.</p>



<p>1, 2, 3, 4, 5, 6, 7.</p>



<p>In this case, the range kind of goes down.</p>



<p>Because if you think about it, every single bit, you know, represents like, you know,</p>



<p>it helps towards the highest possible value that you can represent.</p>



<p>If we&#8217;re using the leftmost bit, then for an 8-bit integer, that&#8217;s 128.</p>



<p>integer that&#8217;s 128 that&#8217;s a value of 128 so we lose a lot off of the maximum</p>



<p>integer that we can represent so the range here is I think it&#8217;s negative 20</p>



<p>128 to positive 127 just keep in mind you&#8217;ll have to trust me in terms of why</p>



<p>is the negative 128 bigger and the positive 127 you know smaller but that&#8217;s</p>



<p>just the way it is so that means we only get these value bits here and if you if</p>



<p>that um let&#8217;s see if i can do this quickly enough without screwing it up we&#8217;ll say 127 minus um</p>



<p>minus negative 128 i guess that&#8217;s 255 and then also a zero but uh in terms of positive and</p>



<p>negative the way this is going to work out is the zero will probably show up twice because uh</p>



<p>255 possible combinations with seven bits.</p>



<p>Normally you would have a range from negative, sorry, from zero to positive 255.</p>



<p>And in this case, we&#8217;re just, you know, losing our range, but we can represent negative numbers.</p>



<p>Anyway. Okay.</p>



<p>So let&#8217;s talk about doing an example real fast. Let&#8217;s see.</p>



<p>I&#8217;ve got like a little notes to tell me what I should do. Okay.</p>



<p>It&#8217;s important to understand that normally when you have, let&#8217;s say, let&#8217;s say you have</p>



<p>like an 8-bit number and you go 1, 2, 3, 4, 5, 6, 7, 8.</p>



<p>And then you have 2 bits right there and so this is like, you know, positive 3.</p>



<p>If you wanted to copy that number into more bytes, like for example, if you wanted to</p>



<p>take a 2-byte integer and have it copy the value of a 1-byte integer, then it&#8217;s pretty</p>



<p>easy.</p>



<p>zeros to the left you&#8217;ll say one two three four five six seven eight that would work perfectly</p>



<p>however if you did this with a negative number let&#8217;s say that we have a negative number</p>



<p>I&#8217;m going to put some of the random patterns so that you don&#8217;t think it&#8217;s the three let&#8217;s see</p>



<p>one two three four five six seven eight that&#8217;s eight total so I&#8217;m going to make that a one and</p>



<p>then get rid of that so we have like eight negative number if we were going to copy that</p>



<p>integer then we would copy paste it to start but then you&#8217;d have to pad with</p>



<p>ones to the left one two three four five six seven eight same negative number</p>



<p>more bits so be very careful about how you pad if you&#8217;re padding an unsigned</p>



<p>integer then yeah you&#8217;ll always pad zeros to the left no matter what is</p>



<p>happening but if you&#8217;re padding a signed integer then you have to pad differently</p>



<p>signed integer you have to pad with whatever the highest bit is so in this case the highest bit was</p>



<p>a zero so we do pad with zeros but then in this case the highest bit was a one so we have to pad</p>



<p>with ones if you don&#8217;t do that you&#8217;re going to end up with a number that doesn&#8217;t actually make</p>



<p>sense okay so now let&#8217;s work on actually converting a number to a negative number or representing a</p>



<p>negative number in twos compliment okay so i&#8217;m going to write twos compliment here and then uh</p>



<p>Let&#8217;s start off with the number negative 109.</p>



<p>Okay, how do we do this?</p>



<p>The first thing is convert it to its positive form.</p>



<p>Take the absolute value. Okay.</p>



<p>So we&#8217;re really just, you know, take positive form here,</p>



<p>and that&#8217;s just going to be positive 109.</p>



<p>Okay, no problem.</p>



<p>I&#8217;ll say start with negative 109, and then we&#8217;ll take the positive form 109.</p>



<p>Now we&#8217;ll convert it to binary.</p>



<p>Okay, this is not a video that teaches you how to convert to binary, so I&#8217;m just going</p>



<p>to try to do this in my head real fast here.</p>



<p>It&#8217;s going to be, let&#8217;s see, we got 8 bits, we&#8217;ll use just 8 bits to store the number,</p>



<p>and because it&#8217;s low enough and I don&#8217;t want to use that many bits, so it&#8217;s odd, so I can</p>



<p>add a 1 there, but maybe for now I should find the highest bit that is less than the</p>



<p>actual number.</p>



<p>So this is the 128 bit right here. So that&#8217;s going to be a zero. This is the 64 bits</p>



<p>I&#8217;m going to put a one there and I&#8217;m just going to say</p>



<p>Mmm, maybe like</p>



<p>This is like maybe not the smartest way to do it. I&#8217;ll say 109 minus 64</p>



<p>Because I put a one there and then 45 so now this is 32 that&#8217;s less than 45. So I&#8217;ll put a one there and</p>



<p>Then it&#8217;s going to be minus 40</p>



<p>So it goes from 45 to 13.</p>



<p>So then that was the 32.</p>



<p>Okay, so 128, 64, 32, and then 16.</p>



<p>Is 16 less than 13?</p>



<p>No, it&#8217;s not.</p>



<p>So we&#8217;ll put a zero here.</p>



<p>And then the next one is going to be just four.</p>



<p>Four is definitely less.</p>



<p>So I&#8217;ll put a one bit there.</p>



<p>And then I&#8217;ll subtract four.</p>



<p>And then one, two, four.</p>



<p>Oh, wait a minute.</p>



<p>One, two, four, eight.</p>



<p>wait a minute one two four eight sorry that was supposed to be subtracted uh subtracting eight</p>



<p>because that was the eight bit uh then i want to get five so here is the four bit so i put a one</p>



<p>there and then i&#8217;ll just put a one here because four plus five is equal to uh i&#8217;m sorry four plus</p>



<p>one is equal to five so let&#8217;s see um it is let me just double check here one</p>



<p>one let me just double check my conversion real fast it&#8217;s going to be</p>



<p>the one bit plus two four plus eight six thirty two plus thirty two plus sixty</p>



<p>four did I get one on nine yeah okay so I guess I did it right so convert to</p>



<p>binary and that&#8217;s going to be this the next thing we&#8217;ll do is we will invert the</p>



<p>bits I made a little edit jump here because I inverted the bits incorrectly</p>



<p>on the first try which is sad but hey it happens the next thing we&#8217;ll do is</p>



<p>but hey it happens the next thing we&#8217;ll do is we&#8217;ll invert the bits so basically you know we</p>



<p>take this original sequence here and i&#8217;ll just turn every single bit uh i&#8217;ll flip it i&#8217;ll flip</p>



<p>ones to zeros and zeros to ones so i&#8217;m going to go one zero zero one zero zero one zero okay so now</p>



<p>we have this number invert the bits then we just have to add one so we&#8217;ll add positive one to that</p>



<p>that it&#8217;s just going to be a one there at the end.</p>



<p>But sometimes that might not be the case</p>



<p>because what if we already had a one there</p>



<p>and we had to add one to that?</p>



<p>Well, we&#8217;d add one to the right side</p>



<p>and it would turn into a zero</p>



<p>and then it would carry a bit to the left.</p>



<p>That would become a zero</p>



<p>and then the carry bit would show up all the way over there.</p>



<p>So I just want you to be aware of the fact that</p>



<p>when you add two binary numbers together,</p>



<p>you have to be careful.</p>



<p>You have to add them the same way you would add decimal numbers.</p>



<p>For each digit&#8217;s position,</p>



<p>you have to add the two numbers together and then if they overflow then you just</p>



<p>kind of wrap around to the lowest number again subtracting you know the highest</p>



<p>value or sorry subtracting the base like in decimal if you add 9 and 9 the answer</p>



<p>is 18 but you&#8217;re not going to write 18 in that one position you&#8217;re going to</p>



<p>subtract the base which is 10 so it&#8217;ll actually be 8 and then you&#8217;ll carry the</p>



<p>plus nine is equal to eight carry the one right so we&#8217;ll do the same thing in binary we&#8217;ll say</p>



<p>if we ever get a one plus one when we&#8217;re adding the answer will be two but then we subtract the</p>



<p>base which is two so the answer is actually zero carry a one bit keep that in mind i&#8217;m just gonna</p>



<p>write it all out for you so you can kind of get a little bit of practice it&#8217;s important to to</p>



<p>practice this because it&#8217;s easy to get wrong i&#8217;m going to put a bunch of dashes up at the top to</p>



<p>and I&#8217;m going to say we&#8217;re going to add, you know, one number plus another number,</p>



<p>put a little plus symbol over there just to try and make sure we do it the right way.</p>



<p>Okay, so how do we add these?</p>



<p>Well, we&#8217;ll just go to the right side.</p>



<p>Zero plus one is one, no carry bit.</p>



<p>One plus zero is one, no carry bit.</p>



<p>Then we got a couple zeros here, no carry bit, of course.</p>



<p>One plus zero is one, no carry bit.</p>



<p>And then a couple zeros and then another one with no carry bit.</p>



<p>Okay, so, you know, we could have done that pretty easily, but</p>



<p>but well now we&#8217;re getting a taste for binary addition that might be harder later.</p>



<p>So we&#8217;ll just do that for now.</p>



<p>And now this is the two&#8217;s complement representation of negative 109.</p>



<p>I&#8217;ll say now we have negative 109 is equal to that.</p>



<p>leftmost number the most powerful sorry leftmost bit the most powerful bit is a one remember one</p>



<p>always indicates negative so when you&#8217;re looking at it if your leftmost bit turns out to be a zero</p>



<p>then you probably did something wrong or you had an overflow maybe um and again if we were going</p>



<p>to try to you know send this number into a two byte number or an eight byte number or whatever</p>



<p>uh then we would just have to pad with the sign bit so one two three four five six seven eight</p>



<p>help my brain so this was this is the way it would look in a two-byte number this is the way it would</p>



<p>look in a three-byte a four-byte and five six seven how many one two three four five six seven</p>



<p>okay one more this is what it would look like as an eight-byte number or a quad word 64-bit number</p>



<p>yeah okay so now we know how to do negative 109 okay so now let&#8217;s do a number that&#8217;s a little bit</p>



<p>negative 29. So let&#8217;s say convert, or how about represent negative 29 into</p>



<p>2&#8217;s complement. Okay. So first we, you know, first get the absolute value.</p>



<p>So just 29. And then we have to invert the, sorry, we have to get that into binary. So</p>



<p>next convert to binary. And I&#8217;m going to start with zeros, one, two, three, four, five, six,</p>



<p>1, 2, 3, 4, 5, 6, 7, 8.</p>



<p>For this video, remember, we&#8217;re choosing to use one byte integers.</p>



<p>But if you wanted to do a bigger one or you had to do a bigger one,</p>



<p>then just, you know, keep that in mind.</p>



<p>Okay, so 128 is not smaller than 29.</p>



<p>64 is not.</p>



<p>32 is not.</p>



<p>16 is, though.</p>



<p>So I&#8217;ll put a 16 bit there.</p>



<p>And I&#8217;ll just subtract 16 from 29.</p>



<p>29 minus 16.</p>



<p>Now we&#8217;ve got 13 left.</p>



<p>So 64, 32, 16.</p>



<p>six four thirty two sixteen eight okay so now I&#8217;m gonna put a one bit there and</p>



<p>I&#8217;m gonna subtract 8 from the remainder and then we got a five which is pretty</p>



<p>easy to do eight four and then a one so now we have well zero zero zero one one</p>



<p>one zero one that&#8217;s the binary number the positive or unsigned representation</p>



<p>representation. So now we&#8217;ll add one. Positive one. And let&#8217;s try to do this the right way so</p>



<p>that we can practice carry bits with addition a little bit. Notice how this one is already there</p>



<p>on the right side. So it&#8217;s going to, we&#8217;re going to have at least one carry bit for sure.</p>



<p>So then I&#8217;m going to go doop like that. And then I&#8217;m going to say that we have like, you know,</p>



<p>what is the result? Put a positive sign there. And then I&#8217;m going to put a bunch of dashes</p>



<p>my carry bits because I can I can forget that pretty easily. So the first thing is we add one</p>



<p>and one. The answer is two, but we can&#8217;t put the number two here because it&#8217;s binary. Instead we</p>



<p>need to subtract the base which is two. So two minus two is equal to zero, but then we have a</p>



<p>carry bit of one. So I&#8217;m going to put a one there. The first carry bit will stay as a dash for this</p>



<p>whole you know exercise because you&#8217;re not going to carry on to the first digit. So now we have</p>



<p>what would have been just zero plus zero now we have one plus zero plus zero so</p>



<p>that means this is going to be one and then the carry bit is just going to be</p>



<p>zero because we don&#8217;t actually carry anything so then we have zero plus one</p>



<p>plus zero so that&#8217;s going to be a one and then zero carry bit and then zero</p>



<p>one zero is just going to be one and then there&#8217;s going to be no carry bit</p>



<p>and then zero one zero again a one no carry bit because we didn&#8217;t actually</p>



<p>zero zero zero and then I&#8217;m just gonna put zeros here okay so now we have</p>



<p>successfully added I blew it totally blew it I always forget steps don&#8217;t</p>



<p>forget the steps this is a good lesson I&#8217;m gonna leave this in the video</p>



<p>because I want you to see that everybody makes mistakes and you got to practice</p>



<p>practice practice especially before you have to actually do this in real life or</p>



<p>or something like that.</p>



<p>Next, convert to binary.</p>



<p>Before you add one, I&#8217;m gonna just remove this.</p>



<p>Oh my gosh.</p>



<p>Next, flip the bits,</p>



<p>which is gonna be 1110010.</p>



<p>Okay.</p>



<p>So now we take this bit flipped number</p>



<p>and we will add one.</p>



<p>So that&#8217;s gonna be zero, zero, zero, zero, zero, zero, zero,</p>



<p>one, oh, it&#8217;s too easy.</p>



<p>Maybe I got excited and I thought,</p>



<p>excited and I thought, oh, it&#8217;s carry bit time.</p>



<p>But even though the last edition that I did was wrong because I forgot to carry</p>



<p>the or flip the bits, you still at least saw a little bit about how to carry the bits.</p>



<p>Right. OK, so it&#8217;s just going to be one.</p>



<p>Let me start from the right side.</p>



<p>One one zero zero zero one one one one one one.</p>



<p>Let me just double check here.</p>



<p>One one one zero zero zero.</p>



<p>OK, so I got that.</p>



<p>Now we have.</p>



<p>negative 29 in twos compliment. Again notice if we actually tried to add those numbers up to be</p>



<p>like an unsigned binary number they&#8217;re not really going to make sense because this is like 64 plus</p>



<p>32 plus 3. So what would that end up being? Let&#8217;s just double check here. 64 plus 32 plus 3, 99.</p>



<p>That&#8217;s not actually the number but the number is 29. So keep in mind you can&#8217;t just look at this</p>



<p>unless you&#8217;re like really, really practiced.</p>



<p>Okay, and again, notice that the number is 1 at the very left,</p>



<p>indicating that it&#8217;s a negative number.</p>



<p>Now, let&#8217;s look at how to subtract one number from another</p>



<p>using 2&#8217;s complement.</p>



<p>Okay, so what I want to do is I want to subtract,</p>



<p>let&#8217;s say, 29 from 109.</p>



<p>Okay, so let&#8217;s subtract, and I&#8217;ll just say 109 minus 29.</p>



<p>109 minus 29.</p>



<p>And how I would do that is basically I&#8217;ll start by just taking 109.</p>



<p>Let&#8217;s see, convert to binary.</p>



<p>So I&#8217;m just going to copy paste that number.</p>



<p>So 109 is this and then 29, take the positive version.</p>



<p>It is just, let&#8217;s see, before we flip the bits or anything,</p>



<p>let me make sure that I grab the right one.</p>



<p>Invert the bits, okay.</p>



<p>So positive 29 is this number.</p>



<p>Whoops.</p>



<p>is this number whoops let me put parentheses around that so it&#8217;s easy to tell and then I&#8217;ll</p>



<p>put a positive sign there like that maybe like that nope nope nope nope how about this okay</p>



<p>so now we have both of these numbers in positive form so now if we added 109 plus 29 that wouldn&#8217;t</p>



<p>negative positive 29 that would be what we wanted right because really if you&#8217;re subtracting</p>



<p>i&#8217;ll say aka</p>



<p>positive 29 plus negative 29 all we really need to do is um invert the 29 and then add the result</p>



<p>to 109 so that means we&#8217;ll we&#8217;ll turn positive 29 into negative 29 using two&#8217;s complement</p>



<p>through the steps again but basically put it there but basically you know that&#8217;s negative 29</p>



<p>so say positive 129 is equal to what i just put up here and then negative 29 is equal to</p>



<p>maybe i should do the parentheses again for clarity uh is equal to this okay so you can tell</p>



<p>that positive 29 is pretty pretty different from negative 29 but now we have</p>



<p>29 but now we have both of those numbers so let&#8217;s see 0 1 1 0 and then we&#8217;re ready to add okay all</p>



<p>we got to do is add them together next add them together maybe I should write the steps up here</p>



<p>negative 29 using twos complement and then next add them together so then I&#8217;m</p>



<p>going to copy paste the bits here and it&#8217;s going to be this plus this do a</p>



<p>positive plus sign just to remind ourselves that we are actually adding</p>



<p>and then I&#8217;m going to put a bunch of placeholders for sign bits up at the</p>



<p>top and now we&#8217;ll have a little bit more fun adding numbers together maybe I&#8217;ll</p>



<p>drag this down</p>



<p>Oh my god. Oh, there we go. Okay. So I&#8217;ll start with the one on the right, the position on the</p>



<p>right. That&#8217;s going to be one plus one equals two, but then that&#8217;s an overflow. So I&#8217;m going to</p>



<p>subtract the base. So it&#8217;s going to be zero. And then don&#8217;t forget to carry the one. Oh,</p>



<p>cool. More interesting. So we have one plus zero plus one. That&#8217;s going to be another two carry</p>



<p>two carry the one so it&#8217;s going to be zero and then carry the one again so i&#8217;m going to put the</p>



<p>carry bit up there and then again we have one plus one is equal to two so it&#8217;s going to be zero</p>



<p>carry the one again zero carry the one and then finally we don&#8217;t really have a carry bit</p>



<p>um so we&#8217;ll just have like a one and there&#8217;s there&#8217;s no carry so it&#8217;s going to be you know</p>



<p>carry a zero and then we add these two together so it&#8217;s going to be a zero carry the one and then</p>



<p>Now we have a three.</p>



<p>Oh, that&#8217;s kind of nice.</p>



<p>So this is an interesting edge case kind of.</p>



<p>One plus one plus one is three.</p>



<p>But if we subtract two, the base from it, you know, three minus two, it&#8217;s going to be one, not zero.</p>



<p>So it actually is going to be a one and then carry the one on top of that.</p>



<p>Then for here, let me space this over a little bit so that I can illustrate what&#8217;s going on a little bit better.</p>



<p>We&#8217;re going to have one plus zero plus one.</p>



<p>definitely going to be zero and then carry the one but there&#8217;s no bit where that carried one can</p>



<p>can show up on right so that one overflows it falls off the edge if this was a bigger number</p>



<p>then okay we you know if we had more bits to this number then sure we would just keep carrying over</p>



<p>over to the left but remember we said before that when we have a very big number let&#8217;s see</p>



<p>bits it&#8217;s just ones all the way to the side that will actually help us make sure that if our final</p>



<p>number is actually going to end up being positive that everything kind of like dominoes like carry</p>



<p>the one carry the one carry the one carry the one carry the one all the way until one of the ones</p>



<p>falls off think about it so anyway this one just is gone we don&#8217;t really care about it anymore</p>



<p>the result is going to be just only eight bits because that&#8217;s the number that we started with</p>



<p>bunch of zeros what is you know the final answer let&#8217;s just compute this</p>



<p>real fast to decimal so this is 128 and then 64 so it&#8217;s gonna be 64 plus not 32</p>



<p>but 16 so 64 plus 16 that&#8217;s gonna be oops 16 that&#8217;s gonna be 80 and now we</p>



<p>just have to ask ourselves again as like a final step to double check yourself to</p>



<p>what you&#8217;re doing and that you got it right is just punch up 109 minus 29 just to make sure</p>



<p>109 minus 29 whoops what happened here 109 minus</p>



<p>oh i think i stole my subtraction key for the annotator 109 minus 29 is 80</p>



<p>so again you know if you&#8217;re if you&#8217;re trying to like you know write something down to do some</p>



<p>you know taking an exam or something you definitely want to double check yourself in several ways</p>



<p>As you can tell from this video alone</p>



<p>I got one of these things wrong because I forgot to input the bits as a step before adding one</p>



<p>So you know your final step should be actually trying to add two numbers together or subtract numbers or whatever</p>



<p>You&#8217;re doing to make sure that you got the binary correct</p>



<p>So let&#8217;s see</p>



<p>Hmm</p>



<p>I guess maybe your first indication that the result was going to be positive would be that</p>



<p>there&#8217;s a zero there. And just, you know, as a sanity check, you look at the top and you&#8217;re like,</p>



<p>well, I was going to subtract a small number from a larger number. So the result should probably be</p>



<p>positive, right? Like 29 is like way lower than 109. So it should be positive, which means the</p>



<p>final result should have a zero at that leftmost position. Okay, so that&#8217;s two&#8217;s compliment,</p>



<p>two&#8217;s complement how to convert numbers from positive to negative in two&#8217;s complement you</p>



<p>know what the sign bit means and all that stuff and how to perform subtraction via two&#8217;s complement</p>



<p>i hope you enjoyed this video thank you for watching i hope you learned a little bit of</p>



<p>stuff and had a little bit of fun see you in the next video hey everybody thanks for watching this</p>



<p>video again from the bottom of my heart i really appreciate it i do hope you did learn something</p>



<p>If you could do me a please, a small little favor, could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now.</p>



<p>It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p>So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p>So please do me a kindness and subscribe.</p>



<p>in the middle of the night and I just wake up because I know somebody</p>



<p>subscribed or followed. It just wakes me up and I get filled with joy. That&#8217;s</p>



<p>exactly what happens every single time. So you could do it as a nice favor to me</p>



<p>or you could you could troll me if you want to just wake me up in the middle</p>



<p>of the night just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what</p>



<p>will happen. Also if you look at the middle of the screen right now you</p>



<p>should see a QR code which you can scan in order to go to the website which I</p>



<p>think is also named somewhere at the bottom of this video and it&#8217;ll take you</p>



<p>you to my main website where you can just kind of like see all the videos I published and the</p>



<p>services and tutorials and things that I offer and all that good stuff and if you have a suggestion</p>



<p>for clarifications or errata or just future videos that you want to see please leave a comment or if</p>



<p>you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever I also</p>



<p>wake up for those in the middle of the night I get I wake up in a cold sweat and I&#8217;m like</p>



<p>it would really mean the world to me. I would really appreciate it. So again,</p>



<p>thank you so much for watching this video and enjoy the cool music as I fade</p>



<p>into the darkness, which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-twos-complement-convert-subtract-negative-binary-numbers/">Master Two&#8217;s Complement: Convert &amp; Subtract Negative Binary Numbers</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-twos-complement-convert-subtract-negative-binary-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
