Showing posts with label Conversion. Show all posts
Showing posts with label Conversion. Show all posts

Wednesday, 27 April 2016

Number Swapping

/*
 * C Program to swap two numbers.
 */

// Includes
#include <stdio.h>

/*
 * Description : Function to swap 2 integers using call by reference.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swap(int *iPtr_num1, int *iPtr_num2)
{
    float temp;
    
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Uses temporary variable to swap 2 numbers.
    temp = *iPtr_num1;
    *iPtr_num1 = *iPtr_num2;
    *iPtr_num2 = temp;
}

/*
 * Description : Function to swap 2 integers using addition and subtraction.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swapUsingAddSub(int *iPtr_num1, int *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using addition and subtraction method
    *iPtr_num1 = *iPtr_num1 + *iPtr_num2;
    *iPtr_num2 = *iPtr_num1 - *iPtr_num2;
    *iPtr_num1 = *iPtr_num1 - *iPtr_num2;
}

/* swapUsingBitwise
 * Description : Function to swap 2 integers using bitwise operators.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swapUsingBitwise(int *iPtr_num1, int *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using bitwise operators
    *iPtr_num1 = *iPtr_num1 ^ *iPtr_num2;
    *iPtr_num2 = *iPtr_num1 ^ *iPtr_num2;
    *iPtr_num1 = *iPtr_num1 ^ *iPtr_num2;
}

/* swapUsingMultDiv
 * Description : Function to swap 2 integers using multiplication and division.
 * Parameters  : iPtr_num1, iPtr_num2 - numbers to be swapped.
 * Returns     : Nothing. Swapped numbwes are directly reflected in calling function.
 */
void swapUsingMultDiv(float *iPtr_num1, float *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using multiplication and division method
    *iPtr_num1 = (*iPtr_num1) * (*iPtr_num2);
    *iPtr_num2 = (*iPtr_num1) / (*iPtr_num2);
    *iPtr_num1 = (*iPtr_num1) / (*iPtr_num2);
}

int main()
{
    int i_num1, i_num2;
    float f_num1, f_num2;
    
    printf("Enter numbwes to be swapped : ");
    scanf("%d %d",&i_num1, &i_num2);
    
    // Swap 2 numbers and print
    swap(&i_num1, &i_num2);
    printf("Swapped                   (%d,%d)\n",i_num1, i_num2);
    
    swapUsingAddSub(&i_num1, &i_num2);
    printf("Swapped using add-sub     (%d,%d)\n",i_num1, i_num2);
    
    swapUsingBitwise(&i_num1, &i_num2);
    printf("Swapped using bitwise xor (%d,%d)\n",i_num1, i_num2);
    
    // For multiplication-division method, arguments needs to be float
    f_num1 = i_num1;    // implicit type casting will take place.
    f_num2 = i_num2;    // implicit type casting will take place.
    swapUsingMultDiv(&f_num1, &f_num2);
    printf("Swapped using mult-div    (%g,%g)\n",f_num1, f_num2);

}

Tuesday, 4 August 2015

Number Systems and Conversions

What is decimal number system ?
A decimal number system is the one which uses numerals from the set 0,1,2,3,4,5,6,7,8,9 to represent a number in the system. It is also called as base 10 or sometimes denary.
Examples :
153   = 1x102 + 5x101 + 3x100                          =   100 +   50 +   3       =   153
3489 = 3x103  + 4x102 + 8x101 + 9x100    = 3000 + 400 + 80 + 9 = 3489

What is Binary number system ?
In binary number system, we represent a number by using only two numerals viz. 0 and 1. The main use of binary number system is in computers to represent bits as on or off.
Example :
101       =                                   1x22 +  0x21 +1x20  =                        4 + 0 + 1 =   5
110011 = 1x25 + 1x24 + 0x23 + 0x22 + 1x21 +1x20  =  32 +16 + 0 + 0 + 2 + 1 = 51

Decimal To Binary Conversion
Humans use decimal number system but computers use binary number systems. Without this conversion it would be very difficult to interact with computers. Let us see how we can achieve conversion of decimal number to binary number.

Following steps are involved in converting a decimal number into a binary
1. Take the decimal number
2. Divide the number with 2 and append the reminder in a sequence.
3. Take the quotient and make it as a number.
4. Repeat steps 2 and 3 until quotient becomes 0.
5. Read all the appended reminders backwards - giving you the desired binary number.

Example :
Decimal Number - 169