Meaning of Perception (from google search) –
1. the ability to see, hear, or become aware of something through the senses.
2. the way in which something is regarded, understood, or interpreted.
The first meaning does not go with this discussion, second one is of my today’s interest. “the way in which something is regarded, understood, or interpreted”. Lets talk more about it-
We all know the meaning of thumbs-up. It means I liked your work, I appreciate your thought. But not always. If you show thumbs-up to someone not from (or familiar with) western culture s/he may interpret it differently. S/he may consider it equivalent to what middle finger mean in western culture. I know if you had shown thumbs up to my grand father he would slap you in the face. Meaning of thumbs-up changes as it is interpreted . More on thumbs-up
Here is another. You found a note on your desk with two vertical parallel lines drawn on it, underneath that written “number of people going to attend the meeting”. This is a note form your boss to prepare yourself for the presentation. Your boss knew you are little nervous speaking in front of large number of people and he did not want you to be surprised when you enter the meeting room. So, he left you this note for your mental preparation. But, he could not have done a great job. You are still confused about the number of people going to be there. Because, meaning of these two vertical lines can be quite diverse. You are at least sure that these lines represent a number. But, what number is that? Answer to this question totally depends on your perception. Depending on how you interpret it can represent two, three, nine, eleven, seventeen and lots of other possibilities. So, the same two vertical lines can represent two in roman number system, three in binary (of our interest), nine in octal, eleven in decimal, seventeen in hexadecimal. (are you a nerd? that’s eleven, go with it.)
The meaning of same thing can be different depending on ones perception. Same is true for data stored in computer memory. Data are stored in computer memory as strings of 1s and 0s. But, what these strings of 1s and 0s mean totally depend on how you (your program) interpret them. Here data type comes in. You might wonder why some programming languages have data types and what are they for. Well, if you are a “OOP” guy then you are probably thinking about data type as an interface (or class) that specifies what operations are allowed on them. But, that is a high level interpretation of data type. As I understand it, in low level (the level of our interest) data types are indication of how the data should be read/written from/to the memory and interpreted – number of bytes, signed/unsigned etc.
From here on I will assume you have some understanding of the C programming language. I am not assuming you understand pointers. Discussing pointer would be totally appropriate in this article. But, it will make the article too long. I also assume you have some understanding of computer memory and how it can be accessed randomly with addresses. You also understand that computer does not actually understand any of the programming languages available including assembly .
So, what happens when we declare a variable of a particular type. Like –
Actually nothing happens. This line of C code will not contribute a single line of code in our compiled program. Then, why do we write it if our program does not need it. Well, we write it for ourselves. We need it. We can’t keep track of all memory addresses for all our variables and functions. Compiler assigns addresses to all the variables declared in a program and then uses corresponding addresses wherever we use these variables. Lets say our variable i declared earlier is assigned address 0x002FC2 (some arbitrary address). Then
i = i + 1;
will be translated to ‘add 1 to the value at memory location 0x002FC2 and put the result back at memory location 0x002FC2’. Well, that does not seem like we need a type for that. Compiler could assign unique addresses to every variable without a type. But, compiler won’t know how many bytes from address 0x002FC2 to be interpreted as the value of i. We tell the compiler that 4 bytes starting from address 0x002FC2 should be considered as the value of i. We tell it by specifying its type as int.
Lets consider another example –
unsigned int x = -1;
unsigned int y = 0;
Now the condition x < y is false. If you are thinking, -1 is assigned to an unsigned integer so it will lose the minus sign and x becomes 1. You are completely wrong. This reminds me of a junior from my university who needed to calculate absolute difference of two numbers (UVA 10055. Hasmot the brave warrior, if you remember). He then wrote something like –
unsigned int diff = a - b; // a and b were declared as int.
Then he asked me why the program shows 4294967291 for a = 10 and b = 15.
Here x < y is false because binary representation of -1 is a string of thirty two 1s . The most significant bit (the leftmost bit) of these 32 bits is the sign bit. This sting of thirty two 1s is then copied into the 4 bytes of memory starting at the address assigned to x by the compiler. similarly 0 is represented as a string of thirty two 0s and copied into the 4 bytes of memory starting at the address assigned to y. When comparing x < y you are instructing the computer to read content of x as an unsigned integer (again, indicated by the data type of x). In C that means read 4 bytes from the memory address assigned to x and there is no sign flag, all 32 bits represent data. So, the value of x becomes 4294967295. The value of y is read similarly which is 0 and the condition x < y being false now make more sense.
Data types gives compiler a way to know how to interpret memory. It also provides a way for the compiler to warn the programmer if they unintentionally use incompatible types. If you pass a double value to a function where int is expected you will get a warning message. Your program will run but the result may not be correct. Beside primitive data types C allows programmers to define their own compound data types with struct construct. If try to place one type of these user defined variable where some other type is expected you will get an error. No mercy, this is not allowed at all.
Lets meet union, our old forgotten friend. These days people usually do not use it that much. But it has its place in linux kernel, Microsoft Foundation Class (MFC) framework and probably in some other places. When we have two or more heterogeneous types of data and only one of which will be needed at any instance we can share the same memory for all of them. We can do it by telling our compiler to assign same address to a group of variables united as union. Lets see some example.
union and struct are declared similarly.
Now my_union becomes a new type of which we can declare new variables.
u has three members u.x, u.y and u.z unlike struct we can use only one of them at a time. All the three member share the same address. If you don’t trust me try printf(“%p %p %p”, &u.x, &u.y, &u.z). If we use u.x we can put an integer in it and use it for a while. When we need a double we use u.y and use it as long as we need it. You are probably think about how much memory is reserved for u. It is the width of the widest member in the union. In this case it is sizeof(double).
Now that you’ve met old friend ‘union’ you are think about its usefulness. Is it really worth using? Or is there any use case for this? Well, of course there’s use case for it. Someday I will tell you how it is used in MFC for dispatching messages to corresponding handler function of varying signature, and when I will be able to understand linux kernel code I will tell you how and why they keep using it.
 Analogies always fail, don’t count on them.
 I could not come up with something more interesting line.
 To be specific infinite number of possibilities, if you can come up with enough symbols to represent all the digits in any number system.
 Assembly is the lowest level human readable programming language.
 Actually it will be translated to machine code. I am saying it in English for you.
 Negative numbers are represented in two’s complement form.
 Unless otherwise specified, numeric literals are signed in C.