52: Bits Operations: Shifting.

Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categorías:

You can do more with bits than just turning them on or off. This episode will show you how to shift bits left or right for either really quick multiplication or division or to maneuver them into place. If you start out with the value 0x5a and want to first isolate the 5 digit, the previous episode explains how to use masking to accomplish this. After masking, you have 0x50 which is not quite the value 5 that you might need. If you want to convert 0x50 into 0x05, then you’re going to have to shift it to the right. Shifting either left or right is based on binary digits not hexadecimal digits. Because each hexadecimal digit is composed of four binary digits, then to shift a hexadecimal value by one of its own digits, you’ll need to shift the underlying bits by four positions. Let me show you this in binary with the value 0x50: 0101 0000 shifting this to the right four times gives: 0010 1000 this is after a single shift 0001 0100 this is after two shifts 0000 1010 this is after three right shifts 0000 0101 and finally after four right shifts. Notice how as the digits shifted to the right, we filled in the vacated positions on the left with zeros. The same thing happens if we shift to the left except that now the vacated positions will be on the right. Maybe you want to swap hexadecimal digits and turn 0x5a into 0xa5. The example just now shows how to move 0x50 into 0x05. We didn’t actually need to mask out the other digit though. Because when shifting, you know that vacated spots get filled in with zeros but it’s also possible for digits to fall off the other side. Let me show you what happens if we shift 0x5a to the left this time by four shifts. 0101 1010 this is 0x5a before any shifts 1011 0100 this is after the first shift to the left 0110 1000 this is another shift to the left and the first one has dropped 1101 0000 this is the third left shift 1010 0000 after four left shifts, the “a” is now in the left digit and the “5” is gone. Listen to the full episode or read the full transcript below. Transcript The previous episode showed you how you could isolate, set, and clear bits either individually or in a group. If you have a byte with the value 5a in hexadecimal, you know how to isolate each half now and with appropriate masks, you can easily turn this into either 50 or 0a. But what if you wanted to work with just that 5 part? In it’s current position, even after masking, it’s not 5 it’s 50. I’ll show you how you can shift this so it is just 5. We going to use some operations called shift left and shift right to accomplish this. These are bitwise operations and allow you to shift bits a certain number of positions. Remember that in hexadecimal each digit actually represents 4 binary digits. So to shift the value 50 so it becomes 05, it’ll need to be shifted to the right by 4 positions. There’s actually a lot more to these operations than might first seem likely. the first thing to note is that each language can define the details of these shift operations as they see fit. The basic operations will be the same but the edge cases could be different between languages. For example in C++, if you try to shift either left or right by more positions than your type has available, then you may not get the results you expect. This is getting into undefined territory so make sure that when you shift bits, that your shift amounts make sense. Another good way to cause problems is to try shifting by a negative number of locations. Your compiler might generate code that behaves differently than another compiler in these situations. Another aspect to be aware of when shifting to the right in C++ has to do with negative values. I’m not talking about trying to shift by a negative amount. That already doesn’t make any sense. I’m talking about what happens when you have a negative number and try to shift it to the right. Most implementations th

Visit the podcast's native language site