Archive | October, 2010

c training – bit wise operators part 3 (more exmaples)


/**************************************************************************************************
*
Function : Write_5BitPartern
*
Description : Will fill up the the memory with certain bit partern.
* Routine# : 6
**************************************************************************************************/

#define BIT_PARTERN 0×17  //(’00010111′)
#define BIT_PARTERN_SIZE (5)

void Write_5BitPartern()
{
unsigned int memory_size = ~0;
int i, j, numbit;
unsigned char *buf = 0×0,
unsigned char bit_partern = BIT_PARTERN;
unsigned char temp = 0;

j = BIT_PARTERN_SIZE -1;
for(i = 0; i < memory_size; i++)
{
for(numbit = 7; numbit >= 0; numbit–)
{
buf[i] = 0;
if( (bit_partern >> j) & 0×01)
{
buf[i] |= (0×1 << numbit) ;
}
if(0 == j)
{
j = BIT_PARTERN_SIZE;
}
j–;
}
}
}

Posted in C - Fresher questions, C Tutorials0 Comments

c traning – bitwise(bit wise) operators usage part 2 ( More examples)


/**************************************************************************************************
* Function : bitwise_compare
* Description : Compare two numbers and return greater one with out using any arithmetic operator.
* Routine# : 5
**************************************************************************************************/


#define FIRST_NUM    0
#define
int bitwise_compare(int num1, int num2)
{
int tmp1, tmp2 = 0×1 << sizeof(tmp1)-1;            //initially tmp2 value is set as 10000 …….. (31 zeros) to check whether the number is negative or not
short greater = 0;

tmp1 = num1 ^ num2;

if(!tmp1)
{
printf(“%d == %d!\n”, num1, num2);
return 0;
}
else if(tmp1 &  tmp2)
{
if(num1 & tmp2)
{
greater = 1;
}
}
else
{
for( ;tmp1; tmp1 &= (tmp1-1))     // calculate highest significant bit
tmp2 = tmp1;

if(num2 & tmp2)
{
greater = 1;
}
}

if(greater)
{
printf(“%d > %d\n” , num2, num1);
return num2;
}
else
{
printf(“%d > %d\n” , num1, num2);
return num1;
}
}

int main()
{
int num1, num2;
int greater;

printf(“Please enter two numbers: “);
if(2 != scanf(“%d%d”, &num1, &num2))
{
return -1;
}
greater = bitwise_compare(num1, num2);
getch();
return 0;
}

Posted in C - Fresher questions, C Tutorials0 Comments

C training – Bitwise operators, usage with examples

/**************************************************************************************************
* Function : bitwise_addtion
* Description : Add two number without using ‘+’ or arithmetic operator.
* Routine# : 3
**************************************************************************************************/

int bitwise_addtion(int augend, int addend)
{
int num1, num2, a, b ;

num1 = augend;
num2 = addend;

while (num2)    {
a = num1 ^ num2;
b = num1 & num2;
b <<= 1;
num1 = a;
num2 = b;
}

return num1;           /* total summation */
}

/**************************************************************************************************
* Function : bitwise_substraction
* Description : Substract two number without using any aritmetic operator.
* Routine# : 4
**************************************************************************************************/

int bitwise_subtraction(int augend, int addend)
{
int num1, num2, a, b ;

num1 = augend;
num2 = addend;
while (num2)    {
a = num1 ^ num2;
b = ~num1 & num2;
b <<= 1;
num1 = a;
num2 = b;
}

return num1;           /* substraction value */
}

Posted in C - Fresher questions, C Tutorials0 Comments

Best .net Training: Why C# not allowing multiple inheritence

1.C# is not allowing the multiple inheritance because of the below reason.

public class A
{
public virtual int m()
{
return 10;
}
}
public class B : A
{
public override int m()
{
return 20;
}
}
public class C : A
{
public override int m()
{
return 30;
}
}
public class D : B, C
{
//If C# supports multiple inheritence.
//then from which class ( B or C) the method will be inherited
//This is where compiler gets confusion.
//that is the reason why C# not supporting multiple inheritence.
public void test()
{
}
}

Posted in C# FAQ7 Comments

c training – Bitwise operators usage

This post will tell about how Bitwise operators can be used and how to use them

Topic: Bitwise

/**************************************************************************************************
Function: show_binary_format
Description : Display the number in binary format.
* Routine# : 1
**************************************************************************************************/
void show_binary_format (unsigned int num, int size)
{
short i;

for (i = (8 * size) -1; i >= 0; i–)    {
if ( (num >> i) & 0×1)
putchar (’1′);
else
putchar (’0′);
if (!(i % 4))
putchar (‘ ‘);
}
putchar (‘\n’);

/**************************************************************************************************
Function: show_binary_format
Description : Display the number in binary format.
* Routine# : 1
**************************************************************************************************/
void show_binary_format (unsigned int num, int size)
{
short i;

for (i = (8 * size) -1; i >= 0; i–)    {
if ( (num >> i) & 0×1)
putchar (’1′);
else
putchar (’0′);
if (!(i % 4))
putchar (‘ ‘);
}
putchar (‘\n’);

We will see more examples on bitwise operators on later sessions..

Posted in C - Fresher questions, C Tutorials0 Comments

c training – variable number of arguments

Variable Argument ( C language )

This post will give 2 sample examples on how to use VARIABLE NUMBER OF ARGUMENTS in C

Topic: Variable Argument

Program-1
/***************************************************************
* Function : add_variably
* Description : Can take variable number of arguments as para-
* meter. But addition is subject to the first argument. In fact first
* argument tells the number of element will take part in the
* addition.
***************************************************************/
#include <stdio.h>**

int add_variably (int, …);

int main ()
{
printf(“The total result : %d\n”, add_variably(4, 10, 20,30,100));

}

int add_variably (int i, …)
{
char *ptr;
int  total = 0;

ptr = (char*) (&i + 1);
while (i–)
{
total = total + (int*)ptr[0];
ptr += sizeof(int) ;
}
return total;
}

Program-2
/***************************************************************
* my_printf functon is an example of variable argument conceptc
* This function can be called instead of printf(). This function still
* use printf() for conversion purpose only.
* Please note this function is not as powerful as printf().
***************************************************************/
#include <stdio.h>

void my_printf (char *p, …)
{
char *q;

q =  (char*) ((char**) &p + 1);

for (; *p; p++)    {
if (*p != ‘%’)    {
putchar (*p);
continue;
}
switch (*++p)    {
case ‘d’:
printf (“%d”, *(int*) q);
q += sizeof (int);
break;

case ‘s’:
printf (“%s”, *(char**) q);
q += sizeof (char*);
break;

case ‘c’:
printf (“%c”, *(char*) q);
q += sizeof (char);
break;

default :
putchar (*p);
break;
}
}
}

Lets see more examples in later sessions on different C concepts ….

Posted in C - Fresher questions, C Tutorials0 Comments

Best .net Training: writing Custom Attribute

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor)]
public class Author : System.Attribute
{public string _name = string.Empty; Publie Author (string name) { _name=name; }}
[Author("Palle Technologies is the Author of Doctor Class")]
public class  Doctor{// Note : When you compile this class an entry is made into the metadata section of the PE file with the name Author,//Value of the author becomes Palle Technologies is the Author of Doctor Class.}
Note: Leave comments after reading the article

Posted in .NET Framework FAQ0 Comments

what is Attribute in .Net Framework

Attribute is a class which will be usefull in adding meta data in the assemblies.

Posted in .NET Framework FAQ1 Comment

C training – Lifetime and scope of variables

Life time and scope:

================

Lets discuss about life time and scope of different variables/elements of your C program.

Life time : This tells about what will be the life time of an element in C program.

Scope       : This tells about what parts of program can access a variable/element of a C program.

Lets see what will be the life time and scope of different kinds of variables.

Auto / local variables : Its life time starts from starting of the function where it was declared and ends as soon as

that function ends.

Its scope is with in that function only (that means that variable declared in a function can’t be

accessed out side of that function.)

Global variables : Its life time starts from starting of the program and ends as soon as your program ends.

Its scope is through out that program .

Local static variables: Its life time starts from starting of the function where it was declared and will remain through out that

program.

Its scope is with in that function only (that means that variable declared in a function can’t be

accessed out side of that function.)

Note : This applies as long as you don’t pass pointer to that variable, if you pass the address then you can access

Global static variables : Its life time starts from starting of your program and remains through out the program.

Its scope is with in that file ( you can’t access it from other files of your program)

Note : This also applies as long as you don’t pass pointer to that variable.

Static functions: Its life time starts from starting of your program and remains through out the program.

Its scope is with in that file ( you can’t access it from other files of your program)

Note : This also applies as long as you don’t pass pointer to that function.

Normal functions : Its life time starts from starting of your program and remains through out the program.

Its scope is through out the program ( you can access it from all other files of your program)

In later sessions we will see complete pictorial representation of  memory architecture and scope and life time.

Posted in C - Fresher questions, C Tutorials0 Comments

c training – Memory layout of C program

Today lets see how and where memory will be allocated for different parts of a  C program

There are basically 4 segments for any given C program , named as below.

1. Text segment - Where your program code (in assembly code format)  will be stored.

2. Data segment - Where your program’s global initialized, static initialized variables will be stored.

3. Heap – Where are all your program’s dynamically allocated variable will be stored.

4. Stack – Where all your functions local variable (except local uninitialized static variables and local initialized static

variable) will be stored.

Lets see an example of above description in below example.

//test.c file

int global_var1 = 10; ==> data segement

int main()

{

int local_var1 = 5; ==> stack

static int local_var2 = 20; ==> data segement

int * dynamic_var3 =  malloc(sizeof(int) * 5); ==> heap

printf (“all done!”);

return 0;

}

After compiling this code, .obj will be generated and while running this program this obj has to be brought into

RAM , where this program’s assembly code will be stored in Text segment of this process

More on this memory architecture of C program in later sessions..

Throw comments if any..

Posted in C - Fresher questions, C Tutorials0 Comments