COME ON CODE ON

A blog about programming and more programming.

Builtin Functions

leave a comment »

There are some useful bulitin function in gcc. Here are some of them :

Built-in Function: int __builtin_ffs (unsigned int x)

Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.

Built-in Function: int __builtin_clz (unsigned int x)

Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.

Built-in Function: int __builtin_ctz (unsigned int x)

Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.

Built-in Function: int __builtin_popcount (unsigned int x)

Returns the number of 1-bits in x.

Built-in Function: int __builtin_parity (unsigned int x)

Returns the parity of x, i.e. the number of 1-bits in x modulo 2.

Try them out yourself. Here’s a sample program.

int main()
{
    int x=30;
    printf("Index of least significant bit:%d\n",
    __builtin_ffs(x)-1);
    printf("Number of leading zeroes :%d\n",
    __builtin_clz(x));
    printf("Index of highest set bit:%d\n",
    31-__builtin_clz(x));
    printf("Number of trailing zeroes :%d\n",
    __builtin_ctz(x));
    printf("Number of 1-bits:%d\n",
    __builtin_popcount(x));
    printf("Parity:%d\n",__builtin_parity(x));

   
    printf("File Name:%s\n",__FILE__);
    printf("DATE:%s\n",__DATE__);
    printf("TIME:%s\n",__TIME__);
}

Also many times in graph algorithms we require +ve INFINITY and -ve INFINITY in our programs. There you can use :

int main()
{
    printf(“+ve INFINTY: %d\n”,INT_MAX);
    printf(“+ve INFINTY: %d\n”,(1<<31)-1);     printf("-ve INFINTY: %d\n",INT_MIN);     printf("-ve INFINTY: %d\n",1<<31); }[/sourcecode] PLAY WITH BITS!!!If you know others do share. -fR0D

Advertisements

Written by fR0DDY

April 17, 2009 at 1:10 PM

Posted in Programming

Tagged with , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: