Sadly I have seen that not many want to learn to program this 2018 🙁 but even if I had only read my previous article a person and after a little time is able to send a commit to a free software project, I would be satisfied with my work 🙂
For those who love security, I promise you that the following will be a post about security todos so everyone is happy, if anyone wants to learn something else (like git, server administration, or I don't know: p), or to comment on some other topic that cannot be answered in a simple way in the comment box, let me know and we see how we can work it 😉
Well, now if we go to our thing, previously we talked about typing, and that this had to do with the way in which we save our variables in a program, now we are going to review a bit of what happens inside and hopefully it can be clear enough.
Bits
I think this is a topic that I always touch when I write about programming, it is certainly something that fascinates me and that has helped me understand many things, now I will try to explain a little how they are, how they are read, and what they are for 🙂
Think of a light switch, when the circuit is closed, we have a 0
on the screen, when we change the position of the switch, because a 1
🙂 simple isn't it?
Now a 0
or with a 1
They can mean many things, it all depends on the creativity with which you take it, suppose I want to know if someone is going to the North or the South, 1
can mean north and 0
, sur 🙂 let's say I want to know if someone is a man or a woman, 1
it can be a man and 0
, woman 🙂. Now I want to know if this person is young or old (> 22), 0
can mean young and 1
, higher. Let's keep imagining… Do you have any pets? 1
I would say yes, while 0
I'd say no Now I want you to read the following line with me:
1001
This is the short way to say ...
Una jóven mujer de no más de 22 años se dirige al norte acompañada de su mascota.
which is very different from:
0110
o Un hombre con más de 22 años de edad se dirige solo hacia el sur.
Bytes
Now let's go one step further, let's learn how to read bytes. A byte is the sequence of 8 bits, which are read from right to left and each 1
represents a power of 2
raised to the n
where n
is the position of the bit. As it sounds like Chinese, let's put a little example 🙂
01001011
We have this byte, now we are going to go from right to left (<-) I am going to put them from top to bottom to be able to write their meaning:
1
: the bit when in position 0
indicates that we have the following 2 raised to zero or 2^0
. This well we know is equivalent to 1
.
1
: the second bit, now the position 1
: 2^1
which is the same as saying 2
0
: third bit ... this should be 2^2
, but since it is not on, we are going to leave it on 0
1
: fourth bit, 2^3
u 8
: )
0
: the same as 0
0
: other 0
1
:now we are in 2^6
o 64
and finally 0
, we already know what it means 🙂 now we are going to add our results and compare them with the following table 🙂 We have a 75
so let's look for it in the column Decimal and we will see what appears in Char
We have one K
!! Congratulations, you already know how to read in binary 🙂 But the most sagacious may have noticed that we have also obtained a decimal number, and that it has a limit (when all values are 1
) That limit is found in the number 255
.
Word
Now more than one will tell me, but what if I need a number greater than 255
? or where can I find other characters like the Japanese? Well the answer is simple, let's put together 2 bytes
. Now that we have two, the possible number of combinations we have is 2^16
o 65536
possible outcomes, such as 0
is one of those, the maximum possible is 65535
. Does that number ring a bell to anyone? Remember the maximum number of ports on a linux system? I leave them homework 😉
Double word & quad word
For the more mathematical there are also specific formats, the double-word contain, as many may have already imagined 2 word
o 4 bytes
(o 32 bits
) of information, the same as saying:
11111111111111111111111111111111
or 0
a 4 294 967 295
At this point many will wonder what happens with negative numbers, that is, somewhere they must be contemplated, right? In order to be able to store a negative number, the processor developers chose to occupy the first bit on the left as a sign value. This means that if the first bit is 0
we are talking about a positive number, but if it is 1
we have a negative. Now see why the bits are so special, they can be whatever you want 😀
But this obviously leaves us with one less position to do the multiplication! So our0
a 4 294 967 295
becomes:
-2,147,483,648
a +2,147,483,647
Now, many of us already have 64 bits
, and this is the value of a quad word, we can have values ranging from 0
a 18 446 744 073 709 551 615
. That is a big number 🙂
Why 8 bit?
This is something that more than one may be wondering, and the answer is in the hardware. From the beginning, processors needed data to be able to perform operations. The data is stored in the memory of the computer and every time the processor requires it, it uses the data buses to get it. In ancient times, these buses could communicate a maximum of 8 bits per cycle, this means that the maximum and most efficient way of moving data, was grouping 8 bits and sending them to the processor.
With the passage of time, until today, processors have developed the ability to move 16 bits, 32 bits and… 64 bits.
What does it have to do with typing?
We are now at the part where it all makes sense 🙂 Typing is a property that programming languages use to name these memory spaces. All variables have their counterpart in one of these types of data, no matter what they are called. These are known as primitive data typesEach strongly typed language has its conception of these values, and the quantity they represent. For example in C we have the library limits.h
which shows us the maximum and minimum amount of primitive values.
Let's see what happens if we try to break one of the values:
On the right we have the values of the file limits.h
and on the left we have taken one of these values (unsigned short int
) and we have assigned a number higher than the corresponding one. As a result the compiler warns us that we are misusing memory because the binary form of 66666
cannot fit in the binary form of 65535
. This leads us to a lesson in performance when we program, if your value is not going to grow much over time, or if you do not require values as large as those of a double o quad wordUsing the correct type reduces the amount of memory requested by the CPU, which implies a higher data retrieval speed if it is well calculated.
On the interpreter side this is easier due to the implicit conversions. Chen we define a variable in languages such as javascript or Python, the interpreter is in charge of understanding what type it is, and allocating enough memory space to perform the operations. Let's see a simple example 🙂
As you can see, we don't have to explain to the python interpreter the type of our variable, because it is in charge of assigning a type and storing it in memory 🙂
Know your variables
This depends on the language and type of implementation that you are going to use, but the first step to be able to program is to learn the variables that you can use 🙂 Once you understand the variables, you will be in a position to use them efficiently and logically to store information (provided by a user or by the system). This is the first step on the programming ladder, and hopefully after reading this article, you will have a better understanding of how your computer works and how it stores information. It will be with me until the next article, remember to leave your comments to see if you have to reinforce or comment on any specific point. Cheers
Well written, succinct and clear, at the same time interesting for all audiences. Nice job.
Thank you very much, greetings 🙂
Great explanation. You're a machine.
Thank you
Very well explained thanks
Thanks to you for reading it in full 🙂
Excellent, thanks for the input. And taking advantage of the binary issue, there is a possibility that you can give us a class of IP, subnet, etc. I understand that it is not a programming issue, but a good explanation of that issue has always been pending.
Again, thanks for your explanation
Hello Diego, it can be done 🙂 to tell the truth I have not gone into the subject much either, but there is no better way to investigate than by looking for something to share about it in an article 🙂 We will leave it for the list because the next post already has a subject and it's going to deal with hardening. Greetings 🙂
You are an excellent teacher, and very generous for teaching what you know. Congratulations and thank you.
Thank you very much Pedro 🙂 I am still looking for places to teach, unfortunately here in Peru it is difficult when you only have a technical professional degree, so now I am looking at the possibility of continuing my university studies here or where I can or even apply to a master's degree in the foreigner, who knows, maybe soon something like that can be given 🙂 but definitely teaching is something that makes my day 🙂 Greetings
I had not read a better explanation of the subject, the first example is brilliant
although I had not heard that word (like 16-bit variables), double word, or quad word
no matter that "I already program", the posts are interesting. If there is to be any problem, the post is to use C (limits.h) as an example of variable sizes, C has the least specific specification that exists
Hello something 🙂 thank you very much, I came up with the example on the way 😛 because it is certainly known data for those who have read a bit of Assembly, and that is what the processor understands 🙂 hahaha certainly C is not very specific, but I think it is Due to the fact that portability and the different architectures it supports have been so varied that C has to accommodate each type of processor to be truly portable 🙂
Greetings and thanks for sharing.
Unnn it would be interesting if this series of tutorials were based on rust, I think it is one of the most interesting languages that are being developed today.
I am behind him, but I recognize that it is a difficult language, but with a great future ...
I look forward to your next articles, they are really interesting.
Greetings.
Hello Sergio, it would certainly be interesting, I have heard that GNOME is planning to implement Rust within its suite of programs, I have not yet seen the full scope they intend, but a migration is coming.
As for languages, I personally am still learning C, I want to be able to start developing in the kernel in the next few months, and I prefer to learn a few languages in depth before I start reviewing new ones, but I can definitely look for something interesting and do a few examples in Rust, since its documentation looks pretty good on its own.
Greetings and thanks for sharing 🙂
Very good explanations, both in this article and in the previous one. I hope you continue with this theme that I find very interesting.
Hello David, I also hope to continue writing and especially motivating people to participate with free software, there are so many projects and needs now that having a few extra hands to develop would be great 🙂
regards
Could you make a post about the Turing machine?
Hello Mart 🙂 I think that in some comment we have touched it before, right? I'm sure I can put together something interesting on the subject 🙂 let's see what can come out. Greetings and thanks for reading all my articles, from the first day until today I have always seen you quite interested 🙂
Wow, thank you very much what a good explanation.
regards
Thank you very much Allan 🙂 regards
I read your previous post! Thank you very much for the explanation, although I think that I will never finish understanding the word.
Hi Ramon 🙂 thank you very much for reading both. If it is of any use, the «word» is the evolution of a byte, it is like the processors, before there were 8-bits, after 16-bits, etc ... this is because every time we require more space to process and better speed or power ... the 'word' was born because the space of the 'byte' fell short, and the same with the 'double' and 'quad' word 🙂 it is the natural evolution that makes us add power and space to continue developing 🙂
regards
This 2018 I learn to program yes or yes, thank you
I'm glad 🙂 try hard !! Cheers
Thank you very much, keep it up. I already know how to program but I await your teachings, that my self-taught programming is full of bad practices.
Thanks Guillermo 🙂 Well I hope you find interesting things also in the following posts 🙂 Greetings
A simple explanation is always the best .. excellent ..
I would propose git ... for programmers it is basic when you want to work as a team and put your work in order .. the versions ..
Very true Edgar, I will try to post content about it, although it is quite difficult to say something new because the git documentation is already abundant and several things are already translated into several languages. I'll see what I can do, greetings and thanks for sharing
Hello, does the number from 0 to 18 446 744 073 709 551 615 correspond to the 64-bit decimal value or does it refer to another value? Well, calculating the power of 2 ^ 64, the result I get is: 18 446 744 073 709 552.
Hi Tiririri, I think you are having a rounding problem, you can try using the command
bc
? is a programming language designed to be as precise as possible, once inside it it is only necessary to do2^64
, maybe that will solve it 🙂 let me know how it went, greetings. The real value is 18 446 744 073 709 551 616, but we are subtracting 1 because 0 counts as a possible value 🙂
Thanks, I calculated that with a power calculator that I found on the net (https://es.calcuworld.com/calculadoras-matematicas/potencias/) and that was the result it gave me.
And yes, I already tried the bc command, and it effectively gives me the value that you wrote down.
Hello again, the image where you refer to the limits.h library is very small and cannot be read (at least I couldn't read it, because I see that everyone did, it seems), and since I don't understand it I no longer continue reading. I don't know if you could change it, I hope I didn't bother you with that.
You can try the mobile version, those look a bit clearer, it's a wordpress theme: / on the other hand, all the C headers can be found in / usr / include /, in this case you can choose to read it directly if you it's easier 🙂 I'll try to upload a bigger image to see if it helps 🙂
Thanks, I think I should be patient with this, heh heh.
Thanks for the article because now I understood a little better the variables and range of representation of a language and but now I think I should start using the library "stdint.h" since they recommend it to use when programming in c since a I had been taught to the old school since memory was precious and one had to be careful with what was used
There is no Katekyo, it is certainly better to use the standard library, but I can assure you that it is better to know what distinguishes a uint_least8_t from a uint_fast8_t and that is achieved by knowing the primitive data types, which was the goal of the post 🙂 Many thanks for the comment and for reading the article, greetings
: Or a masterful explanation! I knew part of the concepts but I get a lot of new knowledge from this post
Thank you very much Victor 🙂 how good that the article can be of help, greetings
I really liked Python to make programming easier, I saw a course in http://zetcode.com/gui/pyqt4/ I loved the final tetris of: http://zetcode.com/gui/pyqt4/thetetrisgame/
This tetris I retouched during a summer to put points, multiplayer and music, little by little, but very entertaining.
Hello Guille, it is certainly very entertaining to see the software you are working on grow little by little 🙂 it is an incomparable experience, and even more so when someone finds what you are developing useful, that is from another world 😀 thank you very much for sharing 🙂
The one I did I have uploaded (30Mb for the music) to http://guillermo.molleda.com/Tetris9.7z
You have to configure a different initial speed for each player or if you restart points after a new game,…, and there are three players for my two daughters to play with me. It is even addictive.