Reversing a number

int n=12345,m=0;

cout«"Orginal No:“<

while(n>0)

m *= 10; 

m += n%10

n /= 10;

}   

cout«”\nRevesed No:“<


Recursive function

int reverse\_num(int n,int m)  
  
{  
  
    if(n==0)  
  
        return m; //base (exit condition)  
  
    m\*=10;  
  
    m+=n%10;  
  
    return reverse\_num(n/10,m); //recursive call  
  
} 
```

To reverse the bits in an integer

Method1 ` unsigned int num; // Reverse the bits in this number. unsigned int temp = num; // temp will have the reversed bits of num. int i; for (i = (sizeof(num)*8-1); i; i–) { temp = temp | (num & 1); temp «= 1; num »= 1; } temp = temp | (num & 1); ` Method2 In this method, we use a lookup table. ` const unsigned char ReverseLookupTable[] = [Read More]

Check if the 20th bit of a 32 bit integer is on or off?

AND it with x00001000 and check if its equal to x00001000

if((num & x00001000)==x00001000)

Note that the digits represented here are in hex.

`
     0      0      0      0      1      0      0      0
                                 ^                   
                                 |

 x0000   0000   0000   0000   0001   0000   0000   0000 = 32 bits

  ^                              ^                    ^
  |                              |                    |

  0th bit                        20th bit             32nd bit
`

Count bits set in an integer

Question: Write a function that determines the number of bits set to 1 in the binary representation of an integer. Method 1 - Simple method (Right shift integer until it becomes 0) Going by the brute force approach, it would be easy to come up with the following solution. If the least significant bit of a number is 1, it will return a result of 1 when we AND it with 1. [Read More]

File Handling in c

/* Program to create a file and write some data the file */ #include #include main( ) { FILE *fp; char stuff[25]; int index; fp = fopen(“TENLINES.TXT”,“w”); /* open for writing */ strcpy(stuff,“This is an example line."); for (index = 1; index <= 10; index++) fprintf(fp,"%s Line number %d\n”, stuff, index); fclose(fp); /* close the file before ending program */ } FILE * fopen(char * filename, char * mode) The mode value in the above example is set to ‘r’, indicating that we want to read from the file. [Read More]

Syntax in C

qualifier:
            volatile
            const

storage-class: 

            auto            extern
            static          register

type:
            void            char            short
            int             long            float
            double          signed          unsigned
            enum-specifier
            typedef-name
            struct-or-union-specifier

What is a dangling pointer? What are reference counters with respect to pointers?

A pointer which points to an object that no longer exists. Its a pointer referring to an area of memory that has been deallocated. Dereferencing such a pointer usually produces garbage. Using reference counters which keep track of how many pointers are pointing to this memory location can prevent such issues. The reference counts are incremented when a new pointer starts to point to the memory location and decremented when they no longer need to point to that memory. [Read More]

Operator Precedence in C & CPP

Operator Precedence Chart Operator Type Operator Associativity 1. Primary Expression Operators () [] . -> expr++ expr– left-to-right 2. Unary Operators * & + - ! ~ ++expr –expr right-to-left (typecast) sizeof() 3. Binary Operators * / % left-to-right + - » « < > <= >= == != & ^ | && || 4. Ternary Operator ?: right-to-left 5. Assignment Operators = += -= *= /= %= »= right-to-left [Read More]