Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Monday, 17 December 2012

How are 16- and 32-bit numbers stored?



A 16-bit number takes two bytes of storage, a most significant byte and a least significant byte. If you write the 16-bit number on paper, you would start with the most significant byte and end with the least significant byte. There is no convention for which order to store them in memory, however.
Let's call the most significant byte M and the least significant byte L. There are two possible ways to store these bytes in memory. You could store M first, followed by L, or L first, followed by M. Storing byte M first in memory is called "forward" or "big-endian" byte ordering. The term big endian comes from the fact that the "big end" of the number comes first, and it is also a reference to the book Gulliver's Travels, in which the term refers to people who eat their boiled eggs with the big end on top.
Storing byte L first is called "reverse" or "little-endian" byte ordering. Most machines store data in a big- endian format. Intel CPUs store data in a little-endian format, however, which can be confusing when someone is trying to connect an Intel microprocessor-based machine to anything else.
A 32-bit number takes four bytes of storage. Let's call them Mm, Ml, Lm, and Ll in decreasing order of significance. There are 4! (4 factorial, or 24) different ways in which these bytes can be ordered. Over the years, computer designers have used just about all 24 ways. The most popular two ways in use today, however, are (Mm, Ml, Lm, Ll), which is big-endian, and (Ll, Lm, Ml, Mm), which is little-endian. As with 16-bit numbers, most machines store 32-bit numbers in a big-endian format, but Intel machines store 32-bit numbers in a little-endian format.


Written by 'Shojib'.

Are bit fields portable?



Bit fields are not portable. Because bit fields cannot span machine words, and because the number of bits in a machine word is different on different machines, a particular program using bit fields might not even compile on a particular machine.
Assuming that your program does compile, the order in which bits are assigned to bit fields is not defined. Therefore, different compilers, or even different versions of the same compiler, could produce code that would not work properly on data generated by compiled older code. Stay away from using bit fields, except in cases in which the machine can directly address bits in memory and the compiler can generate code to take advantage of it and the increase in speed to be gained would be essential to the operation of the program.


Written by 'Shojib'.

What is the most efficient way to store flag values?



A flag is a value used to make a decision between two or more options in the execution of a program. For instance, the /w flag on the MS-DOS dir command causes the command to display filenames in several columns across the screen instead of displaying them one per line. In which a flag is used to indicate which of two possible types is held in a union. Because a flag has a small number of values (often only two), it is tempting to save memory space by not storing each flag in its own int or char.
Efficiency in this case is a tradeoff between size and speed. The most memory-space efficient way to store a flag value is as single bits or groups of bits just large enough to hold all the possible values. This is because most computers cannot address individual bits in memory, so the bit or bits of interest must be extracted from the bytes that contain it.
The most time-efficient way to store flag values is to keep each in its own integer variable. Unfortunately, this method can waste up to 31 bits of a 32-bit variable, which can lead to very inefficient use of memory. If there are only a few flags, it doesn't matter how they are stored. If there are many flags, it might be advantageous to store them packed in an array of characters or integers. They must then be extracted by a process called bit masking, in which unwanted bits are removed from the ones of interest.
Sometimes it is possible to combine a flag with another value to save space. It might be possible to use high- order bits of integers that have values smaller than what an integer can hold. Another possibility is that some data is always a multiple of 2 or 4, so the low-order bits can be used to store a flag.


Written by 'Shojib'.

Sunday, 8 January 2012

Episode 14 : Function Definition


Function Definition

When we use function in the program,  the identifier is called the function definition. This is declare that what kind of statement will be execute in this function.

Function has two main part – function header and function body.

data_type function_name (parameter or argument  list)
header
{
     statement ;
}

body


Function Header : It has three parts –
* Function Name : Function name is defined by the programmer like define the variable name. The rule of define name are same of both variable and function. Keywords are not applicable for function name like variable. We use the letter and underscore between the two or more words. One condition is always  remember that don’t use the same name of library function to user-define function. Example : pay_roll(), student_data(), Printf() [printf() is library function. Difference is cases.] etc.

* Parameter or argument :  Parameter is a general variable of function and it is define in the first bracket of the function. Generally we use this parameter for passing the data from one function to another function and the parameter is work as local variable of that function.  Argument is a value of the parameter which is define in an another function. When a function has a parameter and it is called from another function then call that function with a parameter value as known argument. Argument define in the first bracket when the function is called.

* Return type : We know that when we declare a variable with data type, The same process we use in function. We declare the data type of the function for telling the compiler what type of data it pass.

Example :

 
Output :

I hope you are all understand that operation.

Written by “Shojib”

Episode 13 : Function


Function

Function is a procedure that divided the program into different part for execute one or more statement or instruction.
Function is for make program to easy and finish step by step process. It also increase the reusability of code and we can use these function in different location in our program.
Dividing the program into different small part is called modular programming.

For small program we can declare instruction into main() function but when we write long program like as  we write a database program for an company then we need to define many instruction for different operation like insert data, delete data, modify data etc. If we write these instruction into only  main() function then so much difficult to understand. If we define function by categories it will be so easy and better understand.

There are two types of Function

1.     Library Function : In our previous example , we use printf() function for showing something to the screen. How to work this function it is defined by the compiler and we are just use this function but we are not written this function. Many function are declared previously like printf(), scanf() main() etc. is called library function.

2.     User-defined Function : For our needed, we use so many library function but we need to create another function for our program which is made by us. These function is called user-define function

Example :



Output :


Here declare a user-define function name msg() outside the main() function and we called that function from the main() function. Compiler  start compile from the main(), when compiler find that any function called then the compiler stop compile the main() and find the calling function and execute that function and after execution the calling function compiler come back to the main() function and execute the other statement.

[* When compiler find the second bracket then the compiler define it to function. ]

I hope you are understand the matter.

Written by “Shojib”

Friday, 23 December 2011

Episode 12 : while loop

while loop

When we exit any game then we watch some text like this –
Abort game : y/n
Are you really exit the game : Y/N etc.

Here if user press ‘Y’ then the game will be exit neither continue. Actually in our program behave of while loop like this.

Structure of while loop :

while (condition)
{
Statement 1;
----------------
Statement n;
}



At first computer work from the condition parts. Here we use the conditional expression. If value of the condition is not 0 or false then the statement will be evaluated neither not.

Example :

While (ch != ‘q’)
{
printf(“You did not press q”);
}
printf(“You press q”);


Here if you did not  press q key then the statement will execute neither  the second printf() function execute.

#include <stdio.h>
void main()
{
printf(\n Press any key without N or n for a noisy sound”);
ch = getche(); //read character and store it to ch

while ((ch != ‘N’ ) && (ch != ‘n’))
{
putch(‘\a’);           //echo by speaker
putch(‘\b’);          // backspace
ch = getche();      // read character and store to ch
}
printf(“\n\n\n Thank you for not hearing a  noisy sound”);
}


After run this program, when you type any key without ‘N’ or ‘n’ then the speaker of computer make a bip sound but when you press just “n’ or ‘N’ then no sound occurred and the while loop condition is false then while loop is stop and “printf(“\n\n\n Thank you for not hearing a  noisy sound”);”
 this statement will execute.


Written by ‘Shojib