I like C...but no, I'm not as you may think a C-gurú for what I've stated before. I just like it. I don't like object oriented languages. Maybe the main reason resides in that I like hardware stuff (how do I get to that conclusion I don't care). The truth is that I thought that I knew C. I mean, I can read C and detecting the most used language stuff as declarations, syntax, etc. In fact, I wrote a C-- compiler in my 4th year of University. I said I like hardware. Actually, I'm an electronics hobbyist and when I inserted myself into the micocontroller world is where I knew that, I don't know C at all.
What the heck really means a shift? and why you are asigning 1 to an unsigned char? 1 is an integer and a char is a char! Well, those and a lot of questions arises when I get into the micro world. Note that I didn't emphatize micro as a shorthand for microcontroller. It has more meanings. Micro, as the reduced space you have while programming. Micro, as a shorthand indeed. Micro, as the variables space. When you write code for computers, be real, you don't give a sh*t about space. You need a char, you declare a char; you need an int, you declare a long; did I said double? Yes, why not? Did I mention something about being legible? Forget about that. You need space. It's all what you need, and of course, good code in the sense that it's actually doing the expected things and - if you were smart - in the most accurate way. Wanna be legible? Write a book...what? assembly code seems legible to you? Code is for coding, docs is for documenting. So, document your work. More legible you came with extra varibles, more space you waste. Please, write docs!
It's all about bits.
Yup, bits. If you don't get what a bit is then you may try reading my explanation. You can avoid this section if this is too boring for you. If you are not familiar on what a bit is and all of this computer things of 0s and 1s are magic, then I should ask you don't write any lines of code at least in C. Forget about programming a micro with 2KB of memory. Go with the magics of OOP and feel good in managing objects...Of course, assembly and machine language may have the same vision of C. Although, there are only three instruction from C to assembly. Remember, I wrote a C-- compiler (only a function withouth context switchs). Writting a compiler means going through all the compiler stages. One of those is code generation. As you may guess, in that stage you write assembly lines translating what you wanted to say in the more abstract language. So, trust me, there aren't much more than 3 instructions to assembly. A bit is whatever entity, object or thing you want (be abstract for now) that has only two possible states. Think what you want. Who cares, in math terms nobody cares in what are you thinking a bit is. The important thing is that you realize that a bit has only two states. You may think of a light-bulb (common example). But, I preffer my first thinking on what a bit was. Think about a sheet of paper. It has two sides, doesn't it? If the sheet is all blank you can't distinguish which side is which one. Guess what? Also that doesn't care on maths! It has two possible sides (using the fact that no funny guy turn the sheet in n pieces). And guess another one? It doesn't matter the sheet size. Is it big? is it small? I don't know! it's an object with two states. Side X is state one and side O is state two (you want to change the letters? go ahead change them!). I use X and O as the tic-tac-toe symbols, an X (crux) and an O (big O, zero, circle, etc). Then, we have our first bit. A piece of paper has been converted to a bit!

Grouping (sheet strings).
Ok, we have our bit (or our sh*t? sheet + bit = sh*t). But, it's only one bit. So, it could be showing us the X face or the O face. Remember, two possible states. Now, imagine you just bougth a block of sheets with a lot of them, let's say 500 sheets. If you take 8 of these sheets and you mark them like the first sheet, then you would have 8 sheets with the same caracteristics. What you are actually doing with marking, is making them distinguishable one side of the sheet with the other one. Now imagine you arrange those sheets in a big-long table one aside to the other. At this moment you will have 8 sheets showing you just one face. The X or the O.

If you do some math you'll be deducting that all possible combinations of the 8 sheets showing you the X or the O is 2^8. Why this number? Well, as each sheet has only two faces to show you (don't be an *sshole trying to be a funny guy saying that you could put the paper vertically, horizontally, or whatever...you may be right, you can put the sheet of paper in many many ways, but be aware of the example), you start doing empirics tests. One piece of paper, two faces. Two pieces of paper, 4 faces. Aha! 2 by n...Then, Three pieces of paper, 8 faces..."crap, the recent theory to garbage". Well, the truth is that exist a mathematic equation to calculate how many possibilities you have with N objects when they have two states and it's 2^N. "Wow! I've already seen that numbers in some place!" Of course, almost all related numbers to computers are power of two. But wait, let's continue with this bits thing. If you take those 8 pieces of sheet, or 8 bits, all the thing together they form what is called a 'byte'

Yes, a byte is composed by 8 bits and the maximun possible number of combinations you can have with that is 256. With a 'byte' (8 bits) you can make up to 256 combinations for representing something. Guess what? This concept is used to represents numbers (we aren't going to talk to much about it as it's a more complicated thing for this article). Yes, as you may already noticed you can represent for example, the first 256 natural numbers. "Oh god! why the first?" Well, if you want you can represent integers numbers from -9 to 246. Surprised? Let's count how many numbers are in that interval and you find out that there are 256 numbers. "And...why numbers?" Well, computers were pretended to be calculating machines for scientific formulas, but choose whatever you want to represent. If you need to represent states of some kind of artifact, a way to know how many bits you'll need (or sheet of papers) is using the 2^N formula. Imagine you have ten numbers and two lights. Think about how many combinations you'll need for all possible situations. Statistics and Probability subject taught me to find that number. Combination, Permutation, etc, are tools you'll find useful for this. So, if you have 10 numbers and each number is going to be combined with two lights (that have on/off states) you may think in enumerating the possibilities (mmh..rather slow but, they are not quite much). Take one number, light one off, light two off. Take same number, light one off, light two on...
1. 0 | L1_OFF | L2_OFF
2. 0 | L2_OFF | L2_ON
3. 0 | L2_ON | L2_OFF
4. 0 | L2-ON | L2_ON
5. ...
...and so on. Affortunately, there exists those tools I mentioned above. This is not an article about that, so, the resulting equation is: 10 * 2 * 2 = 400. So, there are 400 combinations for lights and numbers. What do you think? Will a byte suficie? Actually no, a byte has 256 posible combinations so 8 bits it's not enough. Let's add another one! 2^9 = 512 combinations. Seems a good number. Could we use more? Yes indeed, and that's what happening with window$ vi$ta, your favourite game, etc...We can use more of course, but it'll be a waste of resources! "oh god, f*ck off! I got a 4gb ram quad core pc!" Yeah...right. Let's get back to our example. Ok, now we got 9 bits, "is it two bytes?" Nope, "three?" Neither. A byte is a set of 8 bits, so do math and 2 bytes will be a set of 16 bits. Nine bits is more than a byte but it's also less than two bytes..."how is this possible?" Well, it is. Not too hard to realize that, if you change the unit you'll get this question on and on when trying to compare something. For example, 1000grs it's a kilo, is it 1200grs less than 2kg and greater than 1kg?
0s and 1s.
I don't want to start a history course, so briefly if you change X for 1 and O for 0 (zero) then you have a more computer-like sh*t! :) That is how the bits are interpreted in computing by humans, and mainly that is what a bit is when you talk to someone on the same frequency. A string of 0s and 1s representing possibilities, that's all. I mean, you can't open a bit and see if it is a 1 or a 0. It's an abstract representation as we did with X and O with the sheet of papers. If you dig deeper and deeper you'll find out that all of these 0s and 1s are the states of logic gates. "Brrr...Gates, damn it...Maybe he isn't trully Gates and then he changed his surname..." Get rid of your Xs and Os sheets and go catch new ones and write on them big 0s and 1s. Now you have a sh*t string (ok joke's over)

Imagine you are on a room with two persons. A collegue and a strange guy. Both of you know this bits things and the strange guy doesn't. If you want to send a secret number n to your friend then all you need to do is setting that bit string in the nth combination for the n number! (there are various types of representation of decimal numbers). Here is a basic idea on how you can represent numbers starting from 0. All you need to do is label each sheet from left to right with a power of two. For example from left to right with 8 bits: 128 - 64 - 32 - 16 - 8 - 4 - 2 - 1.

Then, the sheet with a one is the number you labeled. The sheet with a 0 means nothing. If you add all the labels that the sheet has a one, then you have a decimal number. You may be wondering why 256 isn't listed above if we have 8 bits. Well, if you add 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1, (all the sheets with 1s), then you'll realize that the result is 255 and that's the maximun number you can represent. "Hey! you've stolen one!" Don't forget about the 0. "Damn..". Many people think that bits are some kind of magic things. That's why I'm writting this. Actually, when someone says: "bit it's the contraction of binary digit", people looks at you as if you said something weird. Binary means all of the above but, in a single word. Binary, two states, two faces...If you want to dig deeper go ahead, dig. Computers are electrics machines in terms that they use electricity to function. A low level voltage, a high level voltage. A 0, a 1. An X, an O. False, True. Red, Green, Keep in what layer you want but know it.
Next time, shift.






