Saturday, January 9, 2010

Some Simple C Programs To Play With Bits

I like programming ie., development. Since my proffessional career started, i am working on C programming only. Thats why always i will be working on C programming. I like to work on Programming.

While working on bit related programming like setting a bit, resetting a bit in an integer, i was getting confusions. Concept and theory will be present in my mind but i will take too much time to produce on paper. Programming on the IDE or any programming tool will be easy. But to write on a paper, me like people feel little difficulty.

Hence i have written some programs and the same i am giving here. This could be helpful to many to understand bits.

1. Program to make a bit 0 if it was 1 and to make 1 if it was 0

void BitToggle()
{
    int Data, Bit;
    int t1,t2;

    printf("Enter the data \n");
    scanf("%d",&Data);

    printf("Enter the bit number to be toggled\n");
    scanf("%d",&Bit);

    if((Data >> Bit) & 0x01) //if bit is set
    {
    Data &= (~(1 << Bit));
    }
    else //if bit is not set
    {
    Data |= (1 << Bit);
    }
}

This logic is very simple and you would get a better logic than this. But this is a good program to start work wround bits. I think no explaination is required for this.


2. Program to reverse the bit order of an integer

void ReverseBitOrder()
{
    for(i=0;i<32;i++)
    {
        p = data >> i; //'data' is the value to be reversed
        t = p & 1; // 'p' and 't' are temporary variables
        result = result | (t & 1);
        result <<=1;
    }
}

This is a good program to play with bits. Also, at firt time, it has taken some more time to do this. First i have tried on a paper. But after doing program only i could able to do because of debugging.

I think these are good programs to start work around bits in C.

Goooooooooood luccccccccccccck

----

Friday, January 1, 2010

Static functions in C

While discussing with my friends, I came to know that I have not understood properly about static functions in C language. I felt uncomfortable about static functions and decided to go through once again and make it perfect.

static void function() //Hidden from other files if not included
{
}

I knew about static functions that the static functions cannot be called from other files of the same workspace. But I tried the same but it didn’t work. That means I was able to call static functions from other files. But after reading one article on static functions, I felt that I was doing some mistakes.

Hence immediately sat with Visual C++ IDE and started to work on that. Created a project and added 2 source files, written main() function and added functions and also a static function.

I came to know the best example for the static functions which is DLL files in Windows. As already I have shared some information about the DLLs and their creation in my previous posts. In DLLs, we will export the functions in definitions (.def) files. We can call those functions only dynamically. Using definition files, we are making some functions public that means allowing to call them from outside the module. If we have some functions in our DLLs which are used by our DLLs only and they are not supposed to call from other than DLLs, those will not come into definition file.

In the same way the static functions are. If we make some functions ‘static’ in one file, those cannot be called from other than this file. In this way we are hiding some functions by from other files my making these as ‘static’.

The mistake what I was doing at first time that I was including the source file (.c file) which was having static functions in other file from where I was calling static functions. Without including the source files, static functions cannot be called.

But all files should be in the same workspace.

What could be the main use of static functions? This was my next question. One of the forums discussion article helped me in this regard.

Say we are writing one module which will have 100 functions required by other modules. But the same module will have other 15 functions which are internal functions called or used by the module itself. Then they will be ‘static’ functions. When we compile the all this file with out own source file in our workspace, whether it may be Linux or Windows IDE software, we are able to call only 100 function which are non static. I think, API functions of Operating systems are like this only. APIs should be non static. All APIs could be using their internal functions which will be static ie., hidden from outside world.

After all these, I felt OK on static functions.

Please drop a mail to Pramoda.ma@gmail.com for any mistakes, suggestions, clarifications or anything.

Good luck.