The last article about bits was intended to people who never in his life heard the terms 'bit' and 'bytes'. You may disagree with my arragement of sheet of papers. What makes you think that 8 lockers or 8 buckets it's trully a 'byte'? They are all abstracts representations.
Nobody told you when you start learning what that lockers or buckets are. They just say to you: "This is a byte". So, you keep drawing that as a donkey following the carrot. Ok, I confess, I still using that representation. It's easy and it's fast.
Switching sheets arrays.
Ok, to keep us standarized (industry loves that) we dispose of our sheets and we start drawing. It's cheaper also!. Here is our sheet:

Now we convert this as the industry want us to do it:

That above is our bit. Let's convert the remaining 7 bits and change the scale a little :

We have formally made our byte. That is the 1001 1010 byte. If you labeled as I told you before:

...and do themath: 128 + 16 + 8 + 2, you'll get 154 What that means? Dunno, maybe means nothing. What I can tell you is that starting from 0000 0000 being the natural number 0 (Ok, hold on, if you think 0 is not included in Naturals go ahead...) and then switching in order the bits values, that is, 0000 0001; 0000 0010; 0000 0011; 0000 00100 you get it? Read again...As I was saying, the byte 1001 1010 is the counted number 154. That's the only thing I can tell you about what 154 means as is it showed there. The 154th combination.
Before I said that this isn't a history course. If you are familiar with ASCII you possibly think about what that 154 is. ASCII, informally, is a way to code letters using a byte. As you may guess, the possible quantity of letters we can code is 256. There are more ways of enconde letters. At the beginning of computing, weird (?) letters were not included (why they are weird I don't know. Go Tokyo and say that they are weird...). If you want to dig up, go ahead, search enconding.
So, if you are thinking is ASCII then you know what that means. And so on, the same as if you were thinking in number representation, your designed alfabet, your code, whatever. It's the only way in this environment. It's all about low voltage, high voltage, zero, one, false, true...etc. You need to adapt to this world.
Now that we have a formally byte explained...tell me what exactly is a shift? If I were a flash designer I'll make your life easier with the idea I have in mind of explaining this using an animation. A big guy "pushing" the byte from left or right squeezing the bits until they switch lockers or buckets...True thing is, I'm a disaster drawing things (maybe I could ask this to my friend).
Shift is a push. Yes, a big heavy bit comes from one side and pushes all the other 8 bits to make his way making the ones in the other end fallout to a bottomless pit. \"Making the ones in the other end fallout to a bottomless pit?\" Not clear enough, it's true. Let's go through it. A shift is an operation, as addition, substraction, etc. As many operations it has an operator and operands. Now, we are going to use C standard. I'm not going deep in abstract shift theory because C shift operands are bytes. So you don't really shift a bit into a byte although you can emulate this using a byte. For example, imagine that the big heavy bodyguard bit makes his way to the fourth locker or bucket from the right of our byte above pushing the others. ASCIIally (graphically), we have:

That is what happens when the pusher push from the right. Same thing applies if it were from the left. Er...not actually same thing (do I need to be perfect?) I mean, analogue thing happens but this time from the left. The bit makes his way to the fourth position from left to rigth pushing the bits from the other side to a bottomless pit. When I learned this at University they taught us differents kinds of shifts. From the left, from the right, with 0, with 1. C standard shift operators '' shift a byte with 0s. So, if you have the 154 in our example: 1001 1010 and you shift from the right with 0 six times you'll get 128, that is, 1000 0000. Let's go throught but textually. You have the binary 1001 1010 and a bottomless pit:
Add a 0 from the right. Then you'll have:
If you add another 0 from the right (also you can see the push as adding 0 from a side):
And that 1 in the bucket label 128 means we have 128 + 0 + 0 + 0 + 0 + 0 + 0 + 0 = 128.
This line of C code could be wrote as:
You'll need to do some tests to find out more things about shifting. Did you know that '1<<2;' gives the same result as '2<<1;'? If a 0 comes pushing until position two in the byte 0000 0001, then that 1 sits now on position labeled 4 (get that? 0000 01000). If now you push a 0 in the byte 0000 0010 only one time, you get 0000 0100. \"Can we say then that '<<' is conmutative?\" Wooooo, hold on...You can't say that just for viewing an example the conmutative propierty holds. You need to prove that. And in order to prove it you need to take an Algebra course maybe, but needs to be proven for all possible cases. But, if you find out that a particular case does not hold conmutative propierty, then you can say that it's not true that '<<' operation is conmutative. So, if you show that '3<<1'(0000 0110) is not equal than '1<<3'(0000 1000) then you say that conmutative propierty does not holds in the '<<' operator. \"but...it certainly is! you said '2<<1' equals '1<<2'!\". Ok, let's put it this way. You can't ensure that conmutative propierty holds in '<<' whatever numbers you take.
We now know that '<<' C operator shifts with 0s from the right. \"With how many?\" The right operand is used for that. More specific:
Shifts a byte from the right with 0s n positions. Or, introduce n 0s from the right pushing bits to the left throwing the overflowed ones (not 1s).
Ok, that's with '<<'. What about '>>'? Well, it's almost the same thing, but who knows why they made the lecture more complicated. I mean, shifting from the right is quite obvious while looking the syntax. It's straighforward seeing 'byte << n' and you may guessing that shifting from left would be 'n >> byte'. For our representation of sheets and/or buckets, the latest seems the approach. But the truth is that a shift with n 0s from the left in C is:
And there you have it. If we take 154 (1001 1010) and we shift left with two 0s the result will be 38 (0010 0110) and the C syntax will be:
No. It's not conmutative.
Maybe you are wondering why all of this crap of shift and bytes and why don't we program in Java? Go, write Java code I'm not telling you not to do it. What I'm actually saying is that you need to know what a bit is and what a byte is. You also need to know what a shift is! One of the most used feature. In micro world you set up bits. We are talking at bit level. A particular bit needs to be set. So, will you made an object called Bit that it has a state atributte with setBit and getBit methods and a Byte object that has setNBit methods ...blablabla? No thanks, wanna set a bit? Continue reading...
Eat my bit.
"This crap of shift things were funny, now what?". When you start making your way to the microcontroller world you'll find out that there is no Operating System (what a deduction, huh?). The micro is at your enterely destiny (wanna burnt it? burnt it!). That means that you need to programm it to make it work. Without a program it's useless. The micro has a lot, a lot of registers (there are differents micros of differents manufacturers with differents caracteristics with differents logo with...ok ok, you get that) and that registers are used to configure the chip facilities. You enable and disable gates, features, modules, timers, and whatever feature the micro has. As you may guess, all of these registers are putted in set of bits. You have 8bits registers, 16bits registers, ...and depending on micro. A microcontroller is like a microprocessor of your pc but a little more limited (or a lot more limited, as you want). It's so that you'll find out that 8bits register is very common thing as is the minimun byte unit.
So, you already know what a byte is and how is represented by most of the human beings. How do you think you can arrange a \"thing\" with a list of eight features that can be turned on and off? why \"thing\"? People get confused uppon new words. And, people get confused uppon old words but in other context. What is a register? A register is that \"thing\" I mentioned. Generally speaking, it keeps the registry of the enabled features (they are also used as buffers to interchange bytes with I/O but, each thing at a time). I give enough time to think of how to arrange that \"thing\", let's go through it:

"What?!? that's the representation of 154! that's a byte! Liar...". Maybe I am, but an 8bit register has that view. That is the reason I told you that I don't really know what that 154 means. Let's change the labels:

The fn labels are the numbered features that this register has. How does enable things it's not part of this lecture. That it's owned by each microcontroller and it's done by hardware, I mean, by logic gates.
The above register features; f0, ... , f7 are enabled by setting a 0 or a 1. That's depend of the manufacturers mood. So, read datasheets. Imagine that f0, ... , f7 are enabled by setting his bit to 1 and disabled by setting his bit to 0. So, f7, f4, f3 and f1 are enabled in that register. \"Wooojooo! I get it! 154 means that f7, f4, f3 and f1 are enabled!\". Yes, indeed. So if you need to set or unset a feature in that registry how do you think you can do it? Nah, not doing:
Using the shift and or operations could be a way. \"OR? or operation?\". Put it this way, OR and AND are byte operations (again, C-related, if you want to dig out in theory how these operations are, dig up and search for boolean algebra). They take two operands; bytes, and each one aplies the OR and AND operation respectevely to each correspondent bit on each byte. All I'm gonna say about AND and OR operations is this table:
bit1 bit2 OR-result bit1 bit2 AND-result
0 0 0 0 0 0
1 0 1 1 0 0
0 1 1 0 1 0
1 1 1 1 1 1
Ok, get back to our feature-enable problem. We need to enable feature six, that is, f6. But, we don't want to disable any of the other ones, just add feature. You are maybe thinking that if we have the 154 that represents our features on, adding a feature f6 (that is, put f6 to 1) it'll be as adding a number to 154. If you remember our labeled example, all features including f6 will result in the binary number: 1101 1010 and 154 was 1001 1010. We got 154 by adding 128 + 16 + 8 +2. Now we want: 1101 1010 that is, 128 + + 64 +16 + 8 +2. See the difference? No! put that calculator away! you don't need that to see that we are adding 64. Now, 154 + 64 = 218 will be our representation of f7, f6, f4, f3 and f1 enabled. Seems a hard work to do it remembering what number we need to add. I tell you what, if you use shift and or operations you are doing exactly the same as we did.
Take a look. We want feature 6 to be enabled. That is the 6th bit. \"No! wait, the 2nd!\".. You are right too. It's all relative . Let's say that the bit to the left is MSB (more significant bit) and to the right LSB (less significant bit). And the one in the middle? Well, although being a \"joke question\", actually in a pair number of things you can't say that only one object is in the middle (cut it!) so bytes are ussally separated by four on one side and four on the other (nibble). And, when you have 16bits register (2 bytes) you talk about the High part of the register and the Low part of the register. Then High part will be a byte and the low part too. Oh, again...this it's not a history course so if you are saying that I'm not right and the LSB and MSB it's also relative, you are right. I'm not gonna talk here weird architectures (dig up: little endian, big endian).
Roll back. We had a byte. We called that byte a storage for our features. We used the word register and we wanted to set/unset features from there. We added 64 to 154 and we have 218. For us that means that f7, f6, f4, f3 and f1 are enabled. Let's do it shifted.
From right to left (from the LSB) and starting with 0, f6 is the 6th bit. If we push a big heavy 1 to that position on a temporary byte, then we'll have 64. All the thing left is to ....add it? Look at the above table, if we use AND operation we'll get a 1 if there was already a 1. But, that means that the feature was already enabled! (some flags are unset if you set it again, depends on micro. Read datasheets). So, a better approach is to use the OR operation. Now, we are ready to set our feature in C language:
Let's go through it:
(1 << 6);
What is the left operand? Left operand is a byte. Binary representation of that one is 000 0001.
What is the right operand? Right operand is an integer that indicates how many 0s you wish to add from the right.
"But, isn't it easier just to add 64?". Maybe for this case, but you'll find out that the above approach it's easier and most commonly used. Nevertheless, you will have the same result, little hacker :P
What is that '|'? '| is the OR operator. Left and right operands are bytes and '|' applies the bit OR operator to the correspondent bit on each byte. See the above table.
That's it, we have enabled a feature. Same thing applies for enabling other features. At least for now we stop here.
Next time we'll see how to disable a feature. I'll get some rest."






