Browsed by
Month: September 2009

How to calculate the no of bits in an integer

How to calculate the no of bits in an integer

For calculating number of bits in an integer value we can have many approaches. This is more simple in my opinion. If you know anything much simpler than these algorithm you can very well share the knowledge or idea with me.Ask me if you have any doubts in this.
For calculating the no of bits in an given Integer

#include
int main()
{
int number=42,counter=0;
while(number)
{
counter++;
number = number >> 1;
}
printf(“total bits %d”,counter);
}

For calculating the no of 1 bits in an given Integer

#include
int main()
{
int number=42,counter=0;
while(number)
{
counter++;
number = number & (number – 1);
}
printf(“total bits %d”,counter);
}

For calculating the no of 0 bits in an given Integer

#include
int main()
{
int number=42,counter=0;
while(number)
{
counter++;
number = number & -1 ;
}
printf(“total bits %d”,counter);
}

How memory leak occurs?

How memory leak occurs?

Memory leak is a problem that happens in programs when there is some unnecessary memory consumption or when program forgets to release the memory it used.
For example

void karthi(void)
{
int *ngk = malloc(20);
return;
}

int main(void)
{
while(1)
karthi();
return;
}

In this program from the main function we are calling the function called karthi(). Inside that function we are allocating 10 bytes for a integer pointer using malloc function. But before using that allocated memory space that function ends and the allocated space doesn’t used and it still remains in memory as unused. This is called as memory leak.

Difference between Session and Cookie

Difference between Session and Cookie

There is some difference between a session and a cookie. Anyway both of them performs the same functionality of maintaining some user information. But in a different ways.

Session

Session consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

Cookie

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
Difference between Session and Cookie

* If you set the variable to “cookies”, then your user wont have to log in each time they enter into the portal.
* The cookie will be saved in a place within the user’s browser until it is deleted by the user.
* But Sessions are popularly used than cookies, as the there is a chance of your cookies getting blocked if the user browser security setting is set high.
* If you set the variable to “sessions”, then user activity will be tracked using browser sessions, and your users will have to log in each time they re-open their browser. Additionally, if you are using the “sessions” variable, you need to secure the “sessions” directory, either by placing it above the web root or by requesting that your web host make it a non-browsable directory.
* The Key difference would be cookies are stored in your hard disk whereas a session aren’t stored in your hard disk. Sessions are basically like tokens, which are generated at authentication. A session is available as long as the browser is opened.

Converting a string to upper case

Converting a string to upper case

This function converts strings into upper case in C language. It is a simple one. I use ternary operator.
Code:

void UppCase(Char *str)
{
while(*str!=0)//traversing until end of atring

/* check whether the char is between ‘a’ and ‘z’. If it is subtract the ASCII value of ‘a’ and add ASCII value of ‘A’. Otherwise print the char as it is */

*str = (*str>=’a’ && *str <= ‘z’)?(*str-‘a’+’A’):*str;
str++;//increment pointer to next position
}

Reverse a Linked List

Reverse a Linked List

It is simple just a four lines of code. It will be more useful for students during placement timings.
Code:

Void ReverseLinkedList(node* header)
{
node *temp,*current,*result;
temp=NULL;
result=NULL;
current=header;
while(current!=NULL)
{
temp=current->next;
current->next=result;
result=current;
current=temp;
}
header=result;

}

Storing fonts into SQL Server Database

Storing fonts into SQL Server Database

Recently I got a project from one of my relative’s organization. It deals about maintaining their customer’s details along with their credit amount and it must generate reports based on their specification and exporting the report results into excel sheet.

Actually I thought it was an easy one to do with ASP.Net grid view controls. So I have started with that. In that application we must store the customers name in Tamil font. It was the main one. Every name will be a combination of tamil and english letters. For example, his/her initials will be in english and name will be in tamil.

To store these type of data into the database we can use the data field type “nVarChar” in SQL server.I am using SQL server 2005 express edition. It is mainly for storing UniCodes. While inserting the data into the database add a prefix “N”. For example your insert query must be like
insert into karthidb(N’unicodedata’,30);

It must be like this. Really it was very much useful for me. I used Thagadoor font and also a java script for toggling from tamil font to english font and vice versa. For any doubts regarding this feel free to contact me.