- 22 hours ago
In this hands-on tutorial, we dive into bit manipulation in x86-64 assembly using Yasm on Ubuntu. We cover the basics of logical operations like AND, OR, XOR, and NOT, then move into shifting (logical vs arithmetic), rotating bits, and why signed vs unsigned matters for shifts.
I explain each concept on paper first, then show real code examples running in GDB to prove how the bits change. We look at patterns in registers, see what happens with negative numbers in two's complement, and even touch on how shifts can multiply or divide by powers of 2.
Perfect if you're learning low-level programming, reverse engineering, or just curious about how bits work under the hood. No prior assembly knowledge needed, but it'll help if you've seen my other videos.
Like and subscribe for more assembly tutorials!
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
Please help support us!
- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
I explain each concept on paper first, then show real code examples running in GDB to prove how the bits change. We look at patterns in registers, see what happens with negative numbers in two's complement, and even touch on how shifts can multiply or divide by powers of 2.
Perfect if you're learning low-level programming, reverse engineering, or just curious about how bits work under the hood. No prior assembly knowledge needed, but it'll help if you've seen my other videos.
Like and subscribe for more assembly tutorials!
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
Please help support us!
- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
Category
🤖
TechTranscript
00:00Hey everybody! In this video we're going to talk all about manipulating bits in x8664 yasm assembly
00:07within ubuntu but if you're not using yasm or you're not on ubuntu this video is probably
00:12going to be pretty useful to you anyway because i'm going to talk about the basic concept of
00:15manipulating bits in various ways. We're going to talk about some basic logical operators like
00:25anding oring x oring and then we're going to talk about shifting the bits to the left and the right
00:31and then we're going to talk about the difference between uh shifting without any uh respect for
00:36whether the the number inside of the bits is an actual integer or whether we just consider them
00:41to just be bits by themselves um or whether we're actually you know respecting the bits as numbers
00:48and also we're going to look at the rotation instruction so first i'm going to explain this
00:53i'm going to write it down in a pad and then i'm going to show you a program that actually does
00:57it just so we can prove that uh it all works okay so for starters let me open up a little notepad here
01:05what the heck am i even talking about let me write this down because i forgot this on my first take i
01:09spent like five minutes uh blew right past the explanation of and and or and x or because i
01:15forgot that that was supposed to be part of this video we're going to look at and or and x or at
01:19least for now and then we're going to look at uh shifting and then we're going to look at uh rotating
01:26okay so first off what is ending i've explained that in another video but long story short and
01:32just requires that two bits uh that you're comparing against each other are both a one in order to
01:38receive a one and so any other pair like a zero versus a one or one versus a zero or two zeros
01:44uh are just going to be a zero so and means both bits that you're comparing have to be a one in
01:50order for the result to be a one um the or is pretty easy if either of the bits is a one then
01:57the result is going to be one so if i say one versus one uh the result is going to be one if i say
02:02you know zero versus one uh the result is going to be a one or if i say one versus zero the result is
02:08going to be a one you know the in the only case where you end up with a zero it's just going to be
02:12zero versus zero results in the zero to upgrade to exclusive or basically exactly one bit needs
02:20to be a one and if two or zero or a one then the result is a zero which means here when we have
02:26the two ones at the top the answer is going to be a zero these are the two stay the same and this
02:31one stays the same so that's XOR again I have another video where I explain this a little bit
02:35better but I'm just doing like a quick overview shifting is pretty interesting because let me do
02:42a pattern let's say zero one one zero suppose we just have four bits I could shift the bits to the
02:48left or to the right so imagine left if I shift them to the left then what I'm really doing is I'm
02:55just taking off that zero on the end and padding on the right with the zero and then if I'm shifting
03:01to the right then well it's to the right I'm just going to you know take off let's see the zero on
03:10the right side and pad with the zero on the left be careful though because what I've actually done
03:14is bitwise or logical shifting I haven't actually shifted with respect to the original number think
03:21about this that number right there on line three if we just consider that an unsigned integer or just
03:26bits then what I just did with the shifting is fine but what if that was a two's complement number
03:32that can represent a signed integer if it's two's complement or if it's just like a signed integer
03:37then the leftmost bit being a zero tells me that that's a positive number right but if I look at
03:43the result of my left shift there's a one now in that first position which tells me that's probably
03:49a negative number we could face a similar problem with shifting to the right I mean we didn't this
03:56time it just looks like it actually shifted and the answer is going to end up being right but what if
04:01we shifted something else right let's see what am I going to do here one one zero zero what if I
04:06shifted that number one to the right the answer would be zero you know one one zero right but then
04:14that's wrong because the original number was negative because there's a one in the first position
04:17but the result is a zero so if I'm not careful about what instructions I'm using and how to respect
04:23whether the number I'm shifting is actually considered an integer assigned integer or if it's just bits
04:30if I don't respect that then I could end up ruining the number so keep that in mind we're going to do
04:35several examples of that I'm not going to show you how to actually arithmetically shift by hand I'm
04:42just going to show you what the instructions in the assembler will do so then the other thing that
04:46we should talk about is rotating so imagine that I rotate this number one to the right well nothing
04:54really happens visually but what you're supposed to do if we rotate to the right you take the number on
05:00the very right side and you just pop it onto the front so it's sort of it doesn't fall off and get
05:04replaced by a zero or get replaced by a one depending on what you're doing it actually moves
05:08to the other side so if I shift this again maybe let's shift it one more time to the right I'm going
05:14to take that one and pop it off the right side and put it on the very front so see do you see how
05:18they're it's kind of rotating if I do this one more time actually let me copy paste this I'll take that
05:24number on the right side pop it off put it back on the front then if I do it again take that zero
05:29pop it off put it back on the front notice how it eventually gets back to the original pattern
05:33and I would have to rotate you know exactly four times to get that to happen
05:36same same thing applies for rotating to the left I'm not going to show you because it's it's the same
05:43thing as rotating to the right just in a different direction one thing that I forgot to say a moment
05:49ago when I was talking about shifting is think about this if we shift to the right pretend that
05:56we don't really care that much let's let me put some extra zeros here so that we don't have to
06:01carry too much we don't have to care too much about what's happening with positive or negative
06:05if you know how to convert to binary which I've explained in other videos then you'll know this
06:10number is let's see there's a one and a two and then these positions are four and eight so this
06:15should be a 12 right if I shift this to the right I should be dividing this by two because the base
06:24of binary is two so every time you you kind of like move to the left in terms of what digit you're
06:29looking at you're increasing the power of that digit by two every time you move to the right you're
06:33decreasing the power of the of the digit by two so that means if I shift the entire thing uh to the
06:40right I should be dividing the whole thing by two right not just one bit so if I just take a zero
06:46off of there and put it on there I've now shift all the bits shifted all the bits one to the right
06:51I should end up with the six let's just compute what it is there's a one there and there's a two
06:57there and there's a four there what's four plus two that's six so this is actually six in binary or
07:03the original number divided by two because we shift it to the right on the other hand if I take this
07:09number one more time and just kind of shift it to the left I should multiply it by two so that means
07:13I should expect to see a 24 let me just remove one of those zeros and put it on the right side there
07:19this is not a rotation I'm just kind of like filling in zeros I'm keeping it simple with an unsigned
07:24integer um so if we look at the positions on the shifted number this is a one a two and a four again if
07:34you don't know how to convert from binary to decimal or back see my other videos uh so then we basically
07:40have an eight here and a 16 what's eight plus 16 that's 24 24 is twice of 12 so we have multiplied by
07:48two so you could multiply by four by shifting twice multiply by eight by shifting three times you know
07:53whatever you want to do but uh shifting is is like a shortcut besides just to move the bits it's also a
07:59shortcut to multiply and divide by two okay so that's the basic idea let me start working on a
08:06little program here for you um before I continue I just want you to know that my program it's got
08:12stuff in it already we've got a make file here if I uh I edit the make file I just have a make file
08:21set up to build the program so again this video is is specific to just you know bits we're not going
08:26to talk about make files see my other videos if you want to know how to make your own make file
08:30same thing for this gdb.txt file I've just put commands that I'm going to send to gdb my debugger
08:37of choice um this is not a gdb video I haven't actually made my gdb video yet so look out for that
08:43in this in the somewhat near future um so we're just going to focus on the actual program for now
08:50anyway so I've got a solution already set up here let me start copy pasting some stuff in there
08:56going to copy paste my data section so first off my sample program is just going to have codes for
09:05writing you know with the system call and exiting with the system call and then I'm going to output
09:11to standard output and I'm going to do a zero return code if you don't know that see my other
09:15videos that I've already put out uh I'm going to make a sample string where I just say hello my name
09:21is gibson montgomery gibson that's not my name but I love those names I'm just going to say hello
09:27basically when the program starts then I have some bit strings here that will help me with the
09:32you know help me demonstrate some of the bitwise operations the ending and the oring and the shifting
09:38and whatever and then uh I'm going to put a negative number here as a variable just to prove to you
09:44that sometimes uh the bitwise or logical shifting will break a number if the number is supposed to
09:50actually be assigned integer and and and use that as evidence for why you should use arithmetic shifting
09:56so now I'm going to start my program here let's see my entry point should start at line 30
10:04I got to be super careful about this because if I don't you know 26 if I don't then all of my
10:10debug statements are going to have to be adjusted and that's going to take time on camera okay so I
10:15think I got it lined up correctly so I'm basically just going to print the hello message and then
10:21maybe you know what at the very end I'm going to exit uh from the system with a system call so
10:29basically if we run this program again system calls and everything like that are not part of this video
10:34see my other videos but uh if this works we should see just like a simple message
10:39okay so that's it my name is Gibson Montgomery Gibson okay so then the next thing that I'm going
10:47to do is I'm going to show you a bitwise and operation so I'm going to copy paste that should
10:54start on line 42 and if I get this wrong the debugging is going to be awful I was trying to
11:01make this a pure assembly program without a bunch of extra work I guess I could have linked gcc
11:05so I could just print results to the terminal but whatever too late now so it starts on 42 and
11:13ends on 51 okay so I think that's good
11:16so the instruction for ending doing a logical and between uh two registers which are full of bits
11:25and then what will happen is every bit gets anded against its own course or I guess the corresponding
11:30bit in the other register is just and so what I'm doing first is I'm going to set up the registers
11:37so I'm going to move the a sequence which if you look up is just you know I put a bunch of bits so
11:44like in hex I like to type hex more than binary when I'm just kind of putting stuff in there
11:49so you know each f is basically four ones so it's going to be like I guess 16 ones and then a bunch of
11:57zeros and then you know a few ones and then some zeros and ones so I'm just making a little pattern
12:02for bits a don't think that they mean anything right now and I'm going to make a different pattern
12:07with bits b and when we look into the debugger we should see the before and after patterns so we can
12:12kind of see what the end instruction did so I'm going to move bits a and bits b into r10 and r11
12:20then I'm going to move so so these are going to be like the original bits that we can inspect later
12:27then I'm going to move r10 the original a pattern into r12 so that I can place an and on r12 with this
12:36and instruction against r11 which is the the b pattern so I'm basically taking the a pattern and I'm
12:44anding it against the b pattern but I'm also loading up the registers so we can see the a pattern then the
12:49b pattern and then the result of ending them together finally on line 50 here I'm using a nope
12:55which is just a non-operation so that I can easily um break the debugger so we can sort of inspect
13:03the program and see what's going on inside of there you know what let me uh let me open this up again
13:08here so that I can control where I'm breaking so 48 is where the nope is that's totally wrong I've
13:16already ruined my program somehow so it's gonna be it's gonna be 50 I guess all right 50 and what
13:25I'm doing inside of this and again this is not a gdb video I'm just saying let's break at line 50 so I
13:31can kind of poke around and I'm going to comment all these things out for now that's going to happen
13:35at the very end it's going to be majestic how many different things we can look at
13:40so I'm telling gdb not to execute any of these instructions except for the uh the breakpoint
13:46instruction so we can just break on line 50 okay hopefully this works I'm going to now use make
13:53debug which under the hood is just going to be compiling and then telling gdb to launch with the
13:58name of my program inside of it and then to execute those things so it hits breakpoint number 50
14:04there's a bunch of crap that gets printed and um I can now uh inspect my registers I'm going to go
14:10info r for just info registers and um let's see so we had r 10 11 and 12 so if we look at the terminal
14:22you can see that r 10 the hex version has the original pattern of the a sequence and then r 11
14:27has the original pattern of the b sequence if we do a logical and and put the result into r 12
14:33then you should see let me just double check that that's definitely r 12 yeah then you should see
14:37the result of ending all of the f's together remember that an f is just four ones so I'm kind
14:42of simplifying it a little bit you could make yours more complicated with lots of randomness in your ones
14:47and zeros just to prove it to yourself but notice how r 12 only has an f where there was an f
14:53in both of the patterns up above so like f f means f f zero means zero f zero means zero you know and so
15:01forth zero f means zero so only where you see two f's will you see an f in the final target pattern
15:07so that's a logical and and that's how you do it in yasm assembly you just load up a register
15:15with some bits r 12 we loaded them up with the first pattern and then we uh we take the second
15:23pattern and say that's what I want to and it against and you can have that in a register or I think
15:28an immediate or a memory location so we're just ending these registers together and you know that
15:34they're loaded up with patterns a and b so nothing too hard at this point let's do uh let's look at
15:40bitwise or this should start on line 52 or I'm screwing myself 52 okay 52 so same thing we're going to load up
15:52uh r 10 and r 11 with uh the bit patterns and then we're going to move uh we're going to move the
15:59first bit pattern into r 12 so that we can or it against the second pattern and so basically r 10 and
16:0411 will have the original bit patterns r 12 will have the result of oring them then we'll do a nope
16:10on line 60 so I want to break here on line 60 uh yep I ruined all my line numbers what did I do wrong
16:18I think I just have it offset by two now for some reason so if we just kind of run the program now
16:25in the debugger again I'm going to do quit and then yes if we uh clear and make debug probably clear
16:31was unnecessary we're broken at line 60 if I say info registers the registers in question are
16:38oh shoot um 10 11 and 12 right so we look at 10 and 11 they have the same pattern as before
16:46and 12 has a lot more f's because if either of the patterns had an f then the resulting pattern
16:53would have an f so this is a logical or same idea as uh and in terms of the way you format the
16:58instruction and and the ops but uh it ors instead of ends okay no problem let's move on next thing I
17:06want to show you is the xor which is pretty cool by the way so far we're just operating on bits
17:13we don't consider these uh bits to actually be numbers maybe they are but uh we should probably
17:18be careful about that uh anyway so I'm going to say let's check out our bitwise xor which is
17:24exclusive or which means exactly one bit uh in any pair needs to be a one for the resulting uh bit to be
17:32a one and of course per usual all bits will be checked uh against each other uh in the pair of
17:38of bit patterns it'll be one bit at a time from each pattern that gets checked
17:43okay so it starts on 62 and then we got a nope there okay so same thing that we did before we're
17:51just loading up two registers with the bit patterns and then r12 is going to be the result of exclusive
17:57oring or xoring the two bit patterns so that means we're going to break on line 70 now let me just
18:02get rid of my other break point change that 68 to a 70 and now I should quit and if we do make debug
18:12we're at 70 good info registers so I think we said it was 10 and 11 had the original bit patterns
18:18and notice the resulting bit pattern it only has an f in a position where there was exactly one f
18:25between the two input patterns so wait did I just do something wrong here what did I just do wrong
18:33hang on a second I'm seeing something bad xor r12 am I looking at r12 yeah r10 and 11 did I just do
18:44something wrong 10 11 and then we load 10 up into 12 and then we xor it by 11 and then it stores it in 12
18:52something's a little fishy here exactly one exactly one xor I don't like that what's oh
19:02I understand now okay so that was a little scary this is shifted because the printout is not great
19:09see how there's like a blank space here that threw me off so you have to imagine that all of the bit
19:14patterns are kind of shifted maybe I should just copy paste this I was worried that I suddenly didn't
19:18know how to do an xor in assembly so let me get rid of these integers here notice how there's just a
19:24blank space that that shifted everything and threw it off in terms of the display so I'm going to do
19:29this and what should really happen now is that there should be a zero here a zero at the front now
19:37everything would make more sense sometimes the display likes to just kind of truncate things and
19:42just assume that everything will be read correctly but it wasn't so notice in the first position both
19:47of the in both the input patterns are in are an F so the output pattern will be a zero you can imagine
19:53under the hood there was a bunch of ones and then a bunch of ones and that resulted in a bunch of zeros
19:57here we've got like an F and a zero so the result is going to be an F and then F and zero zero zero and an F is
20:04an F and then where you see zeros that means there was not exactly one one in each bit position so for
20:13the let's see the first one that's a bunch of zeros for both input patterns so that's why there's a zero
20:19there then for the next two each of them had all ones or just the two F's so that's why xor says it's a
20:26zero and so forth right so like at the end two F's making make a zero okay that was a little scary I'm
20:33glad I figured that out quickly instead of taking a million years okay so now let's do another
20:42example we're done with xor now let's look at bitwise knot I forgot to explain knot I'm not
20:47going to redo this video but let me just say that a knot is just inverting the bit so if we have a zero
20:55it becomes a one and if we have a one it becomes a zero and um if you if you knot against a register
21:02that has lots of bits or a memory location that has lots of bits you can do that and just every
21:07single bit in the sequence will just be flipped from a zero to a one or vice versa so let's see we
21:13just did bitwise xor now we're going to do a knot starting on line 72 I guess and then we're going to
21:23do another nope there so 72 okay all right so now let's get rid of the break point at the line 70 and
21:34put it at line 78 instead 78 oh man this video is going to take forever okay so now let's check out
21:45bitwise knot we're going to have r10 oh uh I guess I should improve this because what I'm doing is I'm
21:55moving everything into r10 and then I'm just knotting r10 so we can't really compare r10 against itself
22:02that's not fun let me instead move uh move uh r10 into r11 so that I can knot r11 so that we can
22:11see two different registers at least okay so let me just update my solution real fast
22:1673
22:19um move r11 r10 okay so now it stops at 75 and that one stops at 75 okay cool
22:32now we got our break point should be at line 79
22:36not 78 now we're ready to run it and see what the knot does
22:41clear and make debug info registers so we should have r10 r11
22:47again the formatting is a little wonky because there are missing zeros
22:52so I'm just going to copy paste it up here I'm going to do this get rid of the numbers
22:58pad on the left for the missing zeros and then now you can see clearly that all of the
23:06the f's were flipped to zero again that means you know each f had four ones inside of it so all those
23:12ones were flipped to a zero so that's why it's a zero okay so knotting pretty easy not too bad
23:18and then I'm going to quit this and show the next example
23:22which was going to be I see 79 let's check out bitwise rotation
23:29I'm going to copy paste all the way up to that note there should start on line 81
23:34I paused before I took a drink of water hopefully I reminded myself later to edit the drink of water
23:43out I don't know maybe you'll be wondering why my mustache is wet right now
23:47if I did not edit it out okay so let's check out bitwise rotation so I'm going to set up
23:53r10 and 11 and 12 and 13 and 14 all with the a pattern and then I'm going to start rotating things
24:02so first I'm going to rotate to the left r11 and then r12 I'm going to rotate by one position on r11
24:11and then two positions on r12 and then I'm going to rotate by one position for r13 and then two positions
24:21for r14 so we should see some interesting patterns the nope is now on line 94 so I'm going to break
24:27there instead whoops so I'm going to change that and I'm now off by three it's getting precarious
24:34and then I'm going to uncomment all of my little print statements to show you the value of the
24:40registers after I rotate it so we should see like a bunch of stuff get printed in the terminal
24:44just to prove to you what's going to happen okay so man I got to remember what's happening now
24:52first thing that I did was I printed r10 to show you one type of rotation and then I printed r10
24:58again to show you another type of rotation so we should see the original and then the two rotated
25:03values and then the original again and then the other two rotated values so here's the original
25:08we're printing it in binary with the special print command in gdb which is just like p slash t
25:14and then the register so this is the original pattern right here and if you just kind of squint
25:20at it for a while you can realize that the dollar sign two pattern has been uh shifted once to the
25:28left actually let me just make sure that I'm doing a shift not a rotate I'm accidentally saying the word
25:33rotate I think and uh that's oh no no it is rotate yeah we're going to shift later I guess
25:40okay so we are definitely rotating so we should see some uh wrap around uh I guess I I used bad
25:51bit patterns for the wrap around but just imagine that uh you know in this pattern right here
25:55uh this one if we're rotating to the left this one fell off and it got placed on the right side of
26:08the next pattern it's going to be the same thing basically uh so but this is rotation so we would
26:12be reusing uh the the bits that fell off but anyway you can see that if I rotated to the left by one
26:18position that whole thing is is kind of shifted to the left I shouldn't use the word shift and then for
26:24the second pattern uh we did a rotation by two positions so all the bits are just kind of moved
26:30uh to the left compared to the very first pattern okay so no problem then the next thing that we're
26:36looking at is just you know rotating to the right and you can tell that everything is offset by one
26:40here and it's offset by two here so rotating to the left by one position by two positions rotating to
26:48the right by one position or by two positions that's rotation in a nutshell okay so then the next thing
26:57that we're going to do we're on 94 now I'm going to grab some logical shifting code
27:02um let's see logical shifting nope nope nope I'm going to grab some code that will shift both
27:10logically and arithmetically so we can kind of see the difference okay so show maybe I should change
27:19just to show some some shifting because uh if I just say it's logical then yeah sure uh the shift
27:29function uh the shift instructions here shl and shr those are uh logical shifts but then down here
27:36when it's a sal and sar those are arithmetic shifts I don't know maybe maybe I can do that
27:42maybe I can just uh duplicate the comment maybe to make it more more intuitive so show arithmetic shifts
27:53and I'll just take that out okay I think that looks pretty good let me uh paste that into 105
28:04on my solution so I don't get out of sync here so what's going to happen here we're basically going
28:09to load up r10 with a negative number I've chosen the number uh negative 12 because I kind of feel
28:15like it at least shows you a little bit uh for dividing and stuff like that so we're loading up
28:21r10 with a negative number and then uh r11 is going to get r10 but then we're going to shift it left by
28:27two positions so that should be uh multiplying it by four but this is well a bitwise shifting so it it's
28:37not going to necessarily respect the fact that the number is in two's complement so this is not a good
28:42idea if you consider r10 to be a number because sometimes the result might be right and sometimes
28:47the result might be wrong here it says math won't work in my comment but sometimes it will accidentally
28:52work but you still want to consider it to be non-working for integers it's only good for bit shifting
28:57uh then we're going to put r12 uh we're going to give it the same value as r10 and then we're just
29:03going to shift it to the right by two positions this will definitely break because when you shift
29:08to the right the sine bit is going to get padded with a zero rather than what the actual sine bit
29:14uh was originally so that will almost certainly break it almost every time
29:18then we're going to use arithmetic shifts which just respect the fact that the original number is
29:24two's complement so you know like we have this negative 12 number it's going to end up being bits
29:29but the uh the shl and the shr instructions are not going to respect the fact that it's negative
29:3612 they're just going to consider them to be bits but the sal shifting to the left
29:41arithmetically and the sar shifting to the right arithmetically are going to respect that
29:46respect the fact that it's two's complement
29:48so let's see we've got all this stuff and then we've got a non-operation here
29:54um you know again like per usual when we're shifting when we're rotating when we're oaring
30:00anding whatever it's just the instruction name followed by both the source and the destination
30:06operand and then the second operand is only going to be source so if we have a shifting left r11 then
30:13the result is going to be stored in r11 but it's going to take as as the source r11 and shift it
30:19to the left by two positions and so forth okay so we have all of that we're going to nope on line
30:26what is it one oh 114 so that means i'm going to stop breaking on 94 and i'm going to break on 114
30:34i thought i was four lines off and now i'm only two lines off i think i just did something else wrong
30:40here between my source let's see is the nope on 114 up there yeah it is okay guess i didn't do that
30:47much wrong okay so let's run this but uh this code up here was just going to print bit patterns
30:54related to our rotations i don't really want that anymore so i'm going to uncomment all of these other
31:00prints that relate to our shifts and this is going to be a nightmare to read and interpret but i'm
31:05going to try i really should have written a more complicated program that can uh just print them in the
31:11terminal so i don't have to like read the debugger whatever it's brain exercise
31:16i think this actually is the end of the program 119 is the last line yeah okay so we just need to run
31:22this and see what happens so really we're interested in these uh you know shifting to the left twice
31:28shifting to the right twice bitwise and then shifting to the left twice arithmetically shifting to the
31:34right twice arithmetically okay so we'll go back to the terminal here quit and then i'm going to do
31:40make debug and now there's a bunch of junk here's the original number and then we're going to bitwise
31:48shift left binary uh notice how it just sort of you know it shifted everything to the left
31:54and you know we're not using we're not printing hex anymore because i want you to see
31:57uh the binary and then um you know like the the patterns are just shifted to to the left like see
32:03here's the first one if we're looking at the right side and then here is the first one if we're
32:08looking at the right side for the shifted version notice how it's two positions to the left
32:11a similar deal for shift right except we lost a couple of numbers there because of the display
32:17so i'm going to copy paste both of these real fast per usual we lost numbers due to the display
32:24so i'm going to say notice how we have two numbers missing here if i added zeros on the right side that's
32:31going to change the actual value so i'm going to have to add them to the left
32:33and now we can clearly see that uh the original pattern here was uh
32:38shifted four positions i think to the left like did i do that what did i where did i even just copy
32:47paste from original shift left and shift right okay so shift right is what it's supposed to be doing
32:54let me just double check here shift right is printing r12 and what does r12 have it has
33:03shift right by two so it should have two positions shifting right by two okay
33:11oh i'm supposed to compare it to the very first one let me just double check that i have this correct
33:21now
33:21shift why is it shifting to the right oh gosh i think i just lost track of myself let me just
33:32give me a second here r14
33:34i'm forgetting where i copy and paste okay bitwise shift right is
33:42bitwise shift right is r12 not r14 oh gosh did i do it wrong were you cringing at home
33:48do i shift right is r12 maybe maybe this will work
33:57and the two that's kind of the same thing that i did before no no no it's different okay so i had
34:02it wrong before now i finally have it right so you can see that the bit pattern is shifted twice
34:06to the right and then on the left two zeros show up which would have destroyed a number okay
34:11so we shift it to the right and now we're ready to look at arithmetic shift
34:15uh to the left and right and um let's see do we have enough bits to actually print everything
34:25this one should look like it shifted to the right
34:28just because of the way it prints so that means this one is totally lined up okay so let me grab
34:32the original pattern here so this is the original pattern and then if i go shift left
34:37in binary i'll just put an l here and then i'll put shift right in binary
34:49there we have enough bits i think in both cases so we don't have to pad
34:54so now if we look at um the original pattern and then we shift left
34:59arithmetically twice notice how it does kind of look like it survived right like this one
35:04moved over to the left twice because the sign bits are just kind of falling off but there's enough
35:09of them that it's not going to change the sign bit thank you twos complement but when we shift to the
35:13right arithmetically normally we would have padded on the left with zeros remember we had zeros there on
35:21the left before but now that we're respecting the arithmetic uh the the padding is correct now for
35:29twos complement so that's why there are ones there instead of zeros and then if you kind of look at
35:34it it's like well we shifted twice to the right let me get rid of the left because we already understand
35:40that now it seems to be that the pattern is uh every bit is shifted over to the right the correct
35:46number of times the zeros dropped off the side and um the sign bits were padded on the left so everything
35:54seems to be correct and then just to kind of prove to you uh that what we're seeing is is real i just
36:00have the same things printed all over again from scratch in decimal so originally we had our r10 as
36:07negative 12 and then if we do a bitwise shift um to the left uh it multiplies it by four and so we're
36:17accidentally correct here remember this is a bitwise shift so it doesn't respect uh twos complement so just
36:22you know by pure chance uh we happen to have a starting number that would end up looking correct
36:29after we shifted it to the left and then when we do a bitwise shift to the right again this could be
36:34disaster because we're not respecting twos complement or or the sign bit and that's exactly what happened
36:40you know definitely if we're shifting to the right that should be 12 divided by 4 equals uh 3 or negative
36:473 but instead we get this gigantic huge number because the sign bit got killed with the zeros
36:53that were padded on the left and then we uh finally try to shift left arithmetically we end up still
37:00with negative 48 which is the correct number if we're going to shift left twice that's multiplying 12
37:05by 4 and that's 48 or negative 48 m and then when we shift to the right that should divide it by 2 twice
37:13because we're shifting to the right by 2 positions so that should be 12 divided by 4 which is negative 3
37:18or sorry negative 12 divided by 4 which is negative 3. so everything seems to be working except for a
37:24couple of uh moments of stumbling on my part which i hope you laughed at um and i think this is the end
37:33of the program so now hopefully you're an expert in uh ending oring x oring nodding shifting left and right
37:42uh bitwise shifting versus also arithmetic shifting and also bitwise rotating we've done it all in this
37:50one video how long did this video take 40 minutes anyway thank you so much for watching this video i
37:57hope you learned a little bit of stuff and you had a little bit of fun i'll see you in the next video
38:04hey everybody thanks for watching this video again from the bottom of my heart i really appreciate it
38:09i do hope you did learn something and have some fun uh if you could do me a please a small little
38:14favor could you please subscribe and follow this channel or these videos or whatever it is you do
38:20on the current social media website that you're looking at right now um it would really mean the
38:24world to me and it'll help make more videos and grow this community so we'll be able to do more videos
38:29longer videos better videos or just i'll be able to keep making videos in general so please
38:34do do me a kindness and uh and subscribe you know sometimes i'm sleeping in the middle of the night
38:40and i just wake up because i know somebody subscribed or followed it just wakes me up and i get filled with
38:45joy that's exactly what happens every single time so you could do it as a nice favor to me or you could
38:50you control me if you want to just wake me up in the middle of the night just subscribe and then i'll
38:54i'll just wake up i promise that's what will happen also uh if you look at the middle of the screen
39:00right now you should see a qr code which you can scan in order to go to the website which i think
39:04is also named somewhere at the bottom of this video and it'll take you to my main website where
39:09you can just kind of like see all the videos i published and the services and tutorials and things
39:13that i offer and all that good stuff and uh if you have a suggestion for uh uh clarifications or errata or
39:23just future videos that you want to see please leave a comment or if you just want to say hey what's up
39:28what's going on you know just send me a comment whatever i also wake up for those in the middle
39:32of the night i get i wake up in a cold sweat and i'm like it would really it really mean the world to
39:38me i would really appreciate it so again thank you so much for watching this video and um enjoy the cool
39:45music as as i fade into the darkness which is coming for us all
40:01so
40:12so
Be the first to comment